准备工作见上一篇批量插入
根据id批量修改
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
/**
* @author xiu
* @date 2022/1/27 11:13
* @description 根据id批量修改
*/
public class MyUpdateListSelectiveMethod extends AbstractMethod {
public MyUpdateListSelectiveMethod(String methodName) {
super(methodName);
}
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
StringBuilder sql = new StringBuilder();
sql.append("<script>\n");
sql.append("<foreach collection=\"records\" item=\"record\" index=\"index\" open=\"\" close=\"\" separator=\";\">");
sql.append(" UPDATE ");
sql.append(tableInfo.getTableName());
sql.append(this.sqlSet(true, false, tableInfo, true, "record", "record."));
sql.append(" WHERE ");
sql.append(String.format(" %s=#{%s} ", tableInfo.getKeyColumn(), "record." + tableInfo.getKeyProperty()));
sql.append("</foreach>");
sql.append("\n</script>");
SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sql.toString(), modelClass);
return this.addUpdateMappedStatement(mapperClass, modelClass, sqlSource);
}
}
然后就能通过myUpdateListSelective进行批量修改数据了