Skip to content

Commit 7477ae6

Browse files
committed
EntityMapper增加selectOne方法
1 parent a65d90c commit 7477ae6

File tree

4 files changed

+146
-8
lines changed

4 files changed

+146
-8
lines changed

src/main/java/com/github/abel533/entity/CommonProvider.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,37 @@
3737
*/
3838
public class CommonProvider extends BaseProvider {
3939

40+
/**
41+
* 查询,入参可以是Entity.class或new Entity()
42+
*
43+
* @param params
44+
* @return
45+
*/
46+
public String selectOne(final Map<String, Object> params) {
47+
return new SQL() {{
48+
Object entity = getEntity(params);
49+
Class<?> entityClass = getEntityClass(params);
50+
EntityHelper.EntityTable entityTable = EntityHelper.getEntityTable(entityClass);
51+
SELECT(EntityHelper.getAllColumns(entityClass));
52+
FROM(entityTable.getName());
53+
if (entity != null) {
54+
final MetaObject metaObject = MapperTemplate.forObject(entity);
55+
for (EntityHelper.EntityColumn column : entityTable.getEntityClassColumns()) {
56+
Object value = metaObject.getValue(column.getProperty());
57+
if (value == null) {
58+
continue;
59+
} else if (column.getJavaType().equals(String.class)) {
60+
if (isNotEmpty((String) value)) {
61+
WHERE(column.getColumn() + "=#{record." + column.getProperty() + "}");
62+
}
63+
} else {
64+
WHERE(column.getColumn() + "=#{record." + column.getProperty() + "}");
65+
}
66+
}
67+
}
68+
}}.toString();
69+
}
70+
4071
/**
4172
* 查询,入参可以是Entity.class或new Entity()
4273
*

src/main/java/com/github/abel533/entity/EntityMapper.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ public EntityMapper(CommonMapper commonMapper) {
4545
this.commonMapper = commonMapper;
4646
}
4747

48+
/**
49+
* 根据参数进行查询,查询结果最多只能有一个
50+
* <br>查询条件为属性String类型不为空,其他类型!=null时
51+
* <br>where property = ? and property2 = ? 条件
52+
*
53+
* @param record
54+
* @param <T>
55+
* @return
56+
*/
57+
public <T> T selectOne(T record) {
58+
if (record == null) {
59+
throw new NullPointerException("实体或者主键参数不能为空!");
60+
}
61+
return (T) EntityHelper.map2Bean(commonMapper.selectOne(record), record.getClass());
62+
}
63+
4864
/**
4965
* 根据参数进行查询,record可以是Class<?>类型
5066
* <br>查询条件为属性String类型不为空,其他类型!=null时

src/main/java/com/github/abel533/entity/mapper/CommonMapper.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@
3636
* @author liuzh
3737
*/
3838
public interface CommonMapper {
39+
/**
40+
* 根据参数进行查询,查询结果最多只能有一个
41+
* <br>查询条件为属性String类型不为空,其他类型!=null时
42+
* <br>where property = ? and property2 = ? 条件
43+
*
44+
* @param record
45+
* @param <T>
46+
* @return
47+
*/
48+
@SelectProvider(type = CommonProvider.class, method = "selectOne")
49+
<T> Map<String, Object> selectOne(@Param("record") T record);
50+
3951
/**
4052
* 根据参数进行查询,record可以是Class<?>类型
4153
* <br>查询条件为属性String类型不为空,其他类型!=null时
@@ -45,9 +57,8 @@ public interface CommonMapper {
4557
* @param <T>
4658
* @return
4759
*/
48-
@Options(flushCache = true)
4960
@SelectProvider(type = CommonProvider.class, method = "select")
50-
<T> List<Map<String,Object>> select(@Param("record") T record);
61+
<T> List<Map<String, Object>> select(@Param("record") T record);
5162

5263
/**
5364
* 根据参数进行查询总数,record可以是Class<?>类型
@@ -58,7 +69,6 @@ public interface CommonMapper {
5869
* @param <T>
5970
* @return
6071
*/
61-
@Options(flushCache = true)
6272
@SelectProvider(type = CommonProvider.class, method = "count")
6373
<T> int count(@Param("record") T record);
6474

@@ -70,9 +80,8 @@ public interface CommonMapper {
7080
* @param <T>
7181
* @return
7282
*/
73-
@Options(flushCache = true)
7483
@SelectProvider(type = CommonProvider.class, method = "selectByPrimaryKey")
75-
<T> Map<String,Object> selectByPrimaryKey(@Param("entityClass") Class<T> entityClass, @Param("key") Object key);
84+
<T> Map<String, Object> selectByPrimaryKey(@Param("entityClass") Class<T> entityClass, @Param("key") Object key);
7685

7786
/**
7887
* 插入数据库,主键字段没有值的时候不会出现在sql中
@@ -146,7 +155,6 @@ public interface CommonMapper {
146155
* @param <T>
147156
* @return
148157
*/
149-
@Options(flushCache = true)
150158
@SelectProvider(type = CommonProvider.class, method = "countByExample")
151159
<T> int countByExample(@Param("entityClass") Class<T> entityClass, @Param("example") Object example);
152160

@@ -169,9 +177,8 @@ public interface CommonMapper {
169177
* @param <T>
170178
* @return
171179
*/
172-
@Options(flushCache = true)
173180
@SelectProvider(type = CommonProvider.class, method = "selectByExample")
174-
<T> List<Map<String,Object>> selectByExample(@Param("entityClass") Class<T> entityClass, @Param("example") Object example);
181+
<T> List<Map<String, Object>> selectByExample(@Param("entityClass") Class<T> entityClass, @Param("example") Object example);
175182

176183
/**
177184
* 通过Example进行更新非空字段
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014 [email protected]
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package com.github.abel533.entity.test;
26+
27+
import com.github.abel533.entity.EntityMapper;
28+
import com.github.abel533.entity.mapper.CommonMapper;
29+
import com.github.abel533.entity.model.Country;
30+
import com.github.abel533.mapper.MybatisHelper;
31+
import org.apache.ibatis.exceptions.TooManyResultsException;
32+
import org.apache.ibatis.session.SqlSession;
33+
import org.junit.Assert;
34+
import org.junit.Test;
35+
36+
/**
37+
* 测试查询
38+
*
39+
* @author liuzh
40+
*/
41+
public class TestSelectOne {
42+
43+
@Test(expected = TooManyResultsException.class)
44+
public void testSelectAllByNew() {
45+
SqlSession sqlSession = MybatisHelper.getSqlSession();
46+
try {
47+
CommonMapper commonMapper = sqlSession.getMapper(CommonMapper.class);
48+
EntityMapper entityMapper = new EntityMapper(commonMapper);
49+
entityMapper.selectOne(new Country());
50+
} finally {
51+
sqlSession.close();
52+
}
53+
}
54+
55+
@Test
56+
public void testSelectOne() {
57+
SqlSession sqlSession = MybatisHelper.getSqlSession();
58+
try {
59+
CommonMapper commonMapper = sqlSession.getMapper(CommonMapper.class);
60+
EntityMapper entityMapper = new EntityMapper(commonMapper);
61+
62+
Country country = new Country();
63+
country.setCountrycode("CN");
64+
Country result = entityMapper.selectOne(country);
65+
66+
Assert.assertEquals("China", result.getCountryname());
67+
} finally {
68+
sqlSession.close();
69+
}
70+
}
71+
72+
@Test(expected = Exception.class)
73+
public void testSelectAllByNull() {
74+
SqlSession sqlSession = MybatisHelper.getSqlSession();
75+
try {
76+
CommonMapper commonMapper = sqlSession.getMapper(CommonMapper.class);
77+
EntityMapper entityMapper = new EntityMapper(commonMapper);
78+
79+
entityMapper.selectOne(null);
80+
} finally {
81+
sqlSession.close();
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)