Skip to content

Commit de9eaa7

Browse files
committed
Mapper接口增加selectOne方法。
1 parent 494154c commit de9eaa7

File tree

4 files changed

+133
-9
lines changed

4 files changed

+133
-9
lines changed

src/main/java/com/github/abel533/mapper/Mapper.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
*/
4141
public interface Mapper<T> {
4242

43+
@SelectProvider(type = MapperProvider.class, method = "dynamicSQL")
44+
T selectOne(T record);
45+
4346
@SelectProvider(type = MapperProvider.class, method = "dynamicSQL")
4447
List<T> select(T record);
4548

src/main/java/com/github/abel533/mapper/MapperProvider.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,27 @@ public MapperProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
4848
super(mapperClass, mapperHelper);
4949
}
5050

51+
/**
52+
* 查询
53+
*
54+
* @param ms
55+
* @return
56+
*/
57+
public SqlNode selectOne(MappedStatement ms) {
58+
Class<?> entityClass = getSelectReturnType(ms);
59+
//修改返回值类型为实体类型
60+
setResultType(ms, entityClass);
61+
List<SqlNode> sqlNodes = new ArrayList<SqlNode>();
62+
//静态的sql部分:select column ... from table
63+
sqlNodes.add(new StaticTextSqlNode("SELECT "
64+
+ EntityHelper.getSelectColumns(entityClass)
65+
+ " FROM "
66+
+ tableName(entityClass)));
67+
//将if添加到<where>
68+
sqlNodes.add(new WhereSqlNode(ms.getConfiguration(), getAllIfColumnNode(entityClass)));
69+
return new MixedSqlNode(sqlNodes);
70+
}
71+
5172
/**
5273
* 查询
5374
*
@@ -66,15 +87,9 @@ public SqlNode select(MappedStatement ms) {
6687
+ tableName(entityClass)));
6788
//将if添加到<where>
6889
sqlNodes.add(new WhereSqlNode(ms.getConfiguration(), getAllIfColumnNode(entityClass)));
69-
StringBuilder orderBy = new StringBuilder();
70-
for (EntityHelper.EntityColumn column : EntityHelper.getColumns(entityClass)) {
71-
if (column.getOrderBy() != null) {
72-
orderBy.append(column.getColumn()).append(" ").append(column.getOrderBy()).append(",");
73-
}
74-
}
75-
if (orderBy.length() > 0) {
76-
orderBy.insert(0, "order by");
77-
sqlNodes.add(new StaticTextSqlNode(orderBy.substring(0, orderBy.length() - 1)));
90+
String orderByClause = EntityHelper.getOrderByClause(entityClass);
91+
if (orderByClause.length() > 0) {
92+
sqlNodes.add(new StaticTextSqlNode(orderByClause));
7893
}
7994
return new MixedSqlNode(sqlNodes);
8095
}

src/main/java/com/github/abel533/mapperhelper/EntityHelper.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,28 @@ public static EntityTable getEntityTable(Class<?> entityClass) {
242242
return entityTable;
243243
}
244244

245+
/**
246+
* 获取默认的orderby语句
247+
*
248+
* @param entityClass
249+
* @return
250+
*/
251+
public static String getOrderByClause(Class<?> entityClass){
252+
StringBuilder orderBy = new StringBuilder();
253+
for (EntityHelper.EntityColumn column : getColumns(entityClass)) {
254+
if (column.getOrderBy() != null) {
255+
if(orderBy.length()!=0){
256+
orderBy.append(",");
257+
}
258+
orderBy.append(column.getColumn()).append(" ").append(column.getOrderBy());
259+
}
260+
}
261+
if (orderBy.length() > 0) {
262+
orderBy.insert(0, "ORDER BY ");
263+
}
264+
return orderBy.toString();
265+
}
266+
245267
/**
246268
* 获取全部列
247269
*
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.github.abel533.test.country;
2+
3+
import com.github.abel533.mapper.CountryMapper;
4+
import com.github.abel533.mapper.MybatisHelper;
5+
import com.github.abel533.model.Country;
6+
import org.apache.ibatis.exceptions.TooManyResultsException;
7+
import org.apache.ibatis.session.SqlSession;
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
11+
/**
12+
* 通过实体类属性进行查询
13+
*
14+
* @author liuzh
15+
*/
16+
public class TestSelectOne {
17+
18+
/**
19+
* 查询全部
20+
*/
21+
@Test(expected = TooManyResultsException.class)
22+
public void testDynamicSelectAll() {
23+
SqlSession sqlSession = MybatisHelper.getSqlSession();
24+
try {
25+
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
26+
Country country = mapper.selectOne(new Country());
27+
} finally {
28+
sqlSession.close();
29+
}
30+
}
31+
32+
/**
33+
* 入参为null时查询全部
34+
*/
35+
@Test(expected = TooManyResultsException.class)
36+
public void testDynamicSelectAllByNull() {
37+
SqlSession sqlSession = MybatisHelper.getSqlSession();
38+
try {
39+
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
40+
mapper.selectOne(null);
41+
} finally {
42+
sqlSession.close();
43+
}
44+
}
45+
46+
/**
47+
* 根据查询条件进行查询
48+
*/
49+
@Test
50+
public void testDynamicSelect() {
51+
SqlSession sqlSession = MybatisHelper.getSqlSession();
52+
try {
53+
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
54+
Country country = new Country();
55+
country.setCountrycode("CN");
56+
Country result = mapper.selectOne(country);
57+
58+
Assert.assertEquals(true, result.getId() == 35);
59+
Assert.assertEquals("China", result.getCountryname());
60+
} finally {
61+
sqlSession.close();
62+
}
63+
}
64+
65+
/**
66+
* 查询不存在的结果
67+
*/
68+
@Test
69+
public void testDynamicSelectZero() {
70+
SqlSession sqlSession = MybatisHelper.getSqlSession();
71+
try {
72+
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
73+
Country country = new Country();
74+
country.setCountrycode("CN");
75+
country.setCountryname("天朝");//实际上是 China
76+
Country result = mapper.selectOne(country);
77+
78+
Assert.assertNull(result);
79+
} finally {
80+
sqlSession.close();
81+
}
82+
}
83+
84+
}

0 commit comments

Comments
 (0)