package com.terra.common.extend;
|
|
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
|
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
|
import com.terra.common.entity.all.StaticData;
|
import org.apache.ibatis.mapping.MappedStatement;
|
import org.apache.ibatis.mapping.SqlSource;
|
|
/**
|
* 批量更新方法
|
* @author WWW
|
*/
|
@SuppressWarnings("ALL")
|
public class UpdateBatchMethod extends AbstractMethod {
|
/**
|
* update user set name = "a", age = 17 where id = 1;
|
* update user set name = "b", age = 18 where id = 2;
|
* <script>
|
* <foreach collection="list" item="item" separator=";">
|
* update user
|
* <set>
|
* <if test="item.name != null and item.name != ''">
|
* name = #{item.name,jdbcType=VARCHAR},
|
* </if>
|
* <if test="item.age != null">
|
* age = #{item.age,jdbcType=INTEGER},
|
* </if>
|
* </set>
|
* where id = #{item.id,jdbcType=INTEGER}
|
* </foreach>
|
* </script>
|
*/
|
@Override
|
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
|
String sql = "<script>\n<foreach collection=\"list\" item=\"item\" separator=\";\">\nupdate %s %s where %s=#{%s} %s\n</foreach>\n</script>";
|
String additional = tableInfo.isWithVersion() ? tableInfo.getVersionFieldInfo().getVersionOli("item", "item.") : "" + tableInfo.getLogicDeleteSql(true, true);
|
//String setSql = sqlSet(tableInfo.isWithLogicDelete(), false, tableInfo, false, "item", "item.");
|
String setSql = getSqlSet(tableInfo.isWithLogicDelete(), false, tableInfo, false, "item", "item.");
|
String sqlResult = String.format(sql, tableInfo.getTableName(), setSql, tableInfo.getKeyColumn(), "item." + tableInfo.getKeyProperty(), additional);
|
|
// update %s %s where %s=#{%s} %s
|
// update tab set a=#{a} where gid=1
|
|
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass);
|
|
// 第三个参数必须和RootMapper的自定义方法名一致
|
return this.addUpdateMappedStatement(mapperClass, modelClass, "updateBatch", sqlSource);
|
}
|
|
private String getSqlSet(boolean logic, boolean ew, TableInfo table, boolean judgeAliasNull, final String alias, final String prefix) {
|
// String sqlScript = table.getAllSqlSet(logic, prefix);
|
String sqlScript = getSqlSet(table);
|
if (judgeAliasNull) {
|
sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", alias), true);
|
}
|
if (ew) {
|
sqlScript = sqlScript + "\n";
|
sqlScript = sqlScript + this.convertIfEwParam("ew.sqlSet", false);
|
}
|
|
sqlScript = SqlScriptUtils.convertSet(sqlScript);
|
|
return sqlScript;
|
}
|
|
private String getSqlSet(TableInfo tableInfo) {
|
StringBuilder sb = new StringBuilder();
|
for (TableFieldInfo f : tableInfo.getFieldList()) {
|
if (StaticData.UPDATE_EXCLUDE_FIELDS.contains(f.getProperty())) {
|
continue;
|
}
|
|
if ("geom".equals(f.getProperty())) {
|
sb.append("<if test=\"item['geom'] != null\">geom=${item.geom},</if>\n");
|
continue;
|
}
|
|
sb.append(String.format("<if test=\"item['%s'] != null\">%s=#{item.%s},</if>\n", f.getProperty(), f.getColumn(), f.getProperty()));
|
}
|
sb.deleteCharAt(sb.length() - 1);
|
|
return sb.toString();
|
}
|
}
|