batch-insert-mybatis

2 篇文章 0 订阅
前言

后台开发中,批量往数据库写数据是一个很常见的功能,下面就简单实现一下使用 mybatis 来 batch 写入。

实现介绍
添加依赖

在项目的 pom.xml 中配置 mybatis 及 mysql 相关的依赖

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.46</version>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.0</version>
</dependency>
Mapper 接口

在这个 mapper 的 接口类中添加批量新增的方法。

import com.jack.entity.User;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface UserMapper {

    /***
     * <p>
     * 批量插入用户数据
     * </p>
     * @author jack
     *
     * @param userList 用户数据列表
     */
    void batchInsertSelective(@Param("userList") List<User> userList);
}
Mapper xml

在这个 mapper 的 xml 中实现批量新增的方法。

<insert id="batchInsertSelective" parameterType="java.util.List">
    <foreach collection="userList" item="user" index="index" separator=";">
      insert into tb_user
      <trim prefix="(" suffix=")" suffixOverrides=",">
        <if test="user.id != null">
          id,
        </if>
        <if test="user.userName != null">
          user_name,
        </if>
        <if test="user.userPassword != null">
          user_password,
        </if>
        <if test="user.userSalt != null">
          user_salt,
        </if>
        <if test="user.phone != null">
          phone,
        </if>
        <if test="user.createUser != null">
          create_user,
        </if>
        <if test="user.createTime != null">
          create_time,
        </if>
        <if test="user.updateUser != null">
          update_user,
        </if>
        <if test="user.updateTime != null">
          update_time,
        </if>
      </trim>
      <trim prefix="values (" suffix=")" suffixOverrides=",">
        <if test="user.id != null">
          #{user.id,jdbcType=VARCHAR},
        </if>
        <if test="user.userName != null">
          #{user.userName,jdbcType=CHAR},
        </if>
        <if test="user.userPassword != null">
          #{user.userPassword,jdbcType=VARCHAR},
        </if>
        <if test="user.userSalt != null">
          #{user.userSalt,jdbcType=VARCHAR},
        </if>
        <if test="user.phone != null">
          #{user.phone,jdbcType=VARCHAR},
        </if>
        <if test="user.createUser != null">
          #{user.createUser,jdbcType=VARCHAR},
        </if>
        <if test="user.createTime!= null">
          #{user.createTime,jdbcType=VARCHAR},
        </if>
        <if test="user.updateUser!= null">
          #{user.updateUser,jdbcType=VARCHAR},
        </if>
        <if test="user.updateTime!= null">
          #{user.updateTime,jdbcType=TIMESTAMP},
        </if>
      </trim>
    </foreach>
</insert>
使用

使用 spring 注入,然后调用即可

@Autowired
private UserMapper userMapper;

public void testMybatisBatchInsertUser(int count) {
    long t1 = System.currentTimeMillis();
    userMapper.batchInsertSelective(getUserList(count));
    System.out.println("【mybatis-batch】插入条数:【" + count + "】耗时:【"
            + (System.currentTimeMillis() - t1) + "】");
}
结语

到此,使用 mybatis 来 batch 写入数据的实现就介绍完了,后续继续其他方式的批量写入 …

如果您看到了这里,欢迎和我沟通交流!
             一个95后码农

个人博客:fy-blog

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值