Mybatis插入大量数据效率对比:foreach插入、SqlSession批量插入、sql插入
阅读本文大概需要 2.8 分钟。
来自:blog.csdn.net/chenping1993/article/details/106116000
使用 SqlSessionFactory,每一批数据执行一次提交 使用mybatis-plus框架的insert方法,for循环,每次执行一次插入 使用ibatis,纯sql插入
//测试类
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class Test1 {
@Autowired
UsersMapper usersMapper;
@Autowired
SqlSessionFactory sqlSessionFactory;
public Listlist = new ArrayList<>();
@Before
public void getList() {
long start = System.currentTimeMillis();
Users user;
for (int i = 1; i <=50000 ; i++) {
user = new Users();
user.setId(i);
user.setName("java");
user.setAge(200);
user.setManagerId(222);
list.add(user);
}
System.out.println("拼装数据 耗时:"+(System.currentTimeMillis()-start));
System.out.println(list.size());
}
@Test
public void batchInsert() {
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,false);
UsersMapper mapper = sqlSession.getMapper(UsersMapper.class);
System.out.println("batchInsert 插入开始========");
long start = System.currentTimeMillis();
for (int i = 0; i < list.size(); i++) {
mapper.insert(list.get(i));
if (i%5000==4999) {
sqlSession.flushStatements();
// sqlSession.commit();
// sqlSession.clearCache();
}
}
// sqlSession.commit();
// sqlSession.clearCache();
sqlSession.flushStatements();
System.out.println("SqlSession 批量插入耗时:"+(System.currentTimeMillis()-start));
}
@Test
public void forEachInsert() {
System.out.println("forEachInsert 插入开始========");
long start = System.currentTimeMillis();
for (int i = 0; i < list.size(); i++) {
usersMapper.insert(list.get(i));
}
System.out.println("foreach 插入耗时:"+(System.currentTimeMillis()-start));
}
@Test
public void sqlInsert() {
System.out.println("sql 插入开始========");
long start = System.currentTimeMillis();
usersMapper.sqlInsert(list);
System.out.println("sql 插入耗时:"+(System.currentTimeMillis()-start));
}
//xml批量插入
@Test
public void xmlInsert() {
System.out.println("xmlInsert 批量插入开始========");
long start = System.currentTimeMillis();
usersMapper.xmlBatchInsert(list);
System.out.println("xmlInsert 批量插入耗时:"+(System.currentTimeMillis()-start));
}
}
//sql插入相关类
@Repository
public interface UsersMapper extends BaseMapper<Users> {
@InsertProvider(type = UsersProvider.class, method = "insertListSql")
public void sqlInsert(Listlist) ;
public void xmlBatchInsert(@Param("list") Listlist) ;
}
public class UsersProvider {
public String insertListSql(Listlist) {
StringBuffer sqlList = new StringBuffer();
sqlList.append(" INSERT INTO users(id,name,age,manager_id) VALUES ");
for (int i = 0; i < list.size() ; i++) {
Users user = list.get(i);
sqlList.append(" (").append(user.getId()).append(",").append("'").append(user.getName()).append("',").append(user.getAge())
.append(",").append(user.getManagerId()).append(")");
if (i < list.size()-1) {
sqlList.append(",");
}
}
return sqlList.toString();
}
}
mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.dao.UsersMapper">
<insert id="xmlBatchInsert">
INSERT INTO users(id,name,age,manager_id) VALUES
<foreach collection="list" item="item" index="index" separator=",">
(#{item.id},
#{item.name},
#{item.age},
#{item.managerId})
foreach>
insert>
mapper>
总结
推荐阅读:
面试官问:Mybatis Plus 是如何实现动态 SQL 语句的?原理你懂吗?
最近面试BAT,整理一份面试资料《Java面试BATJ通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。
朕已阅
评论