Mybatis 批量增加insert


方法一:foreach

容易出现的错误:出现SQL语句错误

解决方式: 在数据库的连接url后添加allowMultiQueries=true(允许批量更新)

jdbc.url=jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true

例子:

xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.center.manager.mapper.FundMapper">
    <insert id="insertForeach" parameterType="java.util.List" useGeneratedKeys="false">
                insert into fund
                ( id,fund_name,fund_code,date_x,data_y,create_by,create_date,update_by,update_date,remarks,del_flag)

                <foreach collection="list" item="item" index="index" separator=",">
          values
                    (
                        #{item.id}, #{item.fundName},#{item.fundCode},#{item.dateX},#{item.dataY},#{item.createBy},
                        #{item.createDate},#{item.updateBy},#{item.updateDate},#{item.remarks},#{item.delFlag}
                    )
                 </foreach>        
    </insert>    
</mapper>

mapper层

package com.center.manager.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.center.manager.entity.Fund;
        @Mapper
    public interface FundMapper {
        int insertForeach(List<Fund> list);
    }

Fun类

package com.center.manager.entity;
import java.util.Date;
        @data
    public class Fund {
        private  String id;    
        private String fundName;        
        private String fundCode;      
        private String dateX;             
        private String dataY;         
        private String remarks; 
        private String createBy; 
        private Date createDate; 
        private String updateBy; 
        private Date updateDate; 
        private String delFlag; 
    }

方法二:

Mybatis内置的ExecutorType有3种,默认的是simple,该模式下它为每个语句的执行创建一个新的预处理语句,单条提交sql;而batch模式重复使用已经预处理的语句,

并且批量执行所有更新语句,显然batch性能将更优;

service层

@Autowired
private SqlSessionTemplate sqlSessionTemplate;

public void add(List<ApplyInfo> list) {
       SqlSession sessionn = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH,false);
       AddInfoDap mapper = session.getMapper(AddInfo.class);
       for(int i = 0; i < list.seize ; i++) {
              mapper.addApplicationDetail(lisr.get(i));
                     if(i == list.size()-1) {
                            session.commit();
                     session.close();
                     }   
       }
}             

xml层

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.center.manager.mapper.FundMapper">
    <insert id="addApplicationDetail" parameterType="com.dao.entity.ApplyInfo" >
                insert into fund
                ( id,fund_name,fund_code,date_x,data_y,create_by,create_date,update_by,update_date,remarks,del_flag)
          values
                    (
                        #{item.id}, #{item.fundName},#{item.fundCode},#{item.dateX},#{item.dataY},#{item.createBy},
                        #{item.createDate},#{item.updateBy},#{item.updateDate},#{item.remarks},#{item.delFlag}
                    )
                 </foreach>        
    </insert>    
</mapper>

 

mapper层

 

 

void addApplicationDetail(ApplyInfo infos)

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM