动态sql

常见的几种:trim、where、set、foreach、if、choose、when

下面通过案例一一演示


if语法

 <select id="selectIfTest1" resultType="cn.kgc.mybatisdemo.mode.User">
select id , userCode , userName from smbms.smbms_user where 1=1
<if test="userName !=null and userName != '' ">
and userName like concat('%',#{userName},'%')
</if>
<if test="roleId != 0">
and userRole = #{roleId}
</if>
</select>

if模块在判断通过后拼接模块内的代码

接下来是where代码

 <select id="selectIfTest2" resultType="cn.kgc.mybatisdemo.mode.User">
select id , userCode , userName from smbms.smbms_user
<where>
<if test="userName != null and userName != ''">
and userName like concat('%',#{userName},'%')
</if>
<if test="roleId != 0">
and roleId = #{roleId}
</if>
</where>
</select>

/*
where标签代替了where关键字,他除了能给添加关键字同时,
也可以智能的出去多余的and和or,where标签一般和if一起使用
当where标签中的if条件都不满足要求的时候,where标签不会拼接关键字
*/

set标签的使用

 <update id="updateUserTest3">
update smbms.smbms_user
<set>
<if test="userName != null and userName != ''">
userName = #{userName},
</if>
<if test="userCode != null and userCode != ''">
userCode = #{userCode},
</if>
<if test="userPassword != null and userPassword != ''">
userPassword = #{userPassword},
</if>
<if test="modifyDate != null" >
modifyDate = now(),
</if>
</set>
</update>

/*
set标签 代替set关键字,可以智能的删除多余的逗号
其他内容和where一样,修改的时候必须要传值,否则会报错,这样就没意义了
*/

trim的使用

<update id="updateUserTest4">
update smbms_user <trim prefix="set" suffixOverrides="," suffix="where id = #{id}">
<if test="userName != null and userName != ''">
userName = #{userName},
</if>
<if test="userCode != null and userCode != ''">
userCode = #{userCode},
</if>
<if test="userPassword != null and userPassword != ''">
userPassword = #{userPassword},
</if>
<if test="modifyDate != null" >
modifyDate = now(),
</if>
</trim>
</update>

/*
prefix添加前缀
prefixOverride清除前缀’
suffix添加后缀
suffixOverride清除后缀

trim 迄今为止最好用的标签
*/

foreach的三种形式

<select id="selectUserByForeachArray" resultType="cn.kgc.mybatisdemo.mode.User">
select id,userName,userCode,userPassword from smbms.smbms_user
where userRole in
/*
collection:当前的参数类型(必填)
open:开始要添加的字符
separator:分隔符
close:结束时要添加的字符
item:相当于变量
*/
<foreach collection="array" open="(" separator="," close=")" item="roleId">
#{roleId}
</foreach>
</select>

第二种

    <select id="selectUserByList" resultType="cn.kgc.mybatisdemo.mode.User">
select id,userName,userCode,userPassword from smbms.smbms_user where userRole in
<foreach collection="list" item="roleId" open="(" separator="," close=")">
#{roleId}
</foreach>
</select>

第三种

    <select id="selectUserByMap" resultType="cn.kgc.mybatisdemo.mode.User">
select id,userName,userCode,userPassword from smbms.smbms_user where userRole in
/*
这里的collection的值放的是Map集合的键
*/
<foreach collection="roleId" open="(" separator="," close=")" item="roleId">
#{roleId}
</foreach>
and gender = #{gender}
</select>

choose的使用

    <select id="selectUserByChoose" resultType="cn.kgc.mybatisdemo.mode.User">
select id,userName,userCode,userPassword from smbms.smbms_user
/*
choose类似于java中的switch when相当于case,只要走一个分支就不进入其他分支
otherwise是默认,如果上面条件不满足就走它
*/
<where>
<choose>
<when test="id != 0 and id != null">
id = #{id}
</when>
<when test="userName != null and userName != '' ">
userName like concat('%',#{userName},'%')
</when>
<when test="gender">
gender = #{gender}
</when>
<otherwise>
1=1
</otherwise>
</choose>
</where>
</select>

最新文章

  1. WPF时间格式化
  2. MySQL SQL中的安全问题
  3. Matlab 查看变量信息who whos命令
  4. [Python爬虫]cnblogs博客备份工具(可扩展成并行)
  5. 【转载-好文】使用 Spring 2.5 注释驱动的 IoC 功能
  6. 学习shell之前你不得不了解的小知识
  7. 转载: scikit-learn学习之K-means聚类算法与 Mini Batch K-Means算法
  8. 【转】[MTK软件原创] [SELinux] 如何设置确认selinux模式
  9. WordPress Duplicator 0.4.4 Cross Site Scripting
  10. DB2数据库实例创建与删除 学习笔记
  11. ubuntu libtiff-dev
  12. SQL Server pivot 行转列遇到的问题
  13. JavaScript数据可视化编程学习(一)Flotr2,包含简单的,柱状图,折线图,饼图,散点图
  14. Leetcode 665. Non-decreasing Array(Easy)
  15. Linux VMware安装CentOS
  16. C#利用Zxing.net生成条形码和二维码并实现打印的功能
  17. pure框架
  18. 在前台jsp页面中取得并使用后台放入域中变量的方法
  19. 解决Swap file &amp;quot;.ceshi.c.swp&amp;quot; already exists!问题
  20. Python运维三十六式:用Python写一个简单的监控系统

热门文章

  1. 分析RESTful API安全性及如何采取保护措施
  2. Django框架之前端渲染-模板层
  3. Knative 初体验:Build Hello World
  4. pycharm同步代码到linux(转)
  5. C#学习笔记:ListBox控件的用法
  6. Easy doesn&#39;t enter into grown-up life
  7. F#周报2019年第28期
  8. EnjoyingSoft之Mule ESB开发教程第一篇:初识Mule ESB
  9. 【小家Spring】老项目迁移问题:@ImportResource导入的xml配置里的Bean能够使用@PropertySource导入的属性值吗?
  10. 一次线上遇到磁盘IO瓶颈的问题处理