mybatis中动态SQL
Posted 永旗狍子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis中动态SQL相关的知识,希望对你有一定的参考价值。
MyBatis的映射文件中支持在基础SQL上添加一些逻辑操作,并动态拼接成完整的SQL之后再执行,以达到SQL复用、简化编程的效果。
1.<sql>
<mapper namespace="com.cos.qf.Dao.EmpMapper">
<!-- sql 存放sql语句 可以帮助我们进行sql语句的复用,例如查询的字段 ,基础表查询 select xx from xxx-->
<sql id="base_sql">
eno,ename,esex,salary,pwd,dno,borndate
</sql>
<select id="getEmp" resultType="com.cos.qf.entity.Emp">
select
<!-- <include refid="base_sql"></include>-->
<include refid="base_sql"/> <!-- 通过ID引用SQL片段 -->
from emp
</select>
</mapper>
2.<where>
<!-- <where> 会自动帮我们拼接where 关键字 WHERE,并且会自动忽略前后缀(如:and | or)-->
<select id="findEmpByKey" resultType="com.cos.qf.entity.Emp">
select
<include refid="base_sql"></include>
from emp
<where>
<if test="eno!=null and eno!=0">
and eno=#{eno}
</if>
<if test="ename!=null and ename!=''">
and ename=#{ename}
</if>
<if test="esex!=null and esex!=''">
and esex=#{esex}
</if>
</where>
</select>
<!-- and salary<=#{esalary} 选中后try/catch的快捷键弹出 -->
<select id="findEmpByKey2" resultType="com.cos.qf.entity.Emp">
select
<include refid="base_sql"></include>
from emp
<where>
<if test="ssalary!=null and ssalary!=0">
and salary>=#{ssalary}
</if>
<if test="esalary!=null and esalary!=0">
<![CDATA[
and salary<=#{esalary}
]]>
</if>
</where>
</select>
3.<set>
<!-- set子句中满足条件的if,会自动忽略后缀(如:,) -->
<update id="updateEmpByKey">
update emp
<set>
<if test="ename!=null">
ename=#{ename},
</if>
<if test="esex!=null">
esex=#{esex},
</if>
</set>
where eno=#{eno}
</update>
4.<trim>
< trim prefix="添加前缀" suffix="添加后缀" prefixOverrides="自动忽略前缀" suffixOverrides="自动忽略后缀" >代替< where > 、< set >
<select id="findEmpByKey3" resultType="com.cos.qf.entity.Emp">
select
<include refid="base_sql"/>
from emp
<trim prefix="Where" prefixOverrides="And | OR" suffixOverrides="," suffix="order by salary desc">
<if test="ssalary!=null and ssalary!=0">
and salary>=#{ssalary}
</if>
<if test="esalary!=null and esalary!=0">
<![CDATA[
and salary<=#{esalary}
]]>
</if>
</trim>
</select>
<update id="updateBookByCondition">
UPDATE t_books
<trim prefix="SET" suffixOverrides=","> <!-- 增加SET前缀,自动忽略后缀 -->
<if test="name != null">
name = #{name} ,
</if>
<if test="author != null">
author = #{author} ,
</if>
<if test="publish != null">
publish = #{publish} ,
</if>
<if test="sort != null">
sort = #{sort}
</if>
</trim>
WHERE id = #{id}
</update>
5.<foreach>
<!-- delete from emp in (1,2,3,4,5,6) -->
<delete id="deleteBookByIds">
DELETE FROM t_books
WHERE id IN
<foreach collection="list" open="(" separator="," close=")" item="id" index="i">
#{id}
</foreach>
</delete>
<!-- 批量新增 -->
<!-- INSERT INTO emp VALUES ((),()) -->
<!--
for (Emp emp : empByKey2) {
System.out.println(emp);
}
-->
<insert id="addEmps" >
insert into emp values
<foreach collection="list" separator="," item="e">
(default,#{e.ename},#{e.esex},#{e.salary},#{e.pwd},#{e.dno},#{e.bornDate})
</foreach>
</insert>
参数 | 描述 | 取值 |
---|---|---|
collection | 容器类型 | list、array、map |
open | 起始符 | ( |
close | 结束符 | ) |
separator | 分隔符 | , |
index | 下标号 | 从0开始,依次递增 |
item | 当前项 | 任意名称(循环中通过 #{任意名称} 表达式访问) |
以上是关于mybatis中动态SQL的主要内容,如果未能解决你的问题,请参考以下文章
Mybatis -- 动态Sql概述动态Sql之<if>(包含<where>)动态Sql之<foreach>sql片段抽取
mybatis动态sql之利用sql标签抽取可重用的sql片段