<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xiaKangan.mapper.EmpMapper">
<!-- SQL片段的重复使用-->
<!-- 将SQL代码放入sql标签中,可以在其他增删改查操作中引入id实现复用-->
<sql id="sqlSyntax">
select * from emp where name like concat('%',#{name},'%') and gender= #{gender} and
entrydate between #{begin} and #{end} order by update_time desc
</sql>
<select id="cSearch" resultType="com.xiaKangan.pojo.Emp">
-- 引入SQL片段
<include refid="sqlSyntax"/>
-- select * from emp where name like concat('%',#{name},'%') and gender= #{gender} and
-- entrydate between #{begin} and #{end} order by update_time desc
</select>
<!-- 动态SQL条件查询-->
<select id="conditionSearch" resultType="com.xiaKangan.pojo.Emp">
select *
from emp
-- where标签当有至少一个条件成立时,生成SQLwhere语句,并且自动修正SQL语句
<where>
<if test="name != null">
name like concat('%' , #{name} , '%')
</if>
<if test="gender != null">
and gender= #{gender}
</if>
<if test="begin != null and end != null">
and entrydate between #{begin} and #{end}
</if>
</where>
order by update_time desc
</select>
<!-- 动态SQL更新数据-->
<update id="update2">
update emp
set
-- 将SQL语句里面多余的语法结构删除掉,避免语法错误
-- trim 标签中还有 prefixOverrides 删除语法中的前缀多余部分
-- 在if语句的外层添加set标签也能纠正语法
<trim suffixOverrides=",">
<if test="username != null">username = #{username},</if>
<if test="name != null">name = #{name},</if>
<if test="gender != null">gender = #{gender},</if>
<if test="image != null">image = #{image},</if>
<if test="job != null">job = #{job},</if>
<if test="entrydate != null">entrydate = #{entrydate},</if>
<if test="deptId != null">dept_id = #{deptId},</if>
<if test="updateTime != null">update_time = #{updateTime}</if>
</trim>
<where>
id = #{id}
</where>
</update>
<!-- 批量删除员工信息(18,19,21)-->
<delete id="deleteByIds">
delete from emp where id in
-- collection 遍历的集合
-- item 遍历出来的元素
-- separater 分隔符
-- open 遍历开始前的符号
-- close 遍历结束的符号
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
</mapper>
XML动态SQL图片文件