Skip to content

Commit 561dd13

Browse files
committed
增加selectByIds和deleteByIds,用法见通用Mapper接口大全
1 parent da8abdb commit 561dd13

File tree

8 files changed

+228
-1
lines changed

8 files changed

+228
-1
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package tk.mybatis.mapper.common;
2+
3+
import tk.mybatis.mapper.common.ids.DeleteByIdsMapper;
4+
import tk.mybatis.mapper.common.ids.SelectByIdsMapper;
5+
6+
/**
7+
* 通用Mapper接口,根据ids操作
8+
*
9+
* @param <T> 不能为空
10+
* @author liuzh
11+
*/
12+
public interface IdsMapper<T> extends SelectByIdsMapper<T>, DeleteByIdsMapper<T> {
13+
14+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package tk.mybatis.mapper.common.ids;
2+
3+
import org.apache.ibatis.annotations.DeleteProvider;
4+
import org.apache.ibatis.annotations.SelectProvider;
5+
import tk.mybatis.mapper.provider.IdsProvider;
6+
7+
import java.util.List;
8+
9+
/**
10+
* 通用Mapper接口,根据ids删除
11+
*
12+
* @param <T> 不能为空
13+
* @author liuzh
14+
*/
15+
public interface DeleteByIdsMapper<T> {
16+
17+
/**
18+
* 根据主键字符串进行删除,类中只有存在一个带有@Id注解的字段
19+
*
20+
* @param ids 如 "1,2,3,4"
21+
* @return
22+
*/
23+
@DeleteProvider(type = IdsProvider.class, method = "dynamicSQL")
24+
int deleteByIds(String ids);
25+
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package tk.mybatis.mapper.common.ids;
2+
3+
import org.apache.ibatis.annotations.SelectProvider;
4+
import tk.mybatis.mapper.provider.IdsProvider;
5+
6+
import java.util.List;
7+
8+
/**
9+
* 通用Mapper接口,根据ids查询
10+
*
11+
* @param <T> 不能为空
12+
* @author liuzh
13+
*/
14+
public interface SelectByIdsMapper<T> {
15+
16+
/**
17+
* 根据主键字符串进行查询,类中只有存在一个带有@Id注解的字段
18+
*
19+
* @param ids 如 "1,2,3,4"
20+
* @return
21+
*/
22+
@SelectProvider(type = IdsProvider.class, method = "dynamicSQL")
23+
List<T> selectByIds(String ids);
24+
25+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package tk.mybatis.mapper.provider;
2+
3+
import org.apache.ibatis.mapping.MappedStatement;
4+
import tk.mybatis.mapper.entity.EntityColumn;
5+
import tk.mybatis.mapper.mapperhelper.EntityHelper;
6+
import tk.mybatis.mapper.mapperhelper.MapperHelper;
7+
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
8+
import tk.mybatis.mapper.mapperhelper.SqlHelper;
9+
10+
import java.util.Set;
11+
12+
/**
13+
* 通过 ids 字符串的各种操作
14+
* <p/>
15+
* ids 如 "1,2,3"
16+
*
17+
* @author liuzh
18+
*/
19+
public class IdsProvider extends MapperTemplate {
20+
21+
public IdsProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
22+
super(mapperClass, mapperHelper);
23+
}
24+
25+
/**
26+
* 根据主键字符串进行删除,类中只有存在一个带有@Id注解的字段
27+
*
28+
* @param ms
29+
* @return
30+
*/
31+
public String deleteByIds(MappedStatement ms) {
32+
final Class<?> entityClass = getEntityClass(ms);
33+
StringBuilder sql = new StringBuilder();
34+
sql.append(SqlHelper.deleteFromTable(entityClass, tableName(entityClass)));
35+
Set<EntityColumn> columnList = EntityHelper.getPKColumns(entityClass);
36+
if (columnList.size() == 1) {
37+
EntityColumn column = columnList.iterator().next();
38+
sql.append(" where ");
39+
sql.append(column.getColumn());
40+
sql.append(" in (${_parameter})");
41+
} else {
42+
throw new RuntimeException("继承 deleteByIds 方法的实体类[" + entityClass.getCanonicalName() + "]中必须只有一个带有 @Id 注解的字段");
43+
}
44+
return sql.toString();
45+
}
46+
47+
/**
48+
* 根据主键字符串进行查询,类中只有存在一个带有@Id注解的字段
49+
*
50+
* @param ms
51+
* @return
52+
*/
53+
public String selectByIds(MappedStatement ms) {
54+
final Class<?> entityClass = getEntityClass(ms);
55+
//将返回值修改为实体类型
56+
setResultType(ms, entityClass);
57+
StringBuilder sql = new StringBuilder();
58+
sql.append(SqlHelper.selectAllColumns(entityClass));
59+
sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
60+
Set<EntityColumn> columnList = EntityHelper.getPKColumns(entityClass);
61+
if (columnList.size() == 1) {
62+
EntityColumn column = columnList.iterator().next();
63+
sql.append(" where ");
64+
sql.append(column.getColumn());
65+
sql.append(" in (${_parameter})");
66+
} else {
67+
throw new RuntimeException("继承 selectByIds 方法的实体类[" + entityClass.getCanonicalName() + "]中必须只有一个带有 @Id 注解的字段");
68+
}
69+
return sql.toString();
70+
}
71+
}

src/test/java/tk/mybatis/mapper/mapper/CountryMapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
package tk.mybatis.mapper.mapper;
2626

27+
import tk.mybatis.mapper.common.IdsMapper;
2728
import tk.mybatis.mapper.common.Mapper;
2829
import tk.mybatis.mapper.common.MySqlMapper;
2930
import tk.mybatis.mapper.hsqldb.HsqldbMapper;
@@ -32,5 +33,5 @@
3233
/**
3334
* Created by liuzh on 2014/11/19.
3435
*/
35-
public interface CountryMapper extends Mapper<Country>, HsqldbMapper<Country>, MySqlMapper<Country> {
36+
public interface CountryMapper extends Mapper<Country>, HsqldbMapper<Country>, MySqlMapper<Country>, IdsMapper<Country> {
3637
}

src/test/java/tk/mybatis/mapper/mapper/MybatisHelper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.apache.ibatis.session.SqlSession;
3030
import org.apache.ibatis.session.SqlSessionFactory;
3131
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
32+
import tk.mybatis.mapper.common.IdsMapper;
3233
import tk.mybatis.mapper.common.Mapper;
3334
import tk.mybatis.mapper.common.MySqlMapper;
3435
import tk.mybatis.mapper.common.SqlServerMapper;
@@ -89,6 +90,7 @@ public class MybatisHelper {
8990
mapperHelper.registerMapper(HsqldbMapper.class);
9091
mapperHelper.registerMapper(MySqlMapper.class);
9192
mapperHelper.registerMapper(SqlServerMapper.class);
93+
mapperHelper.registerMapper(IdsMapper.class);
9294
//配置完成后,执行下面的操作
9395
mapperHelper.processConfiguration(session.getConfiguration());
9496
//OK - mapperHelper的任务已经完成,可以不管了
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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.ids;
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.List;
35+
36+
/**
37+
* 通过 ids 进行操作
38+
*
39+
* @author liuzh
40+
*/
41+
public class TestIds {
42+
43+
@Test
44+
public void testSelectByIds() {
45+
SqlSession sqlSession = MybatisHelper.getSqlSession();
46+
try {
47+
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
48+
List<Country> countryList = mapper.selectByIds("1,2,3");
49+
//查询总数
50+
Assert.assertEquals(3, countryList.size());
51+
} finally {
52+
sqlSession.close();
53+
}
54+
}
55+
56+
@Test
57+
public void testDeleteByIds() {
58+
SqlSession sqlSession = MybatisHelper.getSqlSession();
59+
try {
60+
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
61+
int count = mapper.deleteByIds("1,2,3");
62+
//查询总数
63+
Assert.assertEquals(3, count);
64+
Assert.assertEquals(180, mapper.selectCount(null));
65+
} finally {
66+
sqlSession.rollback();
67+
sqlSession.close();
68+
}
69+
}
70+
71+
}

wiki/mapper3/5.Mappers.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,23 @@ Condition方法和Example方法作用完全一样,只是为了避免Example带
202202
接口:`SqlServerMapper`<br>
203203
说明:这是上面两个接口的组合接口。<br><br>
204204

205+
##Ids接口
206+
207+
通过操作ids字符串进行操作,ids 如 "1,2,3" 这种形式的字符串,这个方法要求实体类中有且只有一个带有`@Id`注解的字段,否则会抛出异常。
208+
209+
接口:`SelectByIdsMapper`<br>
210+
方法:`List<T> selectByIds(String ids)`<br>
211+
说明:根据主键字符串进行查询,类中只有存在一个带有@Id注解的字段<br><br>
212+
213+
接口:`DeleteByIdsMapper`<br>
214+
方法:`int deleteByIds(String ids)`<br>
215+
说明:根据主键字符串进行删除,类中只有存在一个带有@Id注解的字段<br><br>
216+
217+
###Ids组合接口
218+
219+
接口:`IdsMapper<T>`<br>
220+
方法:包含上面Ids中的前两个方法<br><br>
221+
205222
##Mapper<T>接口
206223

207224
接口:`Mapper<T>`<br>

0 commit comments

Comments
 (0)