07. mybatis 动态 sql
MyBatis 的一个强大的特性之一通常是它的动态 SQL 能力。 如果你有使用JDBC 或其他 相似框架的经验,你就明白条件地串联 SQL 字符串在一起是多么的痛苦,确保不能忘了空 格或在列表的最后省略逗号。动态 SQL 可以彻底处理这种痛苦。
常用的动态sql标签
- if
- where
- trim
- set
- choose(when,otherwise)
- foreach
1、if元素
if标签可以对传入的条件进行判断
如下案例:判断是否传入id,若传入则id查询,若不传入则全部查询。
<select id="findByTerm" parameterType="map" resultMap="userResult">
select
id,user_name,password,reg_time
from
t_user
<if test="id!=null && id!='' && id !=0">
where id=#{id}
</if>
</select>
测试:
public void testFindByTerm() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
UserDao userDao = sqlSession.getMapper(UserDao.class);
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", "4");
List<UserPo> userPos = userDao.findByTerm(map);
logger.debug(userPos.toString());
}
2、where元素
对于查询条件个数不确定的情况,可使用<where>元素。
<select id="findByTerm" parameterType="map" resultMap="userResult">
select
id,user_name,password,reg_time
from
t_user
<where >
<if test="id!=null && id!='' && id !=0">
id=#{id}
</if>
<if test="userName!=null && userName!='' ">
and user_name=#{userName}
</if>
</where>
</select>
<where>元素会进行判断,如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头,它会剔除掉AND或OR。
测试:
public void testFindByTerm() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
UserDao userDao = sqlSession.getMapper(UserDao.class);
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", "4");
map.put("userName", "张三");
List<UserPo> userPos = userDao.findByTerm(map);
logger.debug(userPos.toString());
}
3、trim元素
where标签,其实用trim 也可以表示,当WHERE后紧随AND或则OR的时候,就去除AND或者OR。prefix前缀,prefixOverrides覆盖首部指定内容
<select id="findByTerm" parameterType="map" resultMap="userResult">
select
id,user_name,password,reg_time
from
t_user
<trim prefix="where" prefixOverrides="and|or" >
<if test="id!=null && id!='' && id !=0">
id=#{id}
</if>
<if test="userName!=null && userName!='' ">
and user_name=#{userName}
</if>
</trim>
</select>
测试:
public void testFindByTerm() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
UserDao userDao = sqlSession.getMapper(UserDao.class);
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", "4");
map.put("userName", "张三");
List<UserPo> userPos = userDao.findByTerm(map);
logger.debug(userPos.toString());
}
4、set元素
set标签,可以在需要更新的语句中,通过添加判断条件更新需要的指定列。
同时也可以把最后的一个逗号去掉。
<update id="updatePortion" parameterType="UserPo" >
update t_user
<set>
<if test="userName != null && userName != ''">user_name = #{userName},</if>
<if test="password != null && password != ''">password = #{password},</if>
<if test="regTime != null && regTime != ''">reg_time = #{regTime}</if>
</set>
where id = #{id}
</update>
5、foreach元素
foreach标签,可以循环遍历指定的数组或者集合。
如下案例:批量删除
<delete id="batchDelete">
delete from t_user
where id in
<foreach collection="array" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
测试:
public void testBatchDelete() {
SqlSession sqlSession = null;
try {
sqlSession = MyBatisUtil.getSqlSession();
UserDao userDao = sqlSession.getMapper(UserDao.class);
String id="4,6,";
String[] idList = id.split(",");
userDao.batchDelete(idList);
sqlSession.close();
} catch (Exception e) {
// TODO: handle exception
} finally {
sqlSession.close();
}
}
6、XML中的特殊字符
如果 mybatis 使用 XML 配置,不可避免会遇到对一些XML来说的特殊字符。
字符 | 对应的XML | 含义 |
---|---|---|
< | < | 小于 |
> | &rt; | 大于 |
& | & | 和 |
' | ' | 单引号 |
" | " | 双引号 |