动态SQL就是动态的生成SQL。
if标记
假设有这样一种需求:查询用户,当用户名不等于“admin”的时候,我们还需要密码为123456。
数据库中的数据为:
MyBatisConfig.xml
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
|
<?xml version= "1.0" encoding= "UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd" >
<configuration>
<!--定义别名 注意typeAliases一定要在environments之前-->
<typeAliases>
<typeAlias type= "jike.book.pojo.JiKeUser" alias= "JiKeUser" />
<typeAlias type= "jike.book.pojo.Author" alias= "Author" />
</typeAliases>
<environments default = "development" >
<environment id= "development" >
<transactionManager type= "JDBC" >
</transactionManager>
<dataSource type= "POOLED" >
<property name= "driver" value= "com.mysql.jdbc.Driver" />
<property name= "url" value= "jdbc:mysql://localhost:3306/jikebook" />
<property name= "username" value= "root" />
<property name= "password" value= "*****" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource= "jike/book/map/jikeUser.xml" />
</mappers>
</configuration>
|
JiKeUser.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?xml version= "1.0" encoding= "UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace= "/" >
<select id= "selectSQL" resultType= "JiKeUser" parameterType= "JiKeUser" >
SELECT * FROM jikebook.jikeuser
WHERE 1=1
< if test= "userName!='admin'" >
AND password= #{password}
</ if >
</select>
</mapper>
|
测试类:
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
|
package jike.book.test;
import jike.book.pojo.JiKeUser;
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 java.io.IOException;
import java.io.Reader;
import java.util.List;
/**
* DateTime: 2016/9/6 13:36
* 功能:
* 思路:
*/
public class TestSQL {
public static void main(String[] args) {
// 资源路径
String resource= "jike/book/map/MyBatisConfig.xml" ;
Reader reader= null ;
SqlSession session;
try {
reader= Resources.getResourceAsReader(resource);
} catch ( IOException e ) {
e.printStackTrace();
}
SqlSessionFactory sqlMapper= new SqlSessionFactoryBuilder().build(reader);
session=sqlMapper.openSession();
JiKeUser jiKeUser= new JiKeUser();
jiKeUser.setPassword( "123456" );
List<JiKeUser> userList=session.selectList( "selectSQL" ,jiKeUser);
for ( JiKeUser user:userList ) {
System.out.println( "userName:" +user.getUserName());
}
session.close();
}
}
|
运行结果为:
choose标记
假设我们当前有这么一个需求:查询用户,如果用户名不为空就加上用户名这个条件,如果id不为空也加上id这个条件,否则的话就是设置密码不为空,这就是一个多路选择。
MyBatisConfig.xml不改变,在JikeUser.xml中加上:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<select id= "selectJiKeUserChoose" resultType= "JiKeUser" parameterType= "JiKeUser" >
select * from jikeuser where 1 = 1
<choose>
<when test= "userName!=null" >
and userName like #{userName}
</when>
<when test= "id!=0" >
and id =#{id}
</when>
<otherwise>
and password is not null
</otherwise>
</choose>
</select>
|
测试类:假设用户名不为空:
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
|
package jike.book.test;
import jike.book.pojo.JiKeUser;
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 java.io.IOException;
import java.io.Reader;
import java.util.List;
/**
* DateTime: 2016/9/6 13:36
* 功能:
* 思路:
*/
public class TestSQL {
public static void main(String[] args) {
// 资源路径
String resource= "jike/book/map/MyBatisConfig.xml" ;
Reader reader= null ;
SqlSession session;
try {
reader= Resources.getResourceAsReader(resource);
} catch ( IOException e ) {
e.printStackTrace();
}
SqlSessionFactory sqlMapper= new SqlSessionFactoryBuilder().build(reader);
session=sqlMapper.openSession();
JiKeUser jiKeUser= new JiKeUser();
jiKeUser.setUserName( "YEN" );
List<JiKeUser> userList=session.selectList( "selectJiKeUserChoose" ,jiKeUser);
for ( JiKeUser user:userList ) {
System.out.println( "userName:" +user.getUserName());
}
session.close();
}
}
|
结果为:
假设不设置用户名这个条件,即注释掉jiKeUser.setUserName("YEN");:
where标记、set标记
上面我们在choose中查询是不能确定子连接条件中的and是写还是不写,因此加了一个1=1.而where会只能的去判断该不该加。
1
2
3
4
5
6
7
8
9
10
11
|
< select id= "selectJiKeUserWhere" resultType= "JiKeUser" parameterType= "JiKeUser" >
select * from jikeuser
< where >
<if test= "userName!=null" >
and userName like #{userName}
</if>
<if test= "id!=null" >
and id =#{id}
</if>
</ where >
</ select >
|
set标记智能赋值,会自动去掉多余的”,”。
1
2
3
4
5
6
7
8
|
<update id= "updateJiKeUserSet" parameterType= "JiKeUser" >
update JiKeUser
<set>
< if test= "userName != null" >userName=#{userName},</ if >
< if test= "password != null" >password=#{password},</ if >
</set>
where id=#{id}
</update>
|
操作之前的数据:
操作:
操作结果:
1
2
3
4
5
6
7
8
9
10
11
|
<update id= "updateUserTrim" parameterType= "JiKeUser" >
UPDATE JiKeUser
<trim prefix= "SET" suffixOverrides= "," suffix= "WHERE id = #{id}" >
< if test= "userName != null and userName != '' " >
userName = #{userName},
</ if >
< if test= "password != null and password != '' " >
password=#{password},
</ if >
</trim>
</update>
|
foreach标记
通常用于循环查询或循环赋值
1
2
3
4
5
6
7
8
9
10
|
<select id= "selectJiKeUserForeach" resultType= "JiKeUser" parameterType= "list" >
select * from jikeuser
<where>
id in
<foreach item= "item" index= "index" collection= "list"
open= "(" separator= "," close= ")" >
#{item}
</foreach>
</where>
</select>
|
测试:
以上所述是小编给大家介绍的Mybatis动态SQL之if、choose、where、set、trim、foreach标记实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://blog.csdn.net/yen_csdn/article/details/52449436