动态 SQL
动态 SQL
1、一种根据特定条件动态拼装 SQL 语句的功能
2、解决拼接 SQL 语句字符串时的问题
if
1、if 标签可通过 test 属性的表达式(即方法实参)进行判断:若表达式的结果为 true,则标签中的内容会执行;反之标签中的内容不会执行
2、在 where 后添加一个恒成立条件,如:1=1
(1)该恒成立条件并不会影响查询的结果,可以用来拼接 and 语句
(2)例如:当 empName 为 null 时
(3)如果不加上恒成立条件,则 SQL 语句为 select * from emps where and did = ?,此时 where 会与 and 连用,SQL 语句会报错
(4)如果加上一个恒成立条件,则 SQL 语句为 select * from emps where 1= 1 and did = did,此时 SQL 不报错
<!-- List<Emp> getEmpByCondition(Emp emp); -->
<select id="getEmpByCondition" resultType="Emp">
select * from emps where 1=1
<if test="empName != null and empName !=''">
and emp_name = #{empName}
</if>
<if test="eid != null and eid !=''">
and eid = #{eid}
</if>
</select>
where
1、一般 where 和 if 结合使用
(1)若 where 标签中的 if 条件都不满足,则 where 标签没有任何功能,即不会添加 where 关键字
(2)若 where 标签中的 if 条件满足其一,则 where 标签自动添加 where 关键字,并去掉条件最前方多余的 and / or
2、注意:where 标签不能去掉条件后多余的 and / or
<!-- List<Emp> getEmpByCondition(Emp emp); -->
<select id="getEmpByCondition" resultType="Emp">
select * from emps
<where>
<if test="empName != null and empName !=''">
emp_name = #{empName}
</if>
<if test="eid != null and eid !=''">
and eid = #{eid}
</if>
</where>
</select>
trim
1、trim 标签用于去掉或添加标签中的内容
2、常用属性
(1)prefix:在 trim 标签中的内容的前面添加指定内容
(2)suffix:在 trim 标签中的内容的后面添加指定内容
(3)prefixOverrides:在 trim 标签中的内容的前面去掉指定内容
(4)suffixOverrides:在trim标签中的内容的后面去掉指定内容
3、若 trim 中的标签都不满足条件,则 trim 标签没有任何效果,也就是只剩下
<!-- List<Emp> getEmpByCondition(Emp emp); -->
<select id="getEmpByCondition" resultType="Emp">
select * from emps
<trim prefix="where" suffixOverrides="and | or">
<if test="empName != null and empName !=''">
emp_name = #{empName} or
</if>
<if test="eid != null and eid !=''">
eid = #{eid} and
</if>
<if test="email != null and email !=''">
email = #{email}
</if>
</trim>
</select>
choose、when、otherwise
1、when 等价于 if …… else if ……,otherwise 等价于 else
2、when 至少要有一个,otherwise 至多只有一个
3、只执行最先符合条件的 SQL 语句
<!-- List<Emp> getEmpByCondition(Emp emp); -->
<select id="getEmpByChoose" resultType="Emp">
select * from t_emp
<where>
<choose>
<when test="empName != null and empName != ''">
emp_name = #{empName}
</when>
<otherwise>
eid = 1
</otherwise>
</choose>
</where>
</select>
foreach
1、foreach 标签对集合进行遍历
2、属性
(1)collection:设置要循环的数组或集合
(2)item:表示集合或数组中的每一个数据
(3)separator:设置循环体之间的分隔符,分隔符前后默认有一个空格
(4)open:设置 foreach 标签中的内容的开始符
(5)close:设置 foreach 标签中的内容的结束符
3、使用数组,批量删除
(1)以 @Param 的 value / array / arg0 为键,以数组为值
(2)IN() 关键字
<!-- int deleteMoreByArray(@Param("eids") Integer[] eids); -->
<delete id="deleteMoreByArray">
delete from emps where eid in
<foreach collection="eids" item="eid" separator="," open="(" close=")">
#{eid}
</foreach>
<!--
等价于
(
<foreach collection="eids" item="eid" separator=",">
#{eid}
</foreach>
)
-->
</delete>
(3)OR 关键字
<!-- int deleteMoreByArray(@Param("eids") Integer[] eids); -->
<delete id="deleteMoreByArray">
delete from emps where
<foreach collection="eids" item="eid" separator="or">
eid = #{eid}
</foreach>
</delete>
4、使用集合,批量添加
(1)以 @Param 的 value / arg0 / collection / list 为键,以集合为值
<!-- int insertMoreByList(@Param("emps") List<Emp> emps); -->
<insert id="insertMoreByList">
insert into emps values
<foreach collection="emps" item="emp" separator=",">
(#{emp.eid},#{emp.empName},#{emp.email})
</foreach>
</insert>
SQL 片段
1、可以记录一段公共 SQL 片段,在使用的地方通过 include 标签进行引入
2、声明 SQL 片段:<sql> 标签
<sql id="empColumns">eid,emp_name</sql>
3、引用 SQL 片段:<include> 标签
<!-- List<Emp> getEmpByCondition(Emp emp); -->
<select id="getEmpByCondition" resultType="Emp">
select <include refid="empColumns"></include>
from emps
</select>
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战