Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.example.demo.mybatis;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.IdUtil;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* <p>
*
* </p>
*
* @author shyane
* @date 2019/12/9
*/
@Service
public class BatchService {
private SqlSession sqlSession;
public BatchService(SqlSessionFactory sqlSessionFactory) {
sqlSession = new SqlSessionTemplate(sqlSessionFactory, ExecutorType.BATCH);
}
public int batchInsertCity(List<Entity> entities) {
EntityMapper entityMapper = sqlSession.getMapper(EntityMapper.class);
//批量保存执行前时间
long start = System.currentTimeMillis();
for (Entity entity : entities) {
entity.setId(IdUtil.simpleUUID());
entity.setName("ces");
entityMapper.insert(entity);
}
long end = System.currentTimeMillis();
long time = end - start;
//批量保存执行后的时间
Console.log("批量执行时长" + time);
return cities.size();
}
}