一. 动态SQL
Mybatis 框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它存在的意义是为了解决拼接SQL语句字符串时的痛点问题。
1、if标签:判断功能
if标签可通过test属性的表达式进行判断,若表达式的结果为ture,则标签中的内容会执行;反之标签中的内容不会执行。
a.添加恒成立条件
在 where后边加一个恒成立的条件,比如:“1=1” 不影响查询结果
- 情况一:where后多出一个“and”
- 情况二:"所有查询条件都不成立,就多了where”:也应使用添加恒成立条件
在使用Mybatis的if标签时,如果判断条件不成立,SQL语句是不会显示的
- 比如:select * from t_emp where emp_name = ? and age = ? and sex = ? and email= ?
- < if test="empName != null and empName != ''" >emp_name = #{emp_name}< /if >
- 语句不成立:select * from t_emp where and age = ? and sex = ? and email= ?
- 我们会看到错误提示中表明where后面多了个“and”
<!--List<Emp> getEmpByCondition(Emp emp)-->
<select id="getEmpByCondition" resultType="Emp">
select * from t_emp where 1 = 1
<if test="empName != null and empName != ''" >
emp_name = #{empName}
</if>
<if test="age != null and age != ''" >
and age = #{age}
</if>
<if test="sex != null and sex != ''" >
and sex = #{sex}
</if>
<if test="email != null and email != ''" >
and email = #{email}
</if>
</select>
2、Where标签
where和if一般结合使用:
- 若where标签中的if条件都不满足,则where标签没有任何功能,即不会添加where关键字
- 若where标签中的if条件满足,则where标签会自动添加where关键字,并将条件最前方多余的 and | or去掉
注意:where标签不能去掉条件最后多余的and | or
<!--List<Emp> getEmpByCondition(Emp emp)-->
<select id="getEmpByCondition" resultType="Emp">
select * from t_emp
<where>
<if test="empName != null and empName != ''" >
emp_name = #{empName}
</if>
<if test="age != null and age != ''" >
and age = #{age}
</if>
<if test="sex != null and sex != ''" >
and sex = #{sex}
</if>
<if test="email != null and email != ''" >
and email = #{email}
</if>
</where>
</select>
3、trim 标签
prefix | suffix属性:将trim标签中内容前面或后面添加指定内容
prefixOverrides | suffixOverrides 属性:将trim标签中内容前面或后面去掉指定的内容
若标签中没有内容时,trim标签也没有任何效果
<!--List<Emp> getEmpByCondition(Emp emp)-->
<sql id="empColumns">eid,emp_name,age,sex,email</sql>
<select id="getEmpByCondition" resultType="Emp">
select <include refid="empColumns"></include> from t_emp
<trim prefix="where" suffixOverrides="and">
<if test="empName != null and empName != ''">
emp_name = #{empName} and
</if>
<if test="age != null and age != ''">
age = #{age} and
</if>
<if test="gender != null and gender != ''">
gender = #{gender}
</if>
</trim>
</select>
4、choose,when,otherwise 标签
when 相当于 if else,至少有一个
otherwise 相当于 else,最多有一个
相当于Java中的 if…else if,所以只执行一个条件
<!--List<Emp> list getEmpByChoose(Emp emp);-->
<select id="getEmpByChoose" resultType="Emp">
select * from t_emp
<where>
<choose>
<when test="empName != null and empName != ''">
emp_name = #{empName}
</when>
<when test="age != null and age != ''">
age = #{age}
</when>
<when test="sex != null and sex != ''">
sex = #{sex}
</when>
<when test="email != null and email != ''">
email = #{email}
</when>
<otherwise>
did = 1
</otherwise>
</choose>
</where>
</select>
5、foreach标签
collection 属性:设置需要循环的数组或集合
item 属性:表示数组或集合中的每一个数据
separator 属性: 循环体之间的分隔符
open 属性:foreach标签所循环的所有内容的开始符
close 属性:foreach标签所循环的所有内容的结束符
a.批量删除 :通过数组实现批量删除
<!--
int deleteMoreByArray(@Param("eids") Integer[] empIds);
-->
<delete id="deleteMoreByArray">
delete from t_emp where eid in
<foreach collection="empIds" item="empId" separator="," open="(" close=")">
#{empId}
</foreach>
</delete>
<!--通过数组实现批量删除
int deleteMoreByArray(@Param("eids") Integer[] empIds);
-->
<delete id="deleteMoreByArray">
delete from t_emp where
<foreach collection="empIds" item="empId" separator="or">
emp_id = #{empId}
</foreach>
</delete>
b.批量添加:通过集合方式实现批量添加
<!--int insertMoreByList(@Param("emps") List<Emp> emps);-->
<insret id="insertMoreByList">
insert into t_emp values
<foreach collection="emps" item="emp" separator=",">
(null,#{emp.empName},#{emp.age},#{emp.sex},#{emp.email},null)
</foreach>
</insret>
6、sql标签
sql 片段,将查询的字段提取出来
- 声明SQL片段:< sql id="empColumns">eid,emp_name,age,sex,email< /sql >
- 引用SQL片段:< include refid="empColumns">< /include>
<sql id="empColumns">eid,emp_name,age,sex,email</sql>
<select id="getEmpByCondition" resultType="Emp">
select <include refid="empColumns"></include> from t_emp
</select>