天天看點

Mybatis實作批量更新操作 三種方式

Mybatis實作批量更新操作

方式一:

<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>
           

但Mybatis映射檔案中的sql語句預設是不支援以" ; " 結尾的,也就是不支援多條sql語句的執行。是以需要在連接配接mysql的url上加 &allowMultiQueries=true 這個才可以執行。

方式二:

<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>
           

這種方式貌似效率不高,但是可以實作,而且不用改動mysql連接配接

效率參考文章:https://blog.csdn.net/xu1916659422/article/details/77971696/

方式三:

臨時改表sqlSessionFactory的屬性,實作批量送出的java,但無法傳回受影響數量。

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;//送出數量,到達這個數量就送出
            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();
            }
        }
        
}
           

其中 SpringContextUtil 是自己定義的工具類 用來擷取spring加載的bean對象,其中getBean() 獲得的是想要得到的sqlSessionFactory。Mapper 是自己的更具業務需求的Mapper接口類,Object是對象。

總結

方式一 需要修改mysql的連接配接url,讓全局支援多sql執行,不太安全

方式二 當資料量大的時候 ,效率明顯降低

方式三 需要自己控制,自己處理,一些隐藏的問題無法發現。

附件: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;
        }
    }
}