Mybatis批量插入功能

数据库的结构如下所示

使用insert标签与foreach标签实现批量插入功能。

测试插入10000条数据的时间,测试代码如下,时间959ms。

public class Test {
    public  static  void main(String[] args) throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class);

        List<Account> accounts = new ArrayList<>();
        for (int i = 0; i < 10000; i++){
            accounts.add(new Account("admin" +i, "password", "test"));
        }

        Date date = new Date();
        accountMapper.insertBatch(accounts);

        /*for (int i = 0; i < 10000; i++){
            accountMapper.insert(accounts.get(i));
        }*/
        sqlSession.commit();
        System.out.println(new Date().getTime() - date.getTime());
        sqlSession.close();
    }

}

 mapper.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.hyself.icake.dao.AccountMapper">
<insert id="insertBatch" useGeneratedKeys="true">
    INSERT INTO account(account, password, nick_name) VALUES
    <foreach collection="accounts" item="account" separator=",">
        (#{account.account},#{account.password},#{account.nickname})
    </foreach>
</insert>
</mapper>

从上面可以xml文件可以看出,此功能其实是使用了INSERT INTO ... VALUES (),(),()...功能,Mybatis还支持批量提交插入语句,原理是实现了N条INSERT INTO ... VALUES (); INSERT INTO ... VALUES()...;...

mapper.xml的配置如下,可以看到分隔符的配置改成了";",这个地方也可以不配置分隔符,直接在INSERT INTO语句的最后加上分号。除了在XML中设置,还需要在在数据库的url后面加上允许提交多个语句的配置(?allowMultiQueries=true),否则提交会失败。

<?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.hyself.icake.dao.AccountMapper">
    <insert id="insertBatch2" useGeneratedKeys="true">
        <foreach collection="accounts" item="account" separator=";">
            INSERT INTO account(account, password, nick_name) VALUES (#{account.account},#{account.password},#{account.nickname})
        </foreach>
    </insert>
</mapper>

使用openSession(ExecutorType.BATCH);去实现批量插入

代码如下,测试时间为2900ms

public class Test {
    public  static  void main(String[] args) throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
        AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class);

        List<Account> accounts = new ArrayList<>();
        for (int i = 0; i < 10000; i++){
            accounts.add(new Account("admin" +i, "password", "test"));
        }

        /*accountMapper.insertBatch(accounts);*/
        Date date = new Date();
        for (int i = 0; i < 10000; i++){
            accountMapper.insert(accounts.get(i));
        }
        sqlSession.commit();
        System.out.println(new Date().getTime() - date.getTime());
        sqlSession.close();
    }

}

 mapper.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.hyself.icake.dao.AccountMapper">
    <insert id="insert">
        INSERT INTO account(account, password, nick_name) VALUES
            (#{account.account},#{account.password},#{account.nickname})
    </insert>
    
</mapper>

 

但是insert标签与foreach标签的办法因为数字符串拼接,所以当个数太大的时候,程序会报异常。

备注:mapper文件的配置如下。

<?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.hyself.icake.dao.AccountMapper">
<insert id="insertBatch" useGeneratedKeys="true" keyProperty="">
    INSERT INTO account(account, password, nick_name) VALUES
    <foreach collection="accounts" item="account" separator=",">
        (#{account.account},#{account.password},#{account.nickname})
    </foreach>
</insert>
<insert id="insert">
    INSERT INTO account(account, password, nick_name) VALUES
     (#{account.account},#{account.password},#{account.nickname})
</insert>


</mapper>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值