sql分批插入,形式参数list,结合mybatis使用

批量insert多条数据,支持list参数的字段多少是可变长度的,保证调用时候,list内的每一个元素属性是一致的,就可以使用。主要是结合xml模板,mapper接口的参数,能被mybatis取到参数,执行正确的sql语法。

方式一:

<!-- 批量新增  ,包含字段 receive_open_id ,map参数来构造灵活的sql-->
<insert id="insertBatchForColumns" parameterType="map" keyProperty="id" keyColumn="id" useGeneratedKeys="true">
   <!-- <selectKey resultType="java.lang.Integer" keyProperty="id" order="AFTER">  SELECT LAST_INSERT_ID() AS id </selectKey>  -->
   insert into t_cloudcrm_user_coupon_relate
   (open_id, merchant_code,
   <if test="receiveOpenId != null and receiveOpenId != ''">
      receive_open_id,
   </if>
   coupon_id, task_id,
   coupon_state,
   coupon_fee, exp_time,
   crt_time, cur_date, get_type,eff_time,
   send_shop_name, send_state
   )
   values
   <foreach collection="tCloudcrmUserCouponRelateList" item="item" index="index" separator=",">
      (#{item.openId,jdbcType=VARCHAR},
      #{item.merchantCode,jdbcType=CHAR},
      <if test="receiveOpenId != null and receiveOpenId != ''">
      #{item.receiveOpenId,jdbcType=CHAR},
      </if>
      #{item.couponId,jdbcType=INTEGER},
      #{item.taskId,jdbcType=INTEGER},
      #{item.couponState,jdbcType=CHAR},
      #{item.couponFee,jdbcType=INTEGER},
      #{item.expTime,jdbcType=TIMESTAMP},
      #{item.crtTime,jdbcType=TIMESTAMP},
      #{item.curDate,jdbcType=CHAR},
      #{item.getType,jdbcType=CHAR},
      #{item.effTime,jdbcType=TIMESTAMP},
      <choose>
         <when test="item.sendShopName != null and item.sendShopName != ''">#{item.sendShopName,jdbcType=VARCHAR},</when>
         <otherwise>'',</otherwise>
      </choose>
      <choose>
         <w
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: MyBatis 可以通过以下方法进行分批插入: 1. 使用 foreach 标签:可以将数据集合按照一定的大小进行分批,然后在 foreach 标签中循环插入数据。 示例代码: ``` <foreach collection="list" item="item" index="index" separator=";"> insert into table_name (column1, column2, ...) values <foreach collection="item" item="i" separator=","> (#{i.column1}, #{i.column2}, ...) </foreach> </foreach> ``` 2. 使用 JDBC 批量插入:可以将数据集合按照一定的大小进行分批,然后使用 JDBC 的 addBatch 和 executeBatch 方法进行批量插入。 示例代码: ``` SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); try { Mapper mapper = sqlSession.getMapper(Mapper.class); for (int i = 0; i < list.size(); i++) { mapper.insertData(list.get(i)); if (i % batchSize == 0) { sqlSession.flushStatements(); } } sqlSession.flushStatements(); sqlSession.commit(); } finally { sqlSession.close(); } ``` 其中,batchSize 表示每个批次插入的数据条数,可以根据实际情况进行调整。 通过以上两种方式进行分批插入,可以避免一次性插入数据过多导致的性能问题,提高数据插入的效率。 ### 回答2: MyBatis可以通过以下几个步骤来实现分批插入: 1.确定要插入的数据集合,可以将数据集合按照指定大小进行拆分,形成多个子集合。 2.按照需要插入的数据对象的大小,分别创建对应数量的SqlSession对象,以便能够并发地插入数据。 3.在每个SqlSession对象中,使用BatchExecutor执行批量插入操作。BatchExecutor是MyBatis中的内置批量操作执行器,可以实现高效的批量插入。 4.对于每个子集合,通过SqlSession的insert方法将数据插入到数据库中。 5.最后,需要向数据库提交事务,以确保插入的数据生效。可以调用SqlSession对象的commit方法来提交事务。 通过以上步骤,可以将较大的数据集合进行分批插入操作,提高插入数据的效率。同时,通过并发地使用多个SqlSession对象执行插入操作,还可以进一步提高插入数据的速度。 ### 回答3: 在MyBatis中,可以使用两种方法来实现分批插入数据。 方法一:使用BatchExecutor执行器 1. 首先,在MyBatis的配置文件中,将执行器类型设置为BatchExecutor。例如: ``` <!-- mybatis-config.xml --> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <!-- 数据源配置 --> </dataSource> </environment> </environments> <mappers> <!-- Mapper配置 --> </mappers> <settings> <setting name="defaultExecutorType" value="BATCH" /> </settings> </configuration> ``` 2. 然后,在Mapper接口中定义批量插入的方法。例如: ``` // UserMapper.java void batchInsert(List<User> userList); ``` 3. 在Mapper的XML文件中,编写批量插入SQL语句。例如: ``` <!-- UserMapper.xml --> <mapper namespace="com.example.mapper.UserMapper"> <insert id="batchInsert" parameterType="java.util.List"> INSERT INTO user (id, username, password) VALUES <foreach collection="list" item="user" separator=","> (#{user.id}, #{user.username}, #{user.password}) </foreach> </insert> </mapper> ``` 4. 最后,在Java代码中,调用Mapper接口的批量插入方法。例如: ``` // Main.java List<User> userList = new ArrayList<>(); // 添加用户数据到userList try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); userMapper.batchInsert(userList); sqlSession.commit(); } ``` 方法二:使用MyBatis-Plus 1. 引入MyBatis-Plus的依赖。例如: ``` <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.x.x</version> </dependency> ``` 2. 在Mapper接口中,继承BaseMapper接口,并使用@InsertBatch注解定义批量插入的方法。例如: ``` // UserMapper.java @Repository public interface UserMapper extends BaseMapper<User> { @InsertBatch void batchInsert(List<User> userList); } ``` 3. 在Java代码中,调用Mapper接口的批量插入方法。例如: ``` // Main.java List<User> userList = new ArrayList<>(); // 添加用户数据到userList try (SqlSession sqlSession = sqlSessionFactory.openSession()) { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); userMapper.batchInsert(userList); sqlSession.commit(); } ``` 无论使用哪种方法,都可以通过设置合适的批量大小(batchSize)来控制每次插入的数据数量,以达到分批插入的目的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值