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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.cesgroup.mybatiswork;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
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 com.alibaba.fastjson.JSONObject;
import com.cesgroup.mybatiswork.entity.User;
/**
* @author chuwanshun
*
*/
public class AppTest
{
private SqlSession sqlSession=null;
private SqlSessionFactory sqlSessionFactory;
@Before
public void configMybatis() {
// 指定全局配置文件
String resource = "mybatis-config.xml";
// 读取配置文件
InputStream inputStream;
try {
inputStream = Resources.getResourceAsStream(resource);
// 构建sqlSessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 获取sqlSession
// sqlSession= sqlSessionFactory.openSession(ExecutorType.BATCH,false);
sqlSession= sqlSessionFactory.openSession(false);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(sqlSession!=null) {
//sqlSession.close();
}
}
}
/**
*
* 自定义解析器查询测试
*
* json串字段为 info
*/
@Test
public void testSelectUser() {
User user = sqlSession.selectOne("MyMapper.selectUser", 2);
System.out.println(user);
sqlSession.close();
}
/**
*
* 自定义类型解析器插入测试
*/
@Test
public void testInsertJsonObject() {
JSONObject userInfObject = new JSONObject();
userInfObject.put("address", "上海市静安区");
userInfObject.put("telphone", "13823541230");
User user = new User();
user.setId(3);
user.setName("万建斌");
user.setInfo(userInfObject);
int a=sqlSession.insert("MyMapper.insert",user);
sqlSession.commit();
System.out.println("影响的行数:"+a);
}
/**
* 批量插入方法一(经测试该方法较快)
* 通过list方式批量插入
* 500条 378ms
* 5000条615ms
* 10000条897ms
*/
@Test
public void testInsertBatch() {
long start = System.currentTimeMillis();
List<User> list = new ArrayList<>();
for (int i = 1; i < 10000; i++) {
JSONObject userInfObject = new JSONObject();
userInfObject.put("address", "上海市静安区");
userInfObject.put("telphone", "13823541230");
User user = new User();
user.setId(i);
user.setName("万建斌"+i);
user.setInfo(userInfObject);
list.add(user);
}
int a=sqlSession.insert("MyMapper.insertBatch",list);
sqlSession.commit();
long end = System.currentTimeMillis();
System.out.println("---------------" + "批量插入耗时一:"+(end - start) + "---------------");
}
/**
* 批量插入方法二
* 通过改变executor类型
*
* 500条 约375ms
* 5000条约912ms
* 10000条 1527ms
*/
@Test
public void testInsertBatchTwo() {
sqlSession= sqlSessionFactory.openSession(ExecutorType.BATCH,false);
long start = System.currentTimeMillis();
for (int i = 1; i < 10000; i++) {
JSONObject userInfObject = new JSONObject();
userInfObject.put("address", "上海市静安区");
userInfObject.put("telphone", "13823541230");
User user = new User();
user.setId(i);
user.setName("万建斌"+i);
user.setInfo(userInfObject);
int a=sqlSession.insert("MyMapper.insert",user);
}
sqlSession.commit();
long end = System.currentTimeMillis();
System.out.println("---------------" + "批量插入耗时二:"+(end - start) + "---------------");
}
}