MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑。
MyBatis中用于实现动态SQL的元素主要有:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<
select
id
=
"dynamicIfTest"
parameterType
=
"Blog"
resultType
=
"Blog"
>
select * from t_blog where 1 = 1
<
if
test
=
"title != null"
>
and title = #{title}
</
if
>
<
if
test
=
"content != null"
>
and content = #{content}
</
if
>
<
if
test
=
"owner != null"
>
and owner = #{owner}
</
if
>
</
select
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<
select
id
=
"dynamicChooseTest"
parameterType
=
"Blog"
resultType
=
"Blog"
>
select * from t_blog where 1 = 1
<
choose
>
<
when
test
=
"title != null"
>
and title = #{title}
</
when
>
<
when
test
=
"content != null"
>
and content = #{content}
</
when
>
<
otherwise
>
and owner = "owner1"
</
otherwise
>
</
choose
>
</
select
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<
select
id
=
"dynamicWhereTest"
parameterType
=
"Blog"
resultType
=
"Blog"
>
select * from t_blog
<
where
>
<
if
test
=
"title != null"
>
title = #{title}
</
if
>
<
if
test
=
"content != null"
>
and content = #{content}
</
if
>
<
if
test
=
"owner != null"
>
and owner = #{owner}
</
if
>
</
where
>
</
select
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<
select
id
=
"dynamicTrimTest"
parameterType
=
"Blog"
resultType
=
"Blog"
>
select * from t_blog
<
trim
prefix
=
"where"
prefixOverrides
=
"and |or"
>
<
if
test
=
"title != null"
>
title = #{title}
</
if
>
<
if
test
=
"content != null"
>
and content = #{content}
</
if
>
<
if
test
=
"owner != null"
>
or owner = #{owner}
</
if
>
</
trim
>
</
select
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<
update
id
=
"dynamicSetTest"
parameterType
=
"Blog"
>
update t_blog
<
set
>
<
if
test
=
"title != null"
>
title = #{title},
</
if
>
<
if
test
=
"content != null"
>
content = #{content},
</
if
>
<
if
test
=
"owner != null"
>
owner = #{owner}
</
if
>
</
set
>
where id = #{id}
</
update
>
|
1
2
3
4
5
6
7
|
<
select
id
=
"dynamicForeachTest"
resultType
=
"Blog"
>
select * from t_blog where id in
<
foreach
collection
=
"list"
index
=
"index"
item
=
"item"
open
=
"("
separator
=
","
close
=
")"
>
#{item}
</
foreach
>
</
select
>
|
1
|
public
List<Blog> dynamicForeachTest(List<Integer> ids);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@Test
public
void
dynamicForeachTest() {
SqlSession session = Util.getSqlSessionFactory().openSession();
BlogMapper blogMapper = session.getMapper(BlogMapper.
class
);
List<Integer> ids =
new
ArrayList<Integer>();
ids.add(
1
);
ids.add(
3
);
ids.add(
6
);
List<Blog> blogs = blogMapper.dynamicForeachTest(ids);
for
(Blog blog : blogs)
System.out.println(blog);
session.close();
}
|
1
2
3
4
5
6
|
<
select
id
=
"dynamicForeach2Test"
resultType
=
"Blog"
>
select * from t_blog where id in
<
foreach
collection
=
"array"
index
=
"index"
item
=
"item"
open
=
"("
separator
=
","
close
=
")"
>
#{item}
</
foreach
>
</
select
>
|
1
|
public
List<Blog> dynamicForeach2Test(
int
[] ids);
|
1
2
3
4
5
6
7
8
9
10
|
@Test
public
void
dynamicForeach2Test() {
SqlSession session = Util.getSqlSessionFactory().openSession();
BlogMapper blogMapper = session.getMapper(BlogMapper.
class
);
int
[] ids =
new
int
[] {
1
,
3
,
6
,
9
};
List<Blog> blogs = blogMapper.dynamicForeach2Test(ids);
for
(Blog blog : blogs)
System.out.println(blog);
session.close();
}
|
1
2
3
4
5
6
7
|
<
select
id
=
"dynamicForeach3Test"
resultType
=
"Blog"
>
select * from t_blog where title like "%"#{title}"%" and id in
<
foreach
collection
=
"ids"
index
=
"index"
item
=
"item"
open
=
"("
separator
=
","
close
=
")"
>
#{item}
</
foreach
>
</
select
>
|
1
|
public
List<Blog> dynamicForeach3Test(Map<String, Object> params);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@Test
public
void
dynamicForeach3Test() {
SqlSession session = Util.getSqlSessionFactory().openSession();
BlogMapper blogMapper = session.getMapper(BlogMapper.
class
);
final
List<Integer> ids =
new
ArrayList<Integer>();
ids.add(
1
);
ids.add(
2
);
ids.add(
3
);
ids.add(
6
);
ids.add(
7
);
ids.add(
9
);
Map<String, Object> params =
new
HashMap<String, Object>();
params.put(
"ids"
, ids);
params.put(
"title"
,
"中国"
);
List<Blog> blogs = blogMapper.dynamicForeach3Test(params);
for
(Blog blog : blogs)
System.out.println(blog);
session.close();
}
|