File tree Expand file tree Collapse file tree 4 files changed +74
-4
lines changed
main/java/tk/mybatis/mapper
test/java/tk/mybatis/mapper/test/country Expand file tree Collapse file tree 4 files changed +74
-4
lines changed Original file line number Diff line number Diff line change 24
24
25
25
package tk .mybatis .mapper .common .base ;
26
26
27
- import tk .mybatis .mapper .common .base .select .SelectByPrimaryKeyMapper ;
28
- import tk .mybatis .mapper .common .base .select .SelectCountMapper ;
29
- import tk .mybatis .mapper .common .base .select .SelectMapper ;
30
- import tk .mybatis .mapper .common .base .select .SelectOneMapper ;
27
+ import tk .mybatis .mapper .common .base .select .*;
31
28
32
29
/**
33
30
* 通用Mapper接口,基础查询
38
35
public interface BaseSelectMapper <T > extends
39
36
SelectOneMapper <T >,
40
37
SelectMapper <T >,
38
+ SelectAllMapper <T >,
41
39
SelectCountMapper <T >,
42
40
SelectByPrimaryKeyMapper <T > {
43
41
Original file line number Diff line number Diff line change
1
+ package tk .mybatis .mapper .common .base .select ;
2
+
3
+ import org .apache .ibatis .annotations .SelectProvider ;
4
+ import tk .mybatis .mapper .provider .base .BaseSelectProvider ;
5
+
6
+ import java .util .List ;
7
+
8
+ /**
9
+ * @author liuzh
10
+ */
11
+ public interface SelectAllMapper <T > {
12
+
13
+ /**
14
+ * 查询全部结果
15
+ *
16
+ * @return
17
+ */
18
+ @ SelectProvider (type = BaseSelectProvider .class , method = "dynamicSQL" )
19
+ List <T > selectAll ();
20
+ }
Original file line number Diff line number Diff line change @@ -147,4 +147,21 @@ public SqlNode selectCount(MappedStatement ms) {
147
147
sqlNodes .add (new WhereSqlNode (ms .getConfiguration (), getAllIfColumnNode (entityClass )));
148
148
return new MixedSqlNode (sqlNodes );
149
149
}
150
+
151
+ /**
152
+ * 查询全部结果
153
+ *
154
+ * @param ms
155
+ * @return
156
+ */
157
+ public String selectAll (MappedStatement ms ) {
158
+ final Class <?> entityClass = getSelectReturnType (ms );
159
+ //修改返回值类型为实体类型
160
+ setResultType (ms , entityClass );
161
+ //开始拼sql
162
+ StringBuilder sql = new StringBuilder ();
163
+ sql .append ("select " ).append (EntityHelper .getSelectColumns (entityClass )).append (" from " );
164
+ sql .append (tableName (entityClass ));
165
+ return sql .toString ();
166
+ }
150
167
}
Original file line number Diff line number Diff line change
1
+ package tk .mybatis .mapper .test .country ;
2
+
3
+ import org .apache .ibatis .session .SqlSession ;
4
+ import org .junit .Assert ;
5
+ import org .junit .Test ;
6
+ import tk .mybatis .mapper .mapper .CountryMapper ;
7
+ import tk .mybatis .mapper .mapper .MybatisHelper ;
8
+ import tk .mybatis .mapper .model .Country ;
9
+
10
+ import java .util .List ;
11
+
12
+ /**
13
+ * 通过实体类属性进行查询
14
+ *
15
+ * @author liuzh
16
+ */
17
+ public class TestSelectAll {
18
+
19
+ /**
20
+ * 查询全部
21
+ */
22
+ @ Test
23
+ public void testDynamicSelectPage () {
24
+ SqlSession sqlSession = MybatisHelper .getSqlSession ();
25
+ try {
26
+ CountryMapper mapper = sqlSession .getMapper (CountryMapper .class );
27
+
28
+ List <Country > countryList = mapper .selectAll ();
29
+ //查询总数
30
+ Assert .assertEquals (183 , countryList .size ());
31
+ } finally {
32
+ sqlSession .close ();
33
+ }
34
+ }
35
+ }
You can’t perform that action at this time.
0 commit comments