<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qvfan.mybatistest.mapper.EmpMapper"><sql id="commonSelect">select id,username,password,name,gender,image,job,entrydate,dept_id,create_time,update_timefrom emp</sql><!-- id和mapper接口中的方法名保持一致 resultType为单条记录所封装的类型 --><select id="list" resultType="com.qvfan.mybatistest.pojo.Emp"><include refid="commonSelect"></include><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><!-- 动态更新员工信息 --><update id="update2" >update emp<set><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></set>where id = #{id};</update><!--批量删除员工collection : 遍历的集合item : 遍历出来的元素separator : 分隔符open : 遍历开始前拼接的sql片段close : 遍历结束后拼接的sql片段--><delete id="deleteById">delete from emp where id in<foreach collection="ids" item="id" separator="," open="(" close=")">#{id}</foreach></delete>
</mapper>
以上
查询使用了sql include 将通用的抽离出来,用where if 来进行判断 从而实现 动态sql
更新使用了set if
删除使用了foreach 来进行批量删除