Skip to content

Commit 566cad8

Browse files
committed
注入方法deleteBatchIds重命名deleteByIds.
1 parent 3d7faee commit 566cad8

File tree

7 files changed

+125
-77
lines changed

7 files changed

+125
-77
lines changed

changelog-temp.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- feat: 修改AES密钥随机性生成
2121
- feat: UpdateWrapper增加checkSqlInjection方法
2222
- feat: 调整DDL脚本自动装配逻辑(当无实现时或无mybatis-plus-extension模块时不注入DDL运行bean)
23+
- feat: 注入方法deleteBatchIds重命名deleteByIds
2324
- feat: SpringBoot升级至2.7.18和3.2.6
2425
- feat: 升级kotlin至1.9.24
2526
- feat: 升级lombok至1.18.32

mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/enums/SqlMethod.java

+16
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,31 @@ public enum SqlMethod {
3535
@Deprecated
3636
DELETE_BY_MAP("deleteByMap", "根据columnMap 条件删除记录", "<script>\nDELETE FROM %s %s\n</script>"),
3737
DELETE("delete", "根据 entity 条件删除记录", "<script>\nDELETE FROM %s %s %s\n</script>"),
38+
/**
39+
* @deprecated 3.5.7 {@link #DELETE_BY_IDS}
40+
*/
41+
@Deprecated
3842
DELETE_BATCH_BY_IDS("deleteBatchIds", "根据ID集合,批量删除数据", "<script>\nDELETE FROM %s WHERE %s IN (%s)\n</script>"),
43+
/**
44+
* @since 3.5.7
45+
*/
46+
DELETE_BY_IDS("deleteByIds", "根据ID集合,批量删除数据", "<script>\nDELETE FROM %s WHERE %s IN (%s)\n</script>"),
3947

4048
/**
4149
* 逻辑删除
4250
*/
4351
LOGIC_DELETE_BY_ID("deleteById", "根据ID 逻辑删除一条数据", "<script>\nUPDATE %s %s WHERE %s=#{%s} %s\n</script>"),
4452
LOGIC_DELETE_BY_MAP("deleteByMap", "根据columnMap 条件逻辑删除记录", "<script>\nUPDATE %s %s %s\n</script>"),
4553
LOGIC_DELETE("delete", "根据 entity 条件逻辑删除记录", "<script>\nUPDATE %s %s %s %s\n</script>"),
54+
/**
55+
* @deprecated 3.5.7 {@link #LOGIC_DELETE_BY_IDS}
56+
*/
57+
@Deprecated
4658
LOGIC_DELETE_BATCH_BY_IDS("deleteBatchIds", "根据ID集合,批量逻辑删除数据", "<script>\nUPDATE %s %s WHERE %s IN (%s) %s\n</script>"),
59+
/**
60+
* @since 3.5.7
61+
*/
62+
LOGIC_DELETE_BY_IDS("deleteByIds", "根据ID集合,批量逻辑删除数据", "<script>\nUPDATE %s %s WHERE %s IN (%s) %s\n</script>"),
4763

4864
/**
4965
* 修改

mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/DefaultSqlInjector.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public List<AbstractMethod> getMethodList(Configuration configuration, Class<?>
4848
.add(new SelectList());
4949
if (tableInfo.havePK()) {
5050
builder.add(new DeleteById())
51-
.add(new DeleteBatchByIds())
51+
.add(new DeleteByIds())
5252
.add(new UpdateById())
5353
.add(new SelectById())
5454
.add(new SelectBatchByIds());

mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/DeleteBatchByIds.java

+3-73
Original file line numberDiff line numberDiff line change
@@ -15,84 +15,14 @@
1515
*/
1616
package com.baomidou.mybatisplus.core.injector.methods;
1717

18-
import com.baomidou.mybatisplus.core.enums.SqlMethod;
19-
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
20-
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
21-
import com.baomidou.mybatisplus.core.metadata.TableInfo;
22-
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
23-
import com.baomidou.mybatisplus.core.toolkit.Constants;
24-
import com.baomidou.mybatisplus.core.toolkit.StringPool;
25-
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
26-
import org.apache.ibatis.mapping.MappedStatement;
27-
import org.apache.ibatis.mapping.SqlSource;
28-
29-
import java.util.List;
30-
31-
import static java.util.stream.Collectors.joining;
32-
import static java.util.stream.Collectors.toList;
33-
3418
/**
3519
* 根据 ID 集合删除
3620
*
3721
* @author hubin
3822
* @since 2018-04-06
23+
* @deprecated 3.5.7 {@link DeleteByIds}
3924
*/
40-
public class DeleteBatchByIds extends AbstractMethod {
41-
42-
public DeleteBatchByIds() {
43-
this(SqlMethod.DELETE_BATCH_BY_IDS.getMethod());
44-
}
45-
46-
/**
47-
* @param name 方法名
48-
* @since 3.5.0
49-
*/
50-
public DeleteBatchByIds(String name) {
51-
super(name);
52-
}
53-
54-
@Override
55-
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
56-
String sql;
57-
SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE_BATCH_BY_IDS;
58-
if (tableInfo.isWithLogicDelete()) {
59-
sql = logicDeleteScript(tableInfo, sqlMethod);
60-
SqlSource sqlSource = super.createSqlSource(configuration, sql, Object.class);
61-
return addUpdateMappedStatement(mapperClass, modelClass, methodName, sqlSource);
62-
} else {
63-
sqlMethod = SqlMethod.DELETE_BATCH_BY_IDS;
64-
sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), tableInfo.getKeyColumn(),
65-
SqlScriptUtils.convertForeach(
66-
SqlScriptUtils.convertChoose("@org.apache.ibatis.type.SimpleTypeRegistry@isSimpleType(item.getClass())",
67-
"#{item}", "#{item." + tableInfo.getKeyProperty() + "}"),
68-
COLL, null, "item", COMMA));
69-
SqlSource sqlSource = super.createSqlSource(configuration, sql, Object.class);
70-
return this.addDeleteMappedStatement(mapperClass, methodName, sqlSource);
71-
}
72-
}
73-
74-
/**
75-
* @param tableInfo 表信息
76-
* @return 逻辑删除脚本
77-
* @since 3.5.0
78-
*/
79-
public String logicDeleteScript(TableInfo tableInfo, SqlMethod sqlMethod) {
80-
List<TableFieldInfo> fieldInfos = tableInfo.getFieldList().stream()
81-
.filter(TableFieldInfo::isWithUpdateFill)
82-
.filter(f -> !f.isLogicDelete())
83-
.collect(toList());
84-
String sqlSet = "SET ";
85-
if (CollectionUtils.isNotEmpty(fieldInfos)) {
86-
sqlSet += SqlScriptUtils.convertIf(fieldInfos.stream()
87-
.map(i -> i.getSqlSet(Constants.ENTITY + StringPool.DOT)).collect(joining(EMPTY)), String.format("%s != null", Constants.ENTITY), true);
88-
}
89-
sqlSet += StringPool.EMPTY + tableInfo.getLogicDeleteSql(false, false);
90-
return String.format(sqlMethod.getSql(), tableInfo.getTableName(),
91-
sqlSet, tableInfo.getKeyColumn(), SqlScriptUtils.convertForeach(
92-
SqlScriptUtils.convertChoose("@org.apache.ibatis.type.SimpleTypeRegistry@isSimpleType(item.getClass())",
93-
"#{item}", "#{item." + tableInfo.getKeyProperty() + "}"),
94-
COLL, null, "item", COMMA),
95-
tableInfo.getLogicDeleteSql(true, true));
96-
}
25+
@Deprecated
26+
public class DeleteBatchByIds extends DeleteByIds {
9727

9828
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (c) 2011-2024, baomidou (jobob@qq.com).
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.baomidou.mybatisplus.core.injector.methods;
17+
18+
import com.baomidou.mybatisplus.core.enums.SqlMethod;
19+
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
20+
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
21+
import com.baomidou.mybatisplus.core.metadata.TableInfo;
22+
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
23+
import com.baomidou.mybatisplus.core.toolkit.Constants;
24+
import com.baomidou.mybatisplus.core.toolkit.StringPool;
25+
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
26+
import org.apache.ibatis.mapping.MappedStatement;
27+
import org.apache.ibatis.mapping.SqlSource;
28+
29+
import java.util.List;
30+
31+
import static java.util.stream.Collectors.joining;
32+
import static java.util.stream.Collectors.toList;
33+
34+
35+
/**
36+
* 根据 ID 集合删除
37+
*
38+
* @author nieqiurong
39+
* @since 3.5.7
40+
*/
41+
public class DeleteByIds extends AbstractMethod {
42+
43+
public DeleteByIds() {
44+
this(SqlMethod.DELETE_BY_IDS.getMethod());
45+
}
46+
47+
/**
48+
* @param name 方法名
49+
* @since 3.5.0
50+
*/
51+
public DeleteByIds(String name) {
52+
super(name);
53+
}
54+
55+
@Override
56+
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
57+
String sql;
58+
SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE_BY_IDS;
59+
if (tableInfo.isWithLogicDelete()) {
60+
sql = logicDeleteScript(tableInfo, sqlMethod);
61+
SqlSource sqlSource = super.createSqlSource(configuration, sql, Object.class);
62+
return addUpdateMappedStatement(mapperClass, modelClass, methodName, sqlSource);
63+
} else {
64+
sqlMethod = SqlMethod.DELETE_BY_IDS;
65+
sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), tableInfo.getKeyColumn(),
66+
SqlScriptUtils.convertForeach(
67+
SqlScriptUtils.convertChoose("@org.apache.ibatis.type.SimpleTypeRegistry@isSimpleType(item.getClass())",
68+
"#{item}", "#{item." + tableInfo.getKeyProperty() + "}"),
69+
COLL, null, "item", COMMA));
70+
SqlSource sqlSource = super.createSqlSource(configuration, sql, Object.class);
71+
return this.addDeleteMappedStatement(mapperClass, methodName, sqlSource);
72+
}
73+
}
74+
75+
/**
76+
* @param tableInfo 表信息
77+
* @return 逻辑删除脚本
78+
* @since 3.5.0
79+
*/
80+
public String logicDeleteScript(TableInfo tableInfo, SqlMethod sqlMethod) {
81+
List<TableFieldInfo> fieldInfos = tableInfo.getFieldList().stream()
82+
.filter(TableFieldInfo::isWithUpdateFill)
83+
.filter(f -> !f.isLogicDelete())
84+
.collect(toList());
85+
String sqlSet = "SET ";
86+
if (CollectionUtils.isNotEmpty(fieldInfos)) {
87+
sqlSet += SqlScriptUtils.convertIf(fieldInfos.stream()
88+
.map(i -> i.getSqlSet(Constants.ENTITY + StringPool.DOT)).collect(joining(EMPTY)), String.format("%s != null", Constants.ENTITY), true);
89+
}
90+
sqlSet += StringPool.EMPTY + tableInfo.getLogicDeleteSql(false, false);
91+
return String.format(sqlMethod.getSql(), tableInfo.getTableName(),
92+
sqlSet, tableInfo.getKeyColumn(), SqlScriptUtils.convertForeach(
93+
SqlScriptUtils.convertChoose("@org.apache.ibatis.type.SimpleTypeRegistry@isSimpleType(item.getClass())",
94+
"#{item}", "#{item." + tableInfo.getKeyProperty() + "}"),
95+
COLL, null, "item", COMMA),
96+
tableInfo.getLogicDeleteSql(true, true));
97+
}
98+
99+
}

mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/mapper/BaseMapper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ default int deleteByIds(@Param(Constants.COLL) Collection<?> collections, boolea
213213
params.put(Constants.ENTITY, tableInfo.newInstance());
214214
}
215215
params.put(Constants.COLL, collections);
216-
return sqlSession.delete(mapperInterface.getName() + StringPool.DOT + SqlMethod.DELETE_BATCH_BY_IDS.getMethod(), params);
216+
return sqlSession.delete(mapperInterface.getName() + StringPool.DOT + SqlMethod.DELETE_BY_IDS.getMethod(), params);
217217
}
218218

219219
/**

mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/LogicDeleteBatchByIds.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package com.baomidou.mybatisplus.extension.injector.methods;
1717

1818
import com.baomidou.mybatisplus.core.enums.SqlMethod;
19-
import com.baomidou.mybatisplus.core.injector.methods.DeleteBatchByIds;
19+
import com.baomidou.mybatisplus.core.injector.methods.DeleteByIds;
2020
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
2121
import com.baomidou.mybatisplus.core.metadata.TableInfo;
2222
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
@@ -46,8 +46,10 @@
4646
*
4747
* @author nieqiurong
4848
* @since 3.5.0
49+
* @deprecated 3.5.7 {@link DeleteByIds}
4950
*/
50-
public class LogicDeleteBatchByIds extends DeleteBatchByIds {
51+
@Deprecated
52+
public class LogicDeleteBatchByIds extends DeleteByIds {
5153

5254
public LogicDeleteBatchByIds() {
5355
super();

0 commit comments

Comments
 (0)