前言

后台开发中,批量往数据库写数据是一个很常见的功能,下面就简单实现一下使用 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

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐