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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.example.demo;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import com.example.demo.mybatis.BatchService;
import com.example.demo.mybatis.Entity;
import com.example.demo.mybatis.EntityMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SpringBootTest
class DemoApplicationTests {
@Resource
private EntityMapper entityMapper;
@Autowired
private BatchService batchService;
// 测试类型转换
@Test
void contextLoads() {
Entity entity = new Entity();
Map<String, Object> context = new HashMap<>();
context.put("111", "111");
context.put("222", "222");
context.put("333", "333");
context.put("444", "444");
entity.setId(IdUtil.simpleUUID())
.setName(RandomUtil.randomString(4))
.setContext(context);
entityMapper.insert(entity);
}
@Test
void select() {
Entity id = entityMapper.findById("de7b193a8d6aa6aw73w46a5vb6");
Console.log(id);
Console.log(JSONUtil.toJsonStr(id));
}
// 测试批量处
@Test
void batch() {
List<Entity> entities = new ArrayList<Entity>();
for (int i = 0; i < 10000; i++) {
Entity entity = new Entity();
entity.setName(RandomUtil.randomString("ces测试", 4));
entities.add(entity);
}
batchService.batchInsertEntity(entities);
this.noBath(entities);
}
void noBath(List<Entity> entities) {
//批量保存执行前时间
long start = System.currentTimeMillis();
for (Entity entity : entities) {
entity.setId(IdUtil.simpleUUID());
entityMapper.insert(entity);
}
long end = System.currentTimeMillis();
long time2 = end - start;
//批量保存执行后的时间
Console.log("执行时长" + time2);
}
}