重点重点重点,不然会报错
连接数据库url后面加个参数
allowMultiQueries=true
用习惯了 insertList 怎么能没有 updateList呢
就两个类 直接上代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
package com.lancabbage.gorgeous.utils.mybatis; import org.apache.ibatis.mapping.MappedStatement; import tk.mybatis.mapper.entity.EntityColumn; import tk.mybatis.mapper.mapperhelper.EntityHelper; import tk.mybatis.mapper.mapperhelper.MapperHelper; import tk.mybatis.mapper.mapperhelper.MapperTemplate; import tk.mybatis.mapper.mapperhelper.SqlHelper; import java.util.Set; import static tk.mybatis.mapper.mapperhelper.SqlHelper.whereVersion; /** * ListUpdateProvider实现类,批量修改方法实现类 * * @author lanyanhua */ public class ListUpdateProvider extends MapperTemplate { public ListUpdateProvider(Class<?> mapperClass, MapperHelper mapperHelper) { super (mapperClass, mapperHelper); } // /** // * 通过主键更新全部字段 // * // * @param ms // */ // public String updateByPrimaryKey(MappedStatement ms) { // Class<?> entityClass = getEntityClass(ms); // StringBuilder sql = new StringBuilder(); // sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass))); // sql.append(SqlHelper.updateSetColumns(entityClass, null, false, false)); // sql.append(SqlHelper.wherePKColumns(entityClass, true)); // return sql.toString(); // } /** * 通过主键更新不为null的字段 * * @param ms * @return */ public String updateByPrimaryKeySelectiveList(MappedStatement ms) { Class<?> entityClass = getEntityClass(ms); StringBuilder sql = new StringBuilder(); sql.append( "<foreach collection=\"list\" item=\"record\" >" ); //set sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass))); sql.append(SqlHelper.updateSetColumns(entityClass, "record" , true , isNotEmpty())); //where sql.append( "<where>" ); Set<EntityColumn> columnSet = EntityHelper.getPKColumns(entityClass); //当某个列有主键策略时,不需要考虑他的属性是否为空,因为如果为空,一定会根据主键策略给他生成一个值 for (EntityColumn column : columnSet) { sql.append( " AND " ).append(column.getColumnEqualsHolder( "record" )); } sql.append(whereVersion(entityClass)); sql.append( "</where>" ); sql.append( " ;\n" ); sql.append( "</foreach>" ); return sql.toString(); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.lancabbage.gorgeous.utils.mybatis; import org.apache.ibatis.annotations.UpdateProvider; import tk.mybatis.mapper.annotation.RegisterMapper; import java.util.List; /** * 通用Mapper接口,更新 * * @param <T> 不能为空 * @author lanyanhua */ @RegisterMapper public interface ListUpdateByPrimaryKeySelectiveMapper<T> { /** * 根据主键更新属性不为null的值 * * @param record * @return */ @UpdateProvider (type = ListUpdateProvider. class , method = "dynamicSQL" ) int updateByPrimaryKeySelectiveList(List<T> record); } |
我是使用的是Tk.mybatis。代码是复制的原来的updateByPrimaryKeySelective的代码进行的微调
保证最终的结构类似这种就ok
1
2
3
4
5
6
7
8
9
10
11
|
<foreach collection= "list" item= "record" separator= ";" > UPDATE project <set>id = id, < if test= "record.name != null" >name = #{record.name},</ if > < if test= "record.remotePath != null" >remote_path = #{record.remotePath},</ if > < if test= "record.createTime != null" >create_time = #{record.createTime},</ if > < if test= "record.isShow != null" >is_show = #{record.isShow},</ if > < if test= "record.displayOrder != null" >display_order = #{record.displayOrder},</ if > </set> <where>AND id = #{record.id}</where> </foreach> |
使用 就和其他的一样 在你的Mapper类上继承这个类ListUpdateByPrimaryKeySelectiveMapper就好了
1
2
|
public interface BaseMapper<T> extends Mapper<T>, MySqlMapper<T>, ListUpdateByPrimaryKeySelectiveMapper<T> { } |
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注服务器之家的更多内容!
原文链接:https://blog.csdn.net/lanyanhua/article/details/121034603