管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2022-11-20 c3adc095b714ed84c037759de5834fc946494cb7
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
package com.lf.server.extend;
 
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 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 sqlResult = String.format(sql, tableInfo.getTableName(), setSql, tableInfo.getKeyColumn(), "item." + tableInfo.getKeyProperty(), additional);
 
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass);
 
        // 第三个参数必须和RootMapper的自定义方法名一致
        return this.addUpdateMappedStatement(mapperClass, modelClass, "updateBatch", sqlSource);
    }
}