MyBatis动态SQL详解
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑。
MyBatis中用于实现动态SQL的元素主要有:
- if
- choose(when,otherwise)
- trim
- where
- set
- foreach
< 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 >
|
< 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 >
|
< 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 >
|
< 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 >
|
< 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 >
|
- 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
- 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
- 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key
< 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 >
|
public List<Blog> dynamicForeachTest(List<Integer> ids);
|
@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();
|
} |
< 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 >
|
public List<Blog> dynamicForeach2Test( int [] ids);
|
@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();
|
} |
< 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 >
|
public List<Blog> dynamicForeach3Test(Map<String, Object> params);
|
@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();
|
} |
使用下面的方法,让mybatis3实现批量插入数据:
insert into tb(aa, bb)values("aa","bb"),("cc","dd"),("ee","ff")
代码如下所示:
import java.util.List;public class Class {
private int classId;
private String className;private List<Student> lists;public int getClassId() {
return classId;
}public void setClassId(int classId) {
this.classId = classId;
}public String getClassName() {
return className;
}public void setClassName(String className) {
this.className = className;
}public List<Student> getLists() {
return lists;
}public void setLists(List<Student> lists) {
this.lists = lists;
}
}
public class Student {
private int userId;
private String userName;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}查看代码 打印1.<!-- 批量插入例子 -->
<insert id="saveClass" parameterType="classInfo">
insert into class_student(classId, className, userId, userName, createTime)
values
<foreach collection="lists" item="item" index="index" separator=",">
(#{classId},#{className},#{item.userId},#{item.userName},now())
</foreach>
</insert>