|
@@ -0,0 +1,57 @@
|
|
1
|
+import com.mapper.SqlMapper;
|
|
2
|
+import com.mapper.UserMapper;
|
|
3
|
+import com.pojo.entity.UserEntity;
|
|
4
|
+import org.apache.ibatis.io.Resources;
|
|
5
|
+import org.apache.ibatis.session.SqlSession;
|
|
6
|
+import org.apache.ibatis.session.SqlSessionFactory;
|
|
7
|
+import org.apache.ibatis.session.SqlSessionFactoryBuilder;
|
|
8
|
+import org.junit.Before;
|
|
9
|
+import org.junit.Test;
|
|
10
|
+
|
|
11
|
+import java.io.IOException;
|
|
12
|
+import java.io.InputStream;
|
|
13
|
+import java.util.Arrays;
|
|
14
|
+import java.util.HashMap;
|
|
15
|
+import java.util.List;
|
|
16
|
+import java.util.Map;
|
|
17
|
+
|
|
18
|
+public class TestSqlMapper {
|
|
19
|
+ SqlMapper sqlMapper;
|
|
20
|
+ @Before
|
|
21
|
+ public void before() throws IOException {
|
|
22
|
+ InputStream resourceAsStream = Resources.getResourceAsStream("mybatisConfig.xml");
|
|
23
|
+ SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
|
|
24
|
+ SqlSession sqlSession = build.openSession(true);
|
|
25
|
+ sqlMapper = sqlSession.getMapper(SqlMapper.class);
|
|
26
|
+ }
|
|
27
|
+
|
|
28
|
+ /**
|
|
29
|
+ * 模糊查询
|
|
30
|
+ */
|
|
31
|
+ @Test
|
|
32
|
+ public void testLike() {
|
|
33
|
+ List<UserEntity> na = sqlMapper.getUserByLike("na");
|
|
34
|
+ }
|
|
35
|
+ @Test
|
|
36
|
+ public void testList() {
|
|
37
|
+ List<Integer> integers = Arrays.asList(1, 2);
|
|
38
|
+ List<UserEntity> na = sqlMapper.getUserByList(integers);
|
|
39
|
+ }
|
|
40
|
+ @Test
|
|
41
|
+ public void testTableName() {
|
|
42
|
+ List<UserEntity> na = sqlMapper.getUserByTableName("user");
|
|
43
|
+ }
|
|
44
|
+
|
|
45
|
+ /**
|
|
46
|
+ * 插入获取自增主键
|
|
47
|
+ */
|
|
48
|
+ @Test
|
|
49
|
+ public void addUserGeneratedKey() {
|
|
50
|
+ UserEntity userEntity = new UserEntity();
|
|
51
|
+ userEntity.setName("张贤胜");
|
|
52
|
+ userEntity.setAge(27);
|
|
53
|
+ userEntity.setAddress("地址");
|
|
54
|
+ int i = sqlMapper.insertGeneratedKey(userEntity);
|
|
55
|
+
|
|
56
|
+ }
|
|
57
|
+}
|