View Javadoc
1   /*
2    *    Copyright 2009-2024 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.apache.ibatis.binding;
17  
18  import java.util.List;
19  import java.util.Map;
20  
21  import org.apache.ibatis.annotations.Arg;
22  import org.apache.ibatis.annotations.CacheNamespace;
23  import org.apache.ibatis.annotations.Case;
24  import org.apache.ibatis.annotations.ConstructorArgs;
25  import org.apache.ibatis.annotations.Many;
26  import org.apache.ibatis.annotations.MapKey;
27  import org.apache.ibatis.annotations.One;
28  import org.apache.ibatis.annotations.Param;
29  import org.apache.ibatis.annotations.Result;
30  import org.apache.ibatis.annotations.ResultType;
31  import org.apache.ibatis.annotations.Results;
32  import org.apache.ibatis.annotations.Select;
33  import org.apache.ibatis.annotations.SelectProvider;
34  import org.apache.ibatis.annotations.TypeDiscriminator;
35  import org.apache.ibatis.cursor.Cursor;
36  import org.apache.ibatis.domain.blog.Author;
37  import org.apache.ibatis.domain.blog.Blog;
38  import org.apache.ibatis.domain.blog.DraftPost;
39  import org.apache.ibatis.domain.blog.Post;
40  import org.apache.ibatis.mapping.FetchType;
41  import org.apache.ibatis.session.ResultHandler;
42  import org.apache.ibatis.session.RowBounds;
43  
44  @CacheNamespace(readWrite = false)
45  public interface BoundBlogMapper {
46  
47    // ======================================================
48  
49    Blog selectBlogWithPostsUsingSubSelect(int id);
50  
51    // ======================================================
52  
53    int selectRandom();
54  
55    // ======================================================
56  
57    @Select({ "SELECT * FROM blog" })
58    @MapKey("id")
59    Map<Integer, Blog> selectBlogsAsMapById();
60  
61    @Select({ "SELECT * FROM blog ORDER BY id" })
62    @MapKey("id")
63    Map<Integer, Blog> selectRangeBlogsAsMapById(RowBounds rowBounds);
64  
65    // ======================================================
66  
67    // @formatter:off
68    @Select({
69        "SELECT *",
70        "FROM blog"
71    })
72    // @formatter:on
73    List<Blog> selectBlogs();
74  
75    // @formatter:off
76    @Select({
77            "SELECT *",
78            "FROM blog",
79            "ORDER BY id"
80    })
81    // @formatter:on
82    @ResultType(Blog.class)
83    void collectRangeBlogs(ResultHandler<Object> blog, RowBounds rowBounds);
84  
85    // @formatter:off
86    @Select({
87            "SELECT *",
88            "FROM blog",
89            "ORDER BY id"
90    })
91    // @formatter:on
92    Cursor<Blog> openRangeBlogs(RowBounds rowBounds);
93  
94    // ======================================================
95  
96    List<Blog> selectBlogsFromXML();
97  
98    // ======================================================
99  
100   // @formatter:off
101   @Select({
102       "SELECT *",
103       "FROM blog"
104   })
105   // @formatter:on
106   List<Map<String, Object>> selectBlogsAsMaps();
107 
108   // ======================================================
109 
110   @SelectProvider(type = BoundBlogSql.class, method = "selectBlogsSql")
111   List<Blog> selectBlogsUsingProvider();
112 
113   // ======================================================
114 
115   @Select("SELECT * FROM post ORDER BY id")
116   // @formatter:off
117   @TypeDiscriminator(
118       column = "draft",
119       javaType = String.class,
120       cases = {@Case(value = "1", type = DraftPost.class)}
121   )
122   // @formatter:on
123   List<Post> selectPosts();
124 
125   // ======================================================
126 
127   @Select("SELECT * FROM post ORDER BY id")
128   @Results({ @Result(id = true, property = "id", column = "id") })
129   // @formatter:off
130   @TypeDiscriminator(
131       column = "draft",
132       javaType = int.class,
133       cases = {@Case(value = "1", type = DraftPost.class,
134           results = {@Result(id = true, property = "id", column = "id")})}
135   )
136   // @formatter:on
137   List<Post> selectPostsWithResultMap();
138 
139   // ======================================================
140 
141   // @formatter:off
142   @Select("SELECT * FROM "
143       + "blog WHERE id = #{id}")
144   // @formatter:on
145   Blog selectBlog(int id);
146 
147   // ======================================================
148 
149   // @formatter:off
150   @Select("SELECT * FROM blog "
151          + "WHERE id = #{id}")
152   @ConstructorArgs({
153       @Arg(column = "id", javaType = int.class, id = true),
154       @Arg(column = "title", javaType = String.class),
155       @Arg(column = "author_id", javaType = Author.class, select = "org.apache.ibatis.binding.BoundAuthorMapper.selectAuthor"),
156       @Arg(column = "id", javaType = List.class, select = "selectPostsForBlog")
157   })
158   // @formatter:on
159   Blog selectBlogUsingConstructor(int id);
160 
161   Blog selectBlogUsingConstructorWithResultMap(int i);
162 
163   Blog selectBlogUsingConstructorWithResultMapAndProperties(int i);
164 
165   Blog selectBlogUsingConstructorWithResultMapCollection(int i);
166 
167   Blog selectBlogByIdUsingConstructor(int id);
168 
169   // ======================================================
170 
171   // @formatter:off
172   @Select("SELECT * FROM blog "
173          + "WHERE id = #{id}")
174   // @formatter:on
175   Map<String, Object> selectBlogAsMap(Map<String, Object> params);
176 
177   // ======================================================
178 
179   // @formatter:off
180   @Select("SELECT * FROM post "
181          + "WHERE subject like #{query}")
182   // @formatter:on
183   List<Post> selectPostsLike(RowBounds bounds, String query);
184 
185   // ======================================================
186 
187   // @formatter:off
188   @Select("SELECT * FROM post "
189          + "WHERE subject like #{subjectQuery} and body like #{bodyQuery}")
190   // @formatter:on
191   List<Post> selectPostsLikeSubjectAndBody(RowBounds bounds, @Param("subjectQuery") String subjectQuery,
192       @Param("bodyQuery") String bodyQuery);
193 
194   // ======================================================
195 
196   // @formatter:off
197   @Select("SELECT * FROM post "
198          + "WHERE id = #{id}")
199   // @formatter:on
200   List<Post> selectPostsById(int id);
201 
202   // ======================================================
203 
204   // @formatter:off
205   @Select("SELECT * FROM blog "
206          + "WHERE id = #{id} AND title = #{nonExistentParam,jdbcType=VARCHAR}")
207   // @formatter:on
208   Blog selectBlogByNonExistentParam(@Param("id") int id);
209 
210   // @formatter:off
211   @Select("SELECT * FROM blog "
212          + "WHERE id = #{id} AND title = #{params.nonExistentParam,jdbcType=VARCHAR}")
213   // @formatter:on
214   Blog selectBlogByNonExistentNestedParam(@Param("id") int id, @Param("params") Map<String, Object> params);
215 
216   @Select("SELECT * FROM blog WHERE id = #{id}")
217   Blog selectBlogByNullParam(Integer id);
218 
219   // ======================================================
220 
221   // @formatter:off
222   @Select("SELECT * FROM blog "
223          + "WHERE id = #{0} AND title = #{1}")
224   // @formatter:on
225   Blog selectBlogByDefault30ParamNames(int id, String title);
226 
227   // @formatter:off
228   @Select("SELECT * FROM blog "
229          + "WHERE id = #{param1} AND title = #{param2}")
230   // @formatter:on
231   Blog selectBlogByDefault31ParamNames(int id, String title);
232 
233   // ======================================================
234 
235   // @formatter:off
236   @Select("SELECT * FROM blog "
237          + "WHERE ${column} = #{id} AND title = #{value}")
238   // @formatter:on
239   Blog selectBlogWithAParamNamedValue(@Param("column") String column, @Param("id") int id,
240       @Param("value") String title);
241 
242   // ======================================================
243 
244   // @formatter:off
245   @Select({
246       "SELECT *",
247         "FROM blog"
248   })
249   @Results({
250       @Result(property = "author", column = "author_id", one = @One(select = "org.apache.ibatis.binding.BoundAuthorMapper.selectAuthor")),
251       @Result(property = "posts", column = "id", many = @Many(select = "selectPostsById"))
252   })
253   // @formatter:on
254   List<Blog> selectBlogsWithAutorAndPosts();
255 
256   // @formatter:off
257   @Select({
258       "SELECT *",
259         "FROM blog"
260   })
261   @Results({
262       @Result(property = "author", column = "author_id", one = @One(select = "org.apache.ibatis.binding.BoundAuthorMapper.selectAuthor", fetchType = FetchType.EAGER)),
263       @Result(property = "posts", column = "id", many = @Many(select = "selectPostsById", fetchType = FetchType.EAGER))
264   })
265   // @formatter:on
266   List<Blog> selectBlogsWithAutorAndPostsEagerly();
267 
268 }