动态sql
Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它存在的意义是为了解决拼接SQL语 句字符串时的痛点问题。
if标签
DynamicSQLMapper.java
java
package com.light.mybatis.mappers;
import com.light.mybatis.pojo.User;
import java.util.List;
public interface DynamicSQLMapper {
/**
* 这里返回值有可能是多个,也可能是一个。所以返回值是List<User>
* @return
*/
List<User> getUserListConditionForIf(User user);
}
DynamicSQLMapper.xml
xml
<?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="com.light.mybatis.mappers.DynamicSQLMapper">
<select id="getUserListConditionForIf" resultType="user">
<!-- 1 = 1 为了防止第一个if不成立,sql会拼成 ... where and ...-->
select * from user where 1 = 1
<!--
test 里面的account变量来自于调用mapper方法参数传过来的对象,只不过没有加#{}
test 中不支持 && ||, 但是可以是用and, or
-->
<if test="account != null and account != ''">
<!--拼接sql-->
and account = #{account}
</if>
<if test="age > 5">
and age = #{age}
</if>
</select>
</mapper>
生成sql:select * from user WHERE account = ? and age = ?
where标签
where 元素只会在子元素返回任何内容的情况下才插入 "WHERE" 子句。而且,若子句的开头 为 "AND" 或 "OR",where 元素也会将它们去除。
xml
<select id="getUserListConditionForIf" resultType="user">
select * from user
<!--自动加上where-->
<where>
<if test="account != null and account != ''">
<!--
where标签里可以去掉子句开头的and或者or,所以使用where标签and和or通常写在开头,
这样写account = #{account} and,如果后面的条件不成立,就会多出来个and,where标签也不会去掉,会报错。
-->
and account = #{account}
</if>
<if test="age > 5">
and age = #{age}
</if>
</where>
</select>
生成sql:select * from user WHERE account = ? and age = ?
trim标签
若标签中有内容时:
prefix|suffix:将trim标签中内容前面或后面添加指定内容 ,|隔开可以添加多个内容suffixoverrides|prefixoverrides:将trim标签中内容前面或后面去掉指定内容 ,|隔开可以添加多个内容- 标签中没有内容时,
trim标签也没有任何效果
xml
<select id="getUserListConditionForIf" resultType="user">
select * from user
<trim prefix="where" suffixOverrides="and|or">
<if test="account != null and account != ''">
account = #{account}
</if>
<if test="age > 5">
and age = #{age} and
</if>
</trim>
</select>
choose, when, otherwise标签
choose,when,otherwise相当于if...else if...else。和if的区别就是只会执行一个when或者otherwise里面的内容。if标签都要执行,所有if需要写and或者or。而when...otherwise不需要写。
choose就是一个父标签,表示一套when,otherwise集合when表示if...else if...otherwise表示else...
xml
<select id="getUserListConditionForIf" resultType="user">
select * from user
<where>
<choose>
<when test="account != null and account != ''">
account = #{account}
</when>
<when test="name != null and name != ''">
name = #{name}
</when>
<otherwise>
id = 1
</otherwise>
</choose>
</where>
</select>
生成sql:select * from user WHERE account = ?
foreach标签
xml
<delete id="deleteMoreUser">
delete from user where id in
<!--
foreach标签
collection: 集合
item: 集合中每一个元素
separator: 每一个#{id}之间的分隔符
open:以什么开始
close:以什么结束
下面foreach 出来的结果就是(1,2,3)
-->
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
<delete id="deleteMoreUser1">
delete from user where
<!-- 结果为 id=1 or id=2 or id=3 -->
<foreach collection="ids" item="id" separator="or">
id=#{id}
</foreach>
</delete>
sql标签
设置SQL片段: <sql id="empcolumns">id,emp_name,age,sex,email</sql>
引用SQL片段: <include refid="empcolumns"></include>
xml
<sql id="usercolumns">id,emp_name,age,sex,email</sql>
<select id="getUserListConditionForIf" resultType="user">
select <include refid="usercolumns"></include> from user
<!--自动加上where-->
<where>
<choose>
<when test="account != null and account != ''">
account = #{account}
</when>
<when test="name != null and name != ''">
name = #{name}
</when>
<otherwise>
id = 1
</otherwise>
</choose>
</where>
</select>