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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com;
import cn.hutool.core.io.IoUtil;
import cn.hutool.json.JSONObject;
import com.cesgroup.mybatisHomework.entity.Person;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class TestOne {
private SqlSessionFactory sqlSessionFactory = null;
private SqlSession sqlSession = null;
@Before
public void configMybatis() {
// 指定全局配置文件
String resource = "mybatis-config.xml";
// 读取配置文件
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSession = sqlSessionFactory.openSession(false);
} catch (Exception e) {
e.printStackTrace();
} finally {
IoUtil.close(inputStream);
}
}
/**
* TypeHandler查询
*/
@Test
public void selectTest() {
Person person = sqlSession.selectOne("MyMapper.selectPerson", 2);
System.out.println(person);
sqlSession.close();
}
/**
* TypeHandler新增
*/
@Test
public void insertTest() {
Person person = new Person();
person.setId("aaa");
person.setName("张三");
JSONObject userInfObject = new JSONObject();
userInfObject.put("sex", "男");
userInfObject.put("birthday", "1999-12-12");
person.setInfo(userInfObject);
int a = sqlSession.insert("MyMapper.insert", person);
sqlSession.commit();
System.out.println("影响的行数:" + a);
}
/**
* 批量插入1
*/
@Test
public void batchInsert1() {
long begin = System.currentTimeMillis();
List<Person> list = new ArrayList<>();
for (int i = 1; i < 10000; i++) {
Person person = new Person();
person.setId("a" + i);
person.setName("李四" + i);
JSONObject userInfObject = new JSONObject();
userInfObject.put("sex", "男");
userInfObject.put("birthday", "1999-12-12");
person.setInfo(userInfObject);
list.add(person);
}
sqlSession.insert("MyMapper.insertBatch", list);
sqlSession.commit();
long end = System.currentTimeMillis();
System.out.println("##############耗时:" + (end - begin) + " 毫秒");
}
/**
* 批量插入2
*/
@Test
public void batchInsert2() {
sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
long begin = System.currentTimeMillis();
for (int i = 1; i < 10000; i++) {
Person person = new Person();
person.setId("b" + i);
person.setName("王五" + i);
JSONObject userInfObject = new JSONObject();
userInfObject.put("sex", "男");
userInfObject.put("birthday", "1999-12-12");
person.setInfo(userInfObject);
int a = sqlSession.insert("MyMapper.insert", person);
}
sqlSession.commit();
long end = System.currentTimeMillis();
System.out.println("##############耗时:" + (end - begin) + " 毫秒");
}
}