Java Mybatis Batch Modification Package Detailed Explanation
- 2021-12-09 08:44:55
- OfStack
Focus, focus, or you will report errors
Add a parameter after the connection database url
allowMultiQueries=true
How can you get used to insertList without updateList
Code directly on two classes
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 Implementation class, batch modification method implementation class
*
* @author lanyanhua
*/
public class ListUpdateProvider extends MapperTemplate {
public ListUpdateProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
super(mapperClass, mapperHelper);
}
// /**
// * Update all fields with primary key
// *
// * @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();
// }
/**
* Update by primary key is not null Fields of
*
* @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);
// When a column has a primary key policy, it is not necessary to consider whether its attribute is empty, because if it is empty, 1 Will be generated for him according to the primary key policy 1 Values
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();
}
}
package com.lancabbage.gorgeous.utils.mybatis;
import org.apache.ibatis.annotations.UpdateProvider;
import tk.mybatis.mapper.annotation.RegisterMapper;
import java.util.List;
/**
* General Mapper Interface , Update
*
* @param <T> Cannot be empty
* @author lanyanhua
*/
@RegisterMapper
public interface ListUpdateByPrimaryKeySelectiveMapper<T> {
/**
* Updating properties by primary key is not null Value of
*
* @param record
* @return
*/
@UpdateProvider(type = ListUpdateProvider.class, method = "dynamicSQL")
int updateByPrimaryKeySelectiveList(List<T> record);
}
I'm using Tk. mybatis. The code is a fine tuning of the original updateByPrimaryKeySelective code
Ensure that the final structure is similar to this one on ok
<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>
Just inherit this class ListUpdateByPrimaryKeySelectiveMapper from your Mapper class like others
public interface BaseMapper<T> extends Mapper<T>, MySqlMapper<T>, ListUpdateByPrimaryKeySelectiveMapper<T> {
}
Summarize
This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!