动态Sql:
动态Sql:MyBatis的动态Sql为了解决拼接sql语句字符串的问题,它根据特定条件动态的拼接sql语句。
Emp表和Emp实体类:
public class Emp {
private Integer id;
private String empName;
private Integer age;
private String sex;
private String email;
private Dept dept;//多对一,设置一的对象
}
1:if标签
根据if标签的test属性所对应的表达式决定标签中的内容是否拼接到Sql语句中。
场景: 当我们通过多个条件查询emp表中数据时,需要将多个条件使用and进行拼接。
接口的方法:
// if和恒成立条件
List<Emp> getEmpByIf(Emp emp);
接口的映射:
<!--if标签和恒成立条件-->
<!--List<Emp> getEmpByIf(Emp emp);-->
<select id="getEmpByIf" 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标签内有内容,生成where,将内容前多余的or或者and去掉,where标签内没有内容,不会生成where。
场景: 多条件查询时,我们可以不使用恒成立条件去避免sql拼接出错,可以使用where标签
接口的方法:
// where标签
List<Emp> getEmpByWhere(Emp emp);
接口的映射:
<!--where标签-->
<!--List<Emp> getEmpByWhere(Emp emp);-->
<select id="getEmpByWhere" 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!='' ">
or sex=#{sex}
</if>
<if test="email!=null and email!='' ">
and email=#{email}
</if>
</where>
</select
3:trim标签
trim中有内容时:prefix="" , suffix="" 将trim标签中内容前或后添加指定内容,prefixOverrides="" suffixOverrides=""
将trim标签中内容前面或后面去掉指定内容。trim中没有内容时,trim标签失效。
场景: 为了解决where标签后的and或者or
接口的方法:
//trim标签
List<Emp> getEmpByTrim(Emp emp);
接口的映射:
<!--trim标签-->
<!--List<Emp> getEmpByTrim(Emp emp);-->
<select id="getEmpByTrim" resultType="Emp">
select * from t_emp
<trim prefix="where" suffixOverrides="and|or" >
<if test="empName !=null and empName!=''">
emp_name like "%"#{empName} and
</if>
<if test="age!=null and age!=''">
age=#{age} or
</if>
<if test="email!=null and email!=''">
email=#{email}
</if>
</trim>
</select>
4:choose when otherwise标签
相当于java的if…else-if…else, when==if / else-if ,otherwise=else。 即使多个条件成立,最后的sql语句只有一个条件(第一个成立的条件)。 when的条件都不成立则使用otherwise。
when最少一个,otherwise最多一个。
接口的方法:
// choose when otherwise标签
List<Emp> getEmpByChoose(Emp emp);
接口的映射:
<!--choose when otherwise-->
<!-- List<Emp> getEmpByChoose(Emp emp);-->
<select id="getEmpByChoose" resultType="Emp">
select * from t_emp
<where>
<choose>
<when test="empName!=null and empName!=''">
emp_name like "%"#{empName}
</when>
<when test="sex!=null and sex!=''">
sex=#{sex}
</when>
<otherwise>
did=1
</otherwise>
</choose>
</where>
</select>
5:foreach标签
collection:设置需要循环的数组或者集合
item:数组或集合的一个元素
separator:循环体的分隔符
open:开始符号
close:结束符号
场景: 当参数为集合或者数组时,遍历的获取参数
1:数组
建议还是使用@Param为参数命名从而获取参数
接口的方法:
//数组实现批量删除 forEach标签
int deleteMore(@Param("ids") Integer[] ids);
接口的映射:批量删除可以通过id=?or id=?也可以通过id in()两种方式
<!--foreach 数组作为参数-->
<!--int deleteMore(Integer[] ids);-->
<delete id="deleteMore">
<!--SQL语句:delete from t_emp where id=? or id=? or id=?-->
<!--
delete from t_emp where
<foreach collection="ids" separator="or" item="id">
id=#{id}
</foreach>
-->
<!--SQL语句:delete from t_emp where id in ( ? , ? , ? )-->
delete from t_emp where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
2:集合
建议还是使用@Param为参数命名从而获取参数
接口的方法:
//集合实现批量添加 forEach标签
int insertMore(@Param("emps")List<Emp> emps);
接口的映射:批量插入 values(),(),()
循环体内获取集合参数,先获取到item对象,再通过item对象.属性获取值
<!--foreach集合作为参数-->
<!--int insertMore(@Param("emps")List<Emp> emps);-->
<insert id="insertMore">
<!--
insert into t_emp values (null,?,?,?,?,null) , (null,?,?,?,?,null)
-->
insert into t_emp values
<foreach collection="emps" item="emp" separator=",">
(null,#{emp.empName},#{emp.age},#{emp.sex},#{emp.email},null)
</foreach>
</insert>
6:sql标签
设置Sql片段:<sql id="唯一标识"></sql>
使用Sql片段:<include refid="唯一标识"></include>
场景: 比如我们的字段很多,在查询时,可能会写多个方法,所以将字段名作为一个sql片段,使用include调用即可。
接口的方法:
//sql标签
Emp getEmpById(@Param("id") Integer id);
接口的映射:
sql片段:
<!--sql标签-->
<sql id="empColumns">
id,emp_name,age,sex,email
</sql>
查询映射:
<select id="getEmpById" resultType="Emp">
<!--select id,emp_name,age,sex,email from t_emp where id=?-->
select <include refid="empColumns"></include> from t_emp where id=#{id}
</select>