View Javadoc
1   /*
2    *    Copyright 2009-2023 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   // @formatter:off
129   @Results({
130       @Result(id = true, property = "id", column = "id")
131   })
132   @TypeDiscriminator(
133       column = "draft",
134       javaType = int.class,
135       cases = {@Case(value = "1", type = DraftPost.class,
136           results = {@Result(id = true, property = "id", column = "id")})}
137   )
138   // @formatter:on
139   List<Post> selectPostsWithResultMap();
140 
141   // ======================================================
142 
143   // @formatter:off
144   @Select("SELECT * FROM "
145       + "blog WHERE id = #{id}")
146   // @formatter:on
147   Blog selectBlog(int id);
148 
149   // ======================================================
150 
151   // @formatter:off
152   @Select("SELECT * FROM "
153       + "blog WHERE id = #{id}")
154   @ConstructorArgs({
155       @Arg(column = "id", javaType = int.class, id = true),
156       @Arg(column = "title", javaType = String.class),
157       @Arg(column = "author_id", javaType = Author.class, select = "org.apache.ibatis.binding.BoundAuthorMapper.selectAuthor"),
158       @Arg(column = "id", javaType = List.class, select = "selectPostsForBlog")
159   })
160   // @formatter:on
161   Blog selectBlogUsingConstructor(int id);
162 
163   Blog selectBlogUsingConstructorWithResultMap(int i);
164 
165   Blog selectBlogUsingConstructorWithResultMapAndProperties(int i);
166 
167   Blog selectBlogUsingConstructorWithResultMapCollection(int i);
168 
169   Blog selectBlogByIdUsingConstructor(int id);
170 
171   // ======================================================
172 
173   // @formatter:off
174   @Select("SELECT * FROM "
175       + "blog WHERE id = #{id}")
176   // @formatter:on
177   Map<String, Object> selectBlogAsMap(Map<String, Object> params);
178 
179   // ======================================================
180 
181   // @formatter:off
182   @Select("SELECT * FROM "
183     + "post WHERE subject like #{query}")
184   // @formatter:on
185   List<Post> selectPostsLike(RowBounds bounds, String query);
186 
187   // ======================================================
188 
189   // @formatter:off
190   @Select("SELECT * FROM "
191     + "post WHERE subject like #{subjectQuery} and body like #{bodyQuery}")
192   // @formatter:on
193   List<Post> selectPostsLikeSubjectAndBody(RowBounds bounds, @Param("subjectQuery") String subjectQuery,
194       @Param("bodyQuery") String bodyQuery);
195 
196   // ======================================================
197 
198   // @formatter:off
199   @Select("SELECT * FROM "
200     + "post WHERE id = #{id}")
201   // @formatter:on
202   List<Post> selectPostsById(int id);
203 
204   // ======================================================
205 
206   // @formatter:off
207   @Select("SELECT * FROM blog "
208           + "WHERE id = #{id} AND title = #{nonExistentParam,jdbcType=VARCHAR}")
209   // @formatter:on
210   Blog selectBlogByNonExistentParam(@Param("id") int id);
211 
212   // @formatter:off
213   @Select("SELECT * FROM blog "
214           + "WHERE id = #{id} AND title = #{params.nonExistentParam,jdbcType=VARCHAR}")
215   // @formatter:on
216   Blog selectBlogByNonExistentNestedParam(@Param("id") int id, @Param("params") Map<String, Object> params);
217 
218   @Select("SELECT * FROM blog WHERE id = #{id}")
219   Blog selectBlogByNullParam(Integer id);
220 
221   // ======================================================
222 
223   // @formatter:off
224   @Select("SELECT * FROM blog "
225       + "WHERE id = #{0} AND title = #{1}")
226   // @formatter:on
227   Blog selectBlogByDefault30ParamNames(int id, String title);
228 
229   // @formatter:off
230   @Select("SELECT * FROM blog "
231       + "WHERE id = #{param1} AND title = #{param2}")
232   // @formatter:on
233   Blog selectBlogByDefault31ParamNames(int id, String title);
234 
235   // ======================================================
236 
237   // @formatter:off
238   @Select("SELECT * FROM blog "
239       + "WHERE ${column} = #{id} AND title = #{value}")
240   // @formatter:on
241   Blog selectBlogWithAParamNamedValue(@Param("column") String column, @Param("id") int id,
242       @Param("value") String title);
243 
244   // ======================================================
245 
246   // @formatter:off
247   @Select({
248       "SELECT *",
249       "FROM blog"
250   })
251   @Results({
252       @Result(property = "author", column = "author_id", one = @One(select = "org.apache.ibatis.binding.BoundAuthorMapper.selectAuthor")),
253       @Result(property = "posts", column = "id", many = @Many(select = "selectPostsById"))
254   })
255   // @formatter:on
256   List<Blog> selectBlogsWithAutorAndPosts();
257 
258   // @formatter:off
259   @Select({
260       "SELECT *",
261       "FROM blog"
262   })
263   @Results({
264       @Result(property = "author", column = "author_id", one = @One(select = "org.apache.ibatis.binding.BoundAuthorMapper.selectAuthor", fetchType = FetchType.EAGER)),
265       @Result(property = "posts", column = "id", many = @Many(select = "selectPostsById", fetchType = FetchType.EAGER))
266   })
267   // @formatter:on
268   List<Blog> selectBlogsWithAutorAndPostsEagerly();
269 
270 }