Three ways to batch update Mybatis

Mybatis implements batch update operation
Method 1:

<update id="updateBatch"  parameterType="java.util.List">  
    <foreach collection="list" item="item" index="index" open="" close="" separator=";">
        update tableName
        <set>
            name=${item.name},
            name2=${item.name2}
        </set>
        where id = ${item.id}
    </foreach>      
</update>

However, by default, the sql statement in the Mybatis mapping file does not support the execution of multiple sql statements ending with ';'. So you need to add & allowmultiqueries = true to the url to connect to mysql to execute.
Mode two:

<update id="updateBatch" parameterType="java.util.List">
        update tableName
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="c_name =case" suffix="end,">
                <foreach collection="list" item="cus">
                    <if test="cus.name!=null">
                        when id=#{cus.id} then #{cus.name}
                    </if>
                </foreach>
            </trim>
            <trim prefix="c_age =case" suffix="end,">
                <foreach collection="list" item="cus">
                    <if test="cus.age!=null">
                        when id=#{cus.id} then #{cus.age}
                    </if>
                </foreach>
            </trim>
        </trim>
        <where>
            <foreach collection="list" separator="or" item="cus">
                id = #{cus.id}
            </foreach>
        </where>
</update>

This method seems to be inefficient, but it can be implemented without changing the mysql connection
Efficiency reference article: https://blog.csdn.net/xu19166...
Mode three:
Temporarily change the properties of sqlSessionFactory to implement batch submitted java, but the affected quantity cannot be returned.

public int updateBatch(List<Object> list){
        if(list ==null || list.size() <= 0){
            return -1;
        }
        SqlSessionFactory sqlSessionFactory = SpringContextUtil.getBean("sqlSessionFactory");
        SqlSession sqlSession = null;
        try {
            sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,false);
            Mapper mapper = sqlSession.getMapper(Mapper.class);
            int batchCount = 1000;//Submit quantity, submit when the quantity reaches
            for (int index = 0; index < list.size(); index++) {
                Object obj = list.get(index);
                mapper.updateInfo(obj);
                if(index != 0 && index%batchCount == 0){
                    sqlSession.commit();
                }                    
            }
            sqlSession.commit();
            return 0;
        }catch (Exception e){
            sqlSession.rollback();
            return -2;
        }finally {
            if(sqlSession != null){
                sqlSession.close();
            }
        }
        
}

Where SpringContextUtil is a tool class defined by itself to get the bean Object loaded by spring, and getBean() gets the sqlSessionFactory you want. Mapper is a mapper interface class with more business requirements, and Object is an Object.
summary
Mode 1: the connection url of mysql needs to be modified to allow global support for multiple sql execution, which is not safe
Mode 2: when the amount of data is large, the efficiency is significantly reduced
Mode 3 requires self-control and self-treatment. Some hidden problems cannot be found.

Attachment: SpringContextUtil.java

@Component
public class SpringContextUtil implements ApplicationContextAware{

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext(){
        return applicationContext;
    }

    public static Object getBean(Class T){
        try {
            return applicationContext.getBean(T);
        }catch (BeansException e){
            return null;
        }
    }

    public static Object getBean(String name){
        try {
            return applicationContext.getBean(name);
        }catch (BeansException e){
            return null;
        }
    }
}

Tags: Java SQL MySQL Mybatis

Posted on Sun, 01 Dec 2019 22:28:28 -0800 by RealDrift