当前位置:首页 > 编程笔记 > 正文
已解决

mybatis动态sql语法

来自网友在路上 195895提问 提问时间:2023-11-21 02:50:00阅读次数: 95

最佳答案 问答题库958位专家为你答疑解惑

<?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 来进行批量删除

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"mybatis动态sql语法":http://eshow365.cn/6-40823-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!