Skip to content

Commit 948339d

Browse files
committed
增加一个通用的selectAll方法
1 parent 189f2a4 commit 948339d

File tree

4 files changed

+74
-4
lines changed

4 files changed

+74
-4
lines changed

src/main/java/tk/mybatis/mapper/common/base/BaseSelectMapper.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@
2424

2525
package tk.mybatis.mapper.common.base;
2626

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.*;
3128

3229
/**
3330
* 通用Mapper接口,基础查询
@@ -38,6 +35,7 @@
3835
public interface BaseSelectMapper<T> extends
3936
SelectOneMapper<T>,
4037
SelectMapper<T>,
38+
SelectAllMapper<T>,
4139
SelectCountMapper<T>,
4240
SelectByPrimaryKeyMapper<T> {
4341

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}

src/main/java/tk/mybatis/mapper/provider/base/BaseSelectProvider.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,21 @@ public SqlNode selectCount(MappedStatement ms) {
147147
sqlNodes.add(new WhereSqlNode(ms.getConfiguration(), getAllIfColumnNode(entityClass)));
148148
return new MixedSqlNode(sqlNodes);
149149
}
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+
}
150167
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
}

0 commit comments

Comments
 (0)