mybatis批量更新

本文介绍了使用MyBatis进行批量更新时遇到的问题及解决方案。在DAO层注入sqlSessionFactory,XML配置文件正常,但在Service层发现批量更新不起作用,通过在数据库连接URL添加`&allowMultiQueries=true`解决了问题。同时对比了三种不同的批量插入方法,包括单独开session插入、使用foreach SQL和设置ExecutorType.Batch,实验结果显示设置ExecutorType.Batch并配合foreach SQL的插入方式性能最佳。
摘要由CSDN通过智能技术生成


1.先看DAO层


 public void updateList(List<AddService> oldAddServiceList) {
        SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH,true);
        session.update("AddServiceMapper.batchUpdate",oldAddServiceList);
        session.commit();
        session.close();
    }


当然DAO层最上方类中注入上sqlSessionFactory

    @Resource(name = "sqlSessionFactory")
    private SqlSessionFactory sqlSessionFactory;

sqlSessionFactory.openSession(ExecutorType.BATCH,true);


详细了解请点击  http://www.cnblogs.com/xcch/articles/2042298.html

2.再看XML配置文件

 <update id="batchUpdate" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" separator=";">
            update sp_add_service set
                SERVICE_NAME = #{item.serviceName},
                SERVICE_TYPE = #{item.serviceType},
                PRICE = #{item.price},
                DESCRIPTION = #{item.description},
                PRICE_RATE = #{item.priceRate},
                IS_REQUIRED = #{item.isRequired},
                MODIFY_TIME = now()
            where ID = #{item.id}
        </foreach>
    </update>


3.再看Service层

 @Transactional
    public void updateOldAddServces(List<AddService> oldAddServiceList) {
        addServiceDao.updateList(oldAddServiceList);
    }
加上事务注解,防止出异常。好让事务回滚

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值