1.if标签
根据标签中的test属性所对应的表达式决定标签中的内容是否需要拼接到sql中
<!-- List<Emp> getEmpByDyCondition(Emp emp); -->
<select id="getEmpByDyConditionOne" 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>2.where标签
* 当where标签中有内容时,会自动生成where关键字,并且将内容前多余的and或or去掉
* 当where标签中没有内容时,此时where标签没有任何内容
* 注意:where标签不能将其中内容后面多余的and符号去掉,and只能放在前面
<select id="getEmpByDyConditionTwo" resultType="Emp">
select * from t_emp
<where>
<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>
</where>
</select>3.trim标签
* prefix/suffix:在trim标签中内容前面或后面添加指定内容
* prefixOverrides/suffixOverrides:将trim标签中内容前面或后面去掉指定内容
* 若标签中没有内容时,trim标签也没有任何效果
<select id="getEmpByDyCondition" resultType="Emp">
select <include refid="empColumns" /> from t_emp
<trim prefix="where" suffixOverrides="and|or" prefixOverrides="and|or">
<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="sex = null and sex = ''">
and sex = '男'
</if>
<if test="email != null and email != ''">
and email = #{email}
</if>
</trim>
</select>4.choose、when、otherwise标签
相当于if...else if...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 != ''">
and age = #{age}
</when>
<when test="sex != null and sex != ''">
and sex = #{sex}
</when>
<when test="email != null and email != ''">
and email = #{email}
</when>
<otherwise> did = 1 </otherwise>
</choose>
</where>
</select>5.foreach标签
* collection:设置需要循环的数组或集合
* item:表示数组或集合中的每一条数据
* separator:循环体之间的分割符
* open:foreach标签所循环的所有内容的开始符
* close:foreach标签所循环的所有内容的结束符
<!--int deleteMoreByarray(Integer[] eids);-->
<delete id="deleteMoreByarray">
<!--方法1
delete from t_emp where eid in
<foreach collection="eids" item="eid" separator="," open="(" close=")">
#{eid}
</foreach>
-->
<!--方法二-->
delete from t_emp where
<foreach collection="eids" item="eid" separator="or">
eid = #{eid}
</foreach>
</delete>
<!--int insertMoreByList(@Param("emps") List<Emp> emps);-->
<insert id="insertMoreByList">
insert into t_emp(emp_name,password,age,sex,email) values
<foreach collection="emps" item="emp" separator=",">
(#{emp.empName},#{emp.password},#{emp.age},#{emp.sex},#{emp.email})
</foreach>
</insert>6.sql标签
* 设置sql片段:<sql id="empColumns">eid,emp_name,age,sex,email</sql>
* 引用sql片段:select <include refid="empColumns" /> from t_emp
<sql id="empColumns">eid,emp_name,age,sex,email</sql>
<select id="getEmpByDyCondition" resultType="Emp">
select <include refid="empColumns" /> from t_emp
</select>「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




