View Javadoc
1   /*
2    *    Copyright 2009-2021 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    *       http://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.*;
22  import org.apache.ibatis.cursor.Cursor;
23  import org.apache.ibatis.domain.blog.Author;
24  import org.apache.ibatis.domain.blog.Blog;
25  import org.apache.ibatis.domain.blog.DraftPost;
26  import org.apache.ibatis.domain.blog.Post;
27  import org.apache.ibatis.mapping.FetchType;
28  import org.apache.ibatis.session.ResultHandler;
29  import org.apache.ibatis.session.RowBounds;
30  
31  @CacheNamespace(readWrite = false)
32  public interface BoundBlogMapper {
33  
34    //======================================================
35  
36    Blog selectBlogWithPostsUsingSubSelect(int id);
37  
38    //======================================================
39  
40    int selectRandom();
41  
42    //======================================================
43  
44    @Select({ "SELECT * FROM blog"})
45    @MapKey("id")
46    Map<Integer,Blog> selectBlogsAsMapById();
47  
48    @Select({ "SELECT * FROM blog ORDER BY id"})
49    @MapKey("id")
50    Map<Integer,Blog> selectRangeBlogsAsMapById(RowBounds rowBounds);
51  
52    //======================================================
53  
54    @Select({
55        "SELECT *",
56        "FROM blog"
57    })
58    List<Blog> selectBlogs();
59  
60    @Select({
61            "SELECT *",
62            "FROM blog",
63            "ORDER BY id"
64    })
65    @ResultType(Blog.class)
66    void collectRangeBlogs(ResultHandler<Object> blog, RowBounds rowBounds);
67  
68  
69    @Select({
70            "SELECT *",
71            "FROM blog",
72            "ORDER BY id"
73    })
74    Cursor<Blog> openRangeBlogs(RowBounds rowBounds);
75  
76    //======================================================
77  
78    List<Blog> selectBlogsFromXML();
79  
80    //======================================================
81  
82    @Select({
83        "SELECT *",
84        "FROM blog"
85    })
86    List<Map<String,Object>> selectBlogsAsMaps();
87  
88    //======================================================
89  
90    @SelectProvider(type = BoundBlogSql.class, method = "selectBlogsSql")
91    List<Blog> selectBlogsUsingProvider();
92  
93    //======================================================
94  
95    @Select("SELECT * FROM post ORDER BY id")
96    @TypeDiscriminator(
97        column = "draft",
98        javaType = String.class,
99        cases = {@Case(value = "1", type = DraftPost.class)}
100   )
101   List<Post> selectPosts();
102 
103   //======================================================
104 
105   @Select("SELECT * FROM post ORDER BY id")
106   @Results({
107       @Result(id = true, property = "id", column = "id")
108   })
109   @TypeDiscriminator(
110       column = "draft",
111       javaType = int.class,
112       cases = {@Case(value = "1", type = DraftPost.class,
113           results = {@Result(id = true, property = "id", column = "id")})}
114   )
115   List<Post> selectPostsWithResultMap();
116 
117   //======================================================
118 
119   @Select("SELECT * FROM " +
120       "blog WHERE id = #{id}")
121   Blog selectBlog(int id);
122 
123   //======================================================
124 
125   @Select("SELECT * FROM " +
126       "blog WHERE id = #{id}")
127   @ConstructorArgs({
128       @Arg(column = "id", javaType = int.class, id = true),
129       @Arg(column = "title", javaType = String.class),
130       @Arg(column = "author_id", javaType = Author.class, select = "org.apache.ibatis.binding.BoundAuthorMapper.selectAuthor"),
131       @Arg(column = "id", javaType = List.class, select = "selectPostsForBlog")
132   })
133   Blog selectBlogUsingConstructor(int id);
134 
135   Blog selectBlogUsingConstructorWithResultMap(int i);
136 
137   Blog selectBlogUsingConstructorWithResultMapAndProperties(int i);
138 
139   Blog selectBlogUsingConstructorWithResultMapCollection(int i);
140 
141   Blog selectBlogByIdUsingConstructor(int id);
142 
143   //======================================================
144 
145   @Select("SELECT * FROM " +
146       "blog WHERE id = #{id}")
147   Map<String,Object> selectBlogAsMap(Map<String,Object> params);
148 
149   //======================================================
150 
151   @Select("SELECT * FROM " +
152     "post WHERE subject like #{query}")
153   List<Post> selectPostsLike(RowBounds bounds, String query);
154 
155   //======================================================
156 
157   @Select("SELECT * FROM " +
158     "post WHERE subject like #{subjectQuery} and body like #{bodyQuery}")
159   List<Post> selectPostsLikeSubjectAndBody(RowBounds bounds,
160                              @Param("subjectQuery") String subjectQuery,
161                              @Param("bodyQuery") String bodyQuery);
162 
163   //======================================================
164 
165   @Select("SELECT * FROM " +
166     "post WHERE id = #{id}")
167   List<Post> selectPostsById(int id);
168 
169   //======================================================
170 
171   @Select("SELECT * FROM blog " +
172           "WHERE id = #{id} AND title = #{nonExistentParam,jdbcType=VARCHAR}")
173   Blog selectBlogByNonExistentParam(@Param("id") int id);
174 
175   @Select("SELECT * FROM blog " +
176           "WHERE id = #{id} AND title = #{params.nonExistentParam,jdbcType=VARCHAR}")
177   Blog selectBlogByNonExistentNestedParam(@Param("id") int id, @Param("params") Map<String, Object> params);
178 
179   @Select("SELECT * FROM blog WHERE id = #{id}")
180   Blog selectBlogByNullParam(Integer id);
181 
182   //======================================================
183 
184   @Select("SELECT * FROM blog " +
185       "WHERE id = #{0} AND title = #{1}")
186   Blog selectBlogByDefault30ParamNames(int id, String title);
187 
188   @Select("SELECT * FROM blog " +
189       "WHERE id = #{param1} AND title = #{param2}")
190   Blog selectBlogByDefault31ParamNames(int id, String title);
191 
192   //======================================================
193 
194   @Select("SELECT * FROM blog " +
195       "WHERE ${column} = #{id} AND title = #{value}")
196   Blog selectBlogWithAParamNamedValue(@Param("column") String column, @Param("id") int id, @Param("value") String title);
197 
198   //======================================================
199 
200   @Select({
201       "SELECT *",
202       "FROM blog"
203   })
204   @Results({
205       @Result(property = "author", column = "author_id", one = @One(select = "org.apache.ibatis.binding.BoundAuthorMapper.selectAuthor")),
206       @Result(property = "posts", column = "id", many = @Many(select = "selectPostsById"))
207   })
208   List<Blog> selectBlogsWithAutorAndPosts();
209 
210   @Select({
211       "SELECT *",
212       "FROM blog"
213   })
214   @Results({
215       @Result(property = "author", column = "author_id", one = @One(select = "org.apache.ibatis.binding.BoundAuthorMapper.selectAuthor", fetchType=FetchType.EAGER)),
216       @Result(property = "posts", column = "id", many = @Many(select = "selectPostsById", fetchType=FetchType.EAGER))
217   })
218   List<Blog> selectBlogsWithAutorAndPostsEagerly();
219 
220 }