Skip to content

Commit 53a43a4

Browse files
committed
Merge pull request !9 from ptma/master
2 parents f415c9a + 2d7dc9d commit 53a43a4

File tree

5 files changed

+145
-1
lines changed

5 files changed

+145
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public interface BaseSelectMapper<T> extends
3737
SelectMapper<T>,
3838
SelectAllMapper<T>,
3939
SelectCountMapper<T>,
40-
SelectByPrimaryKeyMapper<T> {
40+
SelectByPrimaryKeyMapper<T>,
41+
ExistsWithPrimaryKeyMapper<T> {
4142

4243
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014-2016 abel533@gmail.com
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 tk.mybatis.mapper.common.base.select;
26+
27+
import org.apache.ibatis.annotations.SelectProvider;
28+
import tk.mybatis.mapper.provider.base.BaseSelectProvider;
29+
30+
/**
31+
* 通用Mapper接口,查询
32+
*
33+
* @param <T> 不能为空
34+
* @author liuzh
35+
*/
36+
public interface ExistsWithPrimaryKeyMapper<T> {
37+
38+
/**
39+
* 根据主键字段查询总数,方法参数必须包含完整的主键属性,查询条件使用等号
40+
*
41+
* @param key
42+
* @return
43+
*/
44+
@SelectProvider(type = BaseSelectProvider.class, method = "dynamicSQL")
45+
boolean existsWithPrimaryKey(Object key);
46+
47+
}

src/main/java/tk/mybatis/mapper/mapperhelper/SqlHelper.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,25 @@ public static String selectCount(Class<?> entityClass) {
269269
return sql.toString();
270270
}
271271

272+
/**
273+
* select case when count(x) > 0 then 1 else 0 end
274+
*
275+
* @param entityClass
276+
* @return
277+
*/
278+
public static String selectCountExists(Class<?> entityClass) {
279+
StringBuilder sql = new StringBuilder();
280+
sql.append("SELECT CASE WHEN ");
281+
Set<EntityColumn> pkColumns = EntityHelper.getPKColumns(entityClass);
282+
if (pkColumns.size() == 1) {
283+
sql.append("COUNT(").append(pkColumns.iterator().next().getColumn()).append(") ");
284+
} else {
285+
sql.append("COUNT(*) ");
286+
}
287+
sql.append(" > 0 THEN 1 ELSE 0 END AS result ");
288+
return sql.toString();
289+
}
290+
272291
/**
273292
* from tableName - 动态表名
274293
*

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,21 @@ public String selectCount(MappedStatement ms) {
116116
return sql.toString();
117117
}
118118

119+
/**
120+
* 根据主键查询总数
121+
*
122+
* @param ms
123+
* @return
124+
*/
125+
public String existsWithPrimaryKey(MappedStatement ms) {
126+
Class<?> entityClass = getEntityClass(ms);
127+
StringBuilder sql = new StringBuilder();
128+
sql.append(SqlHelper.selectCountExists(entityClass));
129+
sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
130+
sql.append(SqlHelper.wherePKColumns(entityClass));
131+
return sql.toString();
132+
}
133+
119134
/**
120135
* 查询全部结果
121136
*
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014-2016 abel533@gmail.com
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 tk.mybatis.mapper.test.country;
26+
27+
import org.apache.ibatis.session.SqlSession;
28+
import org.junit.Assert;
29+
import org.junit.Test;
30+
import tk.mybatis.mapper.mapper.CountryMapper;
31+
import tk.mybatis.mapper.mapper.MybatisHelper;
32+
import tk.mybatis.mapper.model.Country;
33+
34+
import java.util.HashMap;
35+
import java.util.Map;
36+
37+
/**
38+
* 通过主键查询
39+
*
40+
* @author liuzh
41+
*/
42+
public class TestExistsWithPrimaryKey {
43+
44+
/**
45+
* 根据PK进行查询
46+
*/
47+
@Test
48+
public void testDynamicExistsWithPrimaryKey() {
49+
SqlSession sqlSession = MybatisHelper.getSqlSession();
50+
try {
51+
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
52+
Country country = new Country();
53+
country.setId(35);
54+
Assert.assertEquals(true, mapper.existsWithPrimaryKey(country));
55+
56+
country.setId(0);
57+
Assert.assertEquals(false, mapper.existsWithPrimaryKey(country));
58+
} finally {
59+
sqlSession.close();
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)