四.动态SQL
动态 SQL是MyBatis强大特性之一。极大的简化我们拼装
SQL的操作。
• 动态 SQL 元素和使用 JSTL 或其他类似基于 XML 的文本处
理器相似。
• MyBatis 采用功能强大的基于 OGNL 的表达式来简化操作。
-if:判断
-trim(where(封装查询条件),set(封装修改条件)):字符串截取,自定义规则
-choose(when,otherwise):分支选择,带了break的switch-case
-foreach:循环遍历
4.1 if
查询的时候如果某些条件前有and或者or之类会给拼装带来问题
方案1:where后面加1=1 ,以后的条件都and xxx
方案2:mybatis使用where标签来将所有的查询条件包括在内,
mybaits就会将where标签中拼装的sql,多出来的and或者or去掉
where只会去掉前置的and,or
ognl会进行字符串与数字的转换判断
<!--public List<Employee> getEmpsByConditionIf(Employee employee);-->
<select id="getEmpsByConditionIf" resultType="com.itlc.mybatis.bean.Employee">
SELECT * FROM tbl_employee
<!--where-->
<!--
查询的时候如果某些条件前有and或者or之类会给拼装带来问题
方案1:where后面加1=1 ,以后的条件都and xxx
方案2:mybatis使用where标签来将所有的查询条件包括在内,
mybaits就会将where标签中拼装的sql,多出来的and或者or去掉
where只会去掉前置的and,or
-->
<where>
<if test="id!=null">
id=#{id}
</if>
<if test="lastName!=null and lastName!=""">
AND last_name LIKE #{lastName}
</if>
<if test="email!=null and email.trim()!=""">
AND email=#{email}
</if>
<!--ognl会进行字符串与数字的转换判断-->
<if test="gender==0 or gender==1">
AND gender=#{gender}
</if>
</where>
</select>
4.2 trim
and在后面,用where标签没用,测试Trim
prefix:前缀,trim标签体是整个字符串拼串后的结果,
prefix给拼串后的整个字符串加一个前缀
prefixOverrides:前缀覆盖:去掉整个字符串前面多余的字符
suffix:后缀,给整个字符串后缀加一个后缀
suffixOverrides:后缀覆盖:去掉整个字符串后面多余的字符
<select id="getEmpsByConditionTrim" resultType="com.itlc.mybatis.bean.Employee">
SELECT * FROM tbl_employee
<!--自定义字符串截取规则-->
<trim prefix="where" suffixOverrides="and">
<if test="id!=null">
id=#{id} AND
</if>
<if test="lastName!=null and lastName!=""">
last_name LIKE #{lastName} AND
</if>
<if test="email!=null and email.trim()!=""">
email=#{email} AND
</if>
<if test="gender==0 or gender==1">
gender=#{gender}
</if>
</trim>
</select>
if与trim的使用实例
<!--public void updateEmp(Employee employee);
实现:带那列的值就更新那列-->
<update id="updateEmp">
UPDATE tbl_employee
<!--SET 用以下标签来代替set-->
<!--1.set标签的使用,自动检测多余的逗号-->
<!--<set>
<if test="lastName!=null">
last_name=#{lastName},
</if>
<if test="email!=null">
email=#{email},
</if>
<if test="gender==0 or gender==1">
gender=#{gender}
</if>
</set>
-->
<!--2.trim标签的使用-->
<trim prefix="set" suffixOverrides=",">
<if test="lastName!=null">
last_name=#{lastName},
</if>
<if test="email!=null">
email=#{email},
</if>
<if test="gender==0 or gender==1">
gender=#{gender}
</if>
</trim>
WHERE id=#{id}
</update>
4.3 choose
<!-- public List<Employee> getEmpsByConditionChoose(Employee employee);-->
<select id="getEmpsByConditionChoose" resultType="com.itlc.mybatis.bean.Employee">
SELECT * FROM tbl_employee
<where>
<!--如果带了id就用id查,带了lastName就用lastName查,只用一个查-->
<choose>
<when test="id!=null">
id=#{id}
</when>
<when test="lastName!=null">
last_name like #{lastName}
</when>
<when test="email!=email">
email=#{email}
</when>
<otherwise>
1=1
</otherwise>
</choose>
</where>
</select>
4.4 foreach
<!--public List<Employee> getEmpsByConditionForeach(List<Integer> items);-->
<select id="getEmpsByConditionForeach" resultType="com.itlc.mybatis.bean.Employee">
SELECT * FROM tbl_employee where id in
<!--
Collection:指定要遍历的集合
list类型的参数会特殊处理封装在map中,map的key就叫list
item:将当前遍历出的元素赋值给指定的变量
separator:每个元素之间的分隔符
open:遍历出所有结果拼接一个开始的字符
close:遍历出所有结果拼接一个结束的字符
index:索引。遍历list的时候index是索引,item为该索引对应的值
遍历map的时候index表示的就是map的key,item就是map的值
#{变量名}就能取出变量的值也就是当前遍历出的元素,每次循环取出一个元素
-->
<foreach collection="ids" item="item_id" separator="," open="(" close=")">
#{item_id}
</foreach>
</select>
<!--批量保存-->
<!--public void addEmps(@Param("emps")List<Employee> emps);-->
<!--MySQL下批量保存:可以foreach遍历,mysql支持value(),(),()语法,推荐使用这种-->
<insert id="addEmps">
INSERT into tbl_employee(last_name, gender, email, dept_id) VALUES
<foreach collection="emps" item="emp" separator=",">
(#{emp.lastName},#{emp.gender},#{emp.email},#{emp.dept.id})
</foreach>
</insert>
<!--若不支持,可以用多条sql语句拼装,若要使得mysql支持多语句拼装,需要再url后面设置allowMultiQueries
jdbc.url=jdbc:mysql://localhost:3306/mybatis1?allowMultiQueries=true-->
<!--<insert id="addEmps">
<foreach collection="emps" item="emp" separator=";">
INSERT into tbl_employee(last_name, gender, email, dept_id) VALUES
(#{emp.lastName},#{emp.gender},#{emp.email},#{emp.dept.id})
</foreach>
</insert>-->
4.5 两个内置参数
不只是方法传递过来的参数可以被用来判断,取值
mybatis默认的还有两个内置参数
_parameter:代表整个参数
单个参数:_parameter就是这个参数
多个参数:参数会被封装为一个map,_parameter就代表这个map
_databaseId:如果配置了databaseIdProvider标签
_databaseId就是代表当前数据库的别名
<!--bind:可以将OGNL表达式的值绑定到一个变量里面,方便后来引用这个变量的值-->
<!--public List<Employee> getEmpsByConditionBind(Employee employee);-->
<select id="getEmpsByConditionBind" resultType="com.itlc.mybatis.bean.Employee">
<bind name="_lastName" value="'%'+lastName+'%'"/>
SELECT * FROM tbl_employee WHERE last_name LIKE #{_lastName}
</select>
4.6 sql片段抽取
抽取可重用的sql片段,方便以后引用
1.sql抽取,经常讲要查询的列名,或者插入的列名抽取出来方便引用
2.include来引用已经抽取的sql
3.include还可以自定义一些property,sql标签内部就能使用自定义的属性,自定义标签使用方法:${prop}
<sql id="insertColunm">
last_name, gender, email, dept_id
</sql>
<!--引用方法-->
<!--
<include refid="insertColunm">
<property name="test" value="abc"/>
</include>
-->