Criteria
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
|
import
java.io.IOException;
import
java.io.Reader;
import
java.util.ArrayList;
import
java.util.List;
import
org.apache.ibatis.io.Resources;
import
org.apache.ibatis.session.SqlSession;
import
org.apache.ibatis.session.SqlSessionFactory;
import
org.apache.ibatis.session.SqlSessionFactoryBuilder;
import
org.apache.log4j.pattern.ClassNamePatternConverter;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import
cn.itcast.ssm.mapper.ItemsMapper;
import
cn.itcast.ssm.po.ItemsExample;
public
class
Student {
public
static
void main(
String
[] args) throws IOException {
/*方式一 */
ItemsExample itemsExample1 =
new
ItemsExample();
itemsExample1.
or
().andIdEqualTo(
5
).andNameIsNotNull();
itemsExample1.
or
().andPicEqualTo(
"xxx"
).andPicIsNull();
List<
Integer
> fieldValues =
new
ArrayList<
Integer
>();
fieldValues.add(
8
);
fieldValues.add(
11
);
fieldValues.add(
14
);
fieldValues.add(
22
);
itemsExample1.
or
().andIdIn(fieldValues);
itemsExample1.
or
().andIdBetween(
5
,
9
);
/* 方式二 criteria1与criteria2是or的关系 */
ItemsExample itemsExample2 =
new
ItemsExample();
ItemsExample.Criteria criteria1 = itemsExample2.createCriteria();
criteria1.andIdIsNull();
criteria1.andPriceEqualTo((float)
3
);
ItemsExample.Criteria criteria2 = itemsExample2.createCriteria();
criteria2.andNameIsNull();
criteria2.andIdGreaterThanOrEqualTo(
5
);
itemsExample2.
or
(criteria2);
//方式一和方式二是等价的
// spring获取mapper代理对象
ApplicationContext applicationContext =
new
ClassPathXmlApplicationContext(
"classpath:applicationContext.xml"
);
ItemsMapper itemsMapper = (ItemsMapper) applicationContext.getBean(
"itemsMapper"
);
itemsMapper.countByExample(itemsExample2);
// 获取SqlSessionFactory
String
resource =
"SqlMapConfig.xml"
;
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sqlMapper =
new
SqlSessionFactoryBuilder().build(reader);
// 获取SqlSession
SqlSession sqlSession = sqlMapper.openSession();
}
}
|
1
|
<
if
test
=
"_parameter != null"
>
|
1
|
<
if
test
=
"name != null"
>
|
1
|
int
updateByExample(
@Param
(
"user"
) User user,
@Param
(
"example"
) UserExample example);
|
1
2
3
4
5
6
7
|
<
update
id
=
"updateByExample"
parameterType
=
"map"
>
update tb_user
set id = #{user.id,jdbcType=INTEGER},
...
<
if
test
=
"_parameter != null"
>
<
include
refid
=
"Update_By_Example_Where_Clause"
/>
if
>
|