Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它存在的意义是为了
解决 拼接SQL语句字符串时的痛点问题。
@TOC
if
if:根据标签内中test属性对应的表达式决定标签中的内容是否需要拼接到sql中
<!--List<emp> getEmpByCondition(Emp emp);-->
<!-- if:根据标签内中test属性对应的表达式决定标签中的内容是否需要拼接到sql中 -->
<select id="getEmpByConditionOne" resultType="Emp">
select * from t_emp where 1=1
<if test="empName !=null and empName !='' ">
and 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>
where
where 标签中如果有内容的话,会自动添加where关键字,并且会把where标签中多于的and或者or去掉(内容前的and),如果where标签中没有任何内容的话,where标签就不会出现
<!-- where 标签中如果有内容的话,会自动添加where关键字,并且会把where标签中多于的and或者or去掉(内容前的and),如果where标签中没有任何内容的话,where标签就不会出现-->
<select id="getEmpByConditionTwo" 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>
trim
prefix/suffix:将trim标签中内容前面或后面添加指定内容
prefixOverrides/suffixOverrides:将trim标签中内容前面或后面去掉指定内容
若标签中没有内容:trim标签也没有任何效果
<select id="getEmpByCondition" resultType="Emp">
select * from t_emp
<trim prefix="where" suffixOverrides="and|or">
<if test="empName !=null and empName !='' ">
emp_name=#{
empName} and
</if>
<if test="age !=null and age !=''">
age=#{
age} and
</if>
<if test="sex !=null and sex!=''">
sex=#{
sex} and
</if>
<if test="email !=null and email !=''">
email=#{
email} and
</if>
</trim>
</select>
choose、when、otherwise
choose:
when:
otherwise:
choose标签为父标签,when与otherwise需要写在choose中
这一套标签相当于Java中的if,else if else,when相当于if,else if ,otherwise相当于else
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=#{
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>
foreach
场景:批量操作:添加;删除
foreach:
collection:需要设置循环的数组或集合 即 传来的数据-即数组
item:表示数组或集合中的每一个数据 即 in里面数据的属性
separator:循环体之间的分隔符 以什么为分隔符
open:以什么开始:foreach标签所循环的所有内容的开始符
close以什么结束:foreach标签所循环的所有内容的结束符
通过这两个标签可以省略()
separator:以什么为分隔符
item:in里面数据的属性,即数组中的每一个数据
<!--int deleteMoreByArray(@Param("eids") Integer[] eids);-->
<delete id="deleteMoreByArray">
delete from t_emp where
<foreach collection="eids" item="eid" separator="or" >
eid=#{
eid}
</foreach>
<!--delete from t_emp where eid in
(
<foreach collection="eids" item="eid" separator=",">
#{
eid}
</foreach>
)-->
</delete>
<!--批量增加-->
<!--int insertMoreByList(@Param("emps") List<Emp> emps);-->
<insert id="insertMoreByList" >
insert into t_emp values
<foreach collection="emps" item="emp" separator=",">
(null,#{
emp.empName},#{
emp.age},#{
emp.sex},#{
emp.email},null)
</foreach>
</insert>
sql
将常用的sql片段进行记录,需要用的时候可以直接引入
<sql id="" ></sql>//id自己命名
<include refid=" "></include>//这里填id名称
<sql id="empColumns">
eid,ename,age,sex,did
</sql>
select <include refid="empColumns"></include> from t_emp