-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
SqlCriteriaHelperTest.java
113 lines (98 loc) · 4.43 KB
/
SqlCriteriaHelperTest.java
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
package tk.mybatis.mapper.weekend;
import org.apache.ibatis.session.SqlSession;
import org.junit.Assert;
import org.junit.Test;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.entity.Country;
import tk.mybatis.mapper.weekend.mapper.CountryMapper;
import java.util.ArrayList;
import java.util.List;
/**
* @author Cheng.Wei
*/
public class SqlCriteriaHelperTest {
/**
* 忽略null值问题
*/
@Test
public void ignore() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
List<Country> selectBySqlCriteriaHelper= mapper.selectByExample(new Example.Builder(Country.class)
.where(SqlCriteriaHelper.custom(Country.class)
.andEqualTo(Country::getCountryname, null)
.andLike(Country::getCountryname, "China")).build());
List<Country> selectByWeekendSqls = mapper.selectByExample(new Example.Builder(Country.class)
.where(WeekendSqls.<Country>custom()
.andEqualTo(Country::getCountryname, null)
.andLike(Country::getCountrycode, "China")).build());
} finally {
sqlSession.close();
}
}
/**
* 不忽略null属性
* 当属性为null 且不忽略 则转换查询 equal null 转 is null
*/
@Test
public void required() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
List<Country> selectBySqlCriteriaHelper= mapper.selectByExample(new Example.Builder(Country.class)
.where(SqlCriteriaHelper.custom(Country.class)
// required = true 则继续查询
.andEqualTo(Country::getCountryname, null, true)).build());
List<Country> selectByWeekendSqls = mapper.selectByExample(new Example.Builder(Country.class)
.where(WeekendSqls.<Country>custom()
.andEqualTo(Country::getCountryname, null)).build());
} finally {
sqlSession.close();
}
}
/**
* like查询 自动拼接 %
*/
@Test
public void like() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
List<Country> selectBySqlCriteriaHelper= mapper.selectByExample(new Example.Builder(Country.class)
.where(SqlCriteriaHelper.custom(Country.class)
.andLike(Country::getCountryname, "Chin")
.orLike(Country::getCountryname, "A")).build());
List<Country> selectByWeekendSqls = mapper.selectByExample(new Example.Builder(Country.class)
.where(WeekendSqls.<Country>custom()
.andLike(Country::getCountryname, "Chin")
.orLike(Country::getCountryname, "A")).build());
//判断两个结果数组内容是否相同
Assert.assertArrayEquals(selectBySqlCriteriaHelper.toArray(), selectByWeekendSqls.toArray());
} finally {
sqlSession.close();
}
}
/**
* in查询 空集合问题
*/
@Test
public void list() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
List<Country> selectBySqlCriteriaHelper= mapper.selectByExample(new Example.Builder(Country.class)
.where(SqlCriteriaHelper.custom(Country.class)
.andIn(Country::getCountryname, new ArrayList())
.orLike(Country::getCountryname, "A")).build());
List<Country> selectByWeekendSqls = mapper.selectByExample(new Example.Builder(Country.class)
.where(WeekendSqls.<Country>custom()
.andIn(Country::getCountryname, new ArrayList())
.orLike(Country::getCountryname, "A")).build());
//判断两个结果数组内容是否相同
Assert.assertArrayEquals(selectBySqlCriteriaHelper.toArray(), selectByWeekendSqls.toArray());
} finally {
sqlSession.close();
}
}
}