向数据库里插入10000条数据,分别使用三种插入方式,比较三种方式效率,第一种用时3000ms,第二种用时1500ms,第三种800ms,第三种效率最高。
package com.zhanghw.mybatisbatch; import com.zhanghw.mybatisbatch.entity.User; import com.zhanghw.mybatisbatch.mapper.UserMapper; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import org.junit.runner.RunWith; import org.mybatis.spring.SqlSessionTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.ArrayList; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest public class MybatisBatchApplicationTests { @Autowired private UserMapper userMapper; @Autowired private SqlSessionTemplate sqlSessionTemplate; /** * 通过简单循环批量插入 * @throws Exception */ @Test public void testInsertBatch1() throws Exception { long start = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { User user = User.builder().age(i).name("zhangsan" + i).password(i + "xxxx").sex("男").build(); int insert = userMapper.insert(user); System.out.println(insert); } long end = System.currentTimeMillis(); System.out.println("-------时间3188--------" + (start - end) + "---------------"); } /** * session使用批量模式插入数据 */ @Test public void testInsertBatch2() { long start = System.currentTimeMillis(); //跟上述sql区别,将session设置为批量形式 SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory() .openSession(ExecutorType.BATCH, false); UserMapper mapper = sqlSession.getMapper(UserMapper.class); for (int i = 0; i < 10000; i++) { User user = User.builder().age(i).name("zhangsan" + i).password(i + "xxxx").sex("男").build(); int insert = mapper.insert(user); System.out.println(insert); } sqlSession.commit(); long end = System.currentTimeMillis(); System.out.println("----时间1535-----------" + (start - end) + "---------------"); } /** * 通过foreach标签完成批量插入 */ @Test public void testInsertBatch3() { long start = System.currentTimeMillis(); List<User> list = new ArrayList<>(); for (int i = 0; i < 10000; i++) { User user = User.builder().age(i).name("zhangsan" + i).password(i + "xxxx").sex("男").build(); list.add(user); } int i = userMapper.insertBatch(list); System.out.println(i); long end = System.currentTimeMillis(); System.out.println("----时间824-----------" + (start - end) + "---------------"); } }
对应的mapper:
package com.zhanghw.mybatisbatch.mapper; import com.zhanghw.mybatisbatch.entity.User; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import java.util.List; @Mapper public interface UserMapper { public int insert(User user); public int insertBatch(@Param("list") List<User> list); }
对应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.zhanghw.mybatisbatch.mapper.UserMapper"> <insert id="insert"> INSERT INTO user ( name,password,sex,age) VALUES( #{name}, #{password}, #{sex}, #{age}) </insert> <insert id="insertBatch" parameterType="com.zhanghw.mybatisbatch.entity.User"> INSERT INTO user (name,password,sex,age) VALUES <foreach collection ="list" item="user" separator =","> (#{user.name}, #{user.password}, #{user.sex}, #{user.age}) </foreach > </insert> </mapper>