Mybatis使用动态sql
动态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>
Mybatis使用动态sql的更多相关文章
- MyBatis的动态SQL详解
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑,本文详解mybatis的动态sql,需要的朋友可以参考下 MyBatis 的一个强大的特性之一通常是它 ...
- Mybatis解析动态sql原理分析
前言 废话不多说,直接进入文章. 我们在使用mybatis的时候,会在xml中编写sql语句. 比如这段动态sql代码: <update id="update" parame ...
- mybatis 使用动态SQL
RoleMapper.java public interface RoleMapper { public void add(Role role); public void update(Role ro ...
- MyBatis框架——动态SQL、缓存机制、逆向工程
MyBatis框架--动态SQL.缓存机制.逆向工程 一.Dynamic SQL 为什么需要动态SQL?有时候需要根据实际传入的参数来动态的拼接SQL语句.最常用的就是:where和if标签 1.参考 ...
- 使用Mybatis实现动态SQL(一)
使用Mybatis实现动态SQL 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 写在前面: *本章节适合有Mybatis基础者观看* 前置讲解 我现在写一个查询全部的 ...
- MyBatis探究-----动态SQL详解
1.if标签 接口中方法:public List<Employee> getEmpsByEmpProperties(Employee employee); XML中:where 1=1必不 ...
- mybatis中的.xml文件总结——mybatis的动态sql
resultMap resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功. 如果sql查询字段名和pojo的属性名不一致,可以通过re ...
- mybatis.5.动态SQL
1.动态SQL,解决关联sql字符串的问题,mybatis的动态sql基于OGNL表达式 if语句,在DeptMapper.xml增加如下语句; <select id="selectB ...
- MyBatis的动态SQL详解-各种标签使用
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. MyBatis中用于实现动态SQL的元素主要有: if choose(when,otherwise) ...
- 利用MyBatis的动态SQL特性抽象统一SQL查询接口
1. SQL查询的统一抽象 MyBatis制动动态SQL的构造,利用动态SQL和自定义的参数Bean抽象,可以将绝大部分SQL查询抽象为一个统一接口,查询参数使用一个自定义bean继承Map,使用映射 ...
随机推荐
- 【面试】MySQL 中NULL和空值的区别?
做一个积极的人 编码.改bug.提升自己 我有一个乐园,面向编程,春暖花开! 01 小木的故事 作为后台开发,在日常工作中如果要接触Mysql数据库,那么不可避免会遇到Mysql中的NULL和空值.那 ...
- ECS通过mail发送邮件
发送邮件报错,因为新购实例默认对外访问25端口封禁状态,建议使用加密465端口 1.配置mailx [root@wiki ~]# yum install -y mailx ##yum安装完mailx之 ...
- 你真的会用mysql行级锁吗?mysql 行级锁全解析
在互联网大并发应用大行其道的今天,应用的开发总是离不开锁,在分布式应用中,最常见的莫过于基于数据库的行级锁了,由于互联网公司中比较主流的数据库还是mysql,所以这一话题绕不开的就是mysql了,但是 ...
- Node.js实现PC端类微信聊天软件(五)
Github StackChat 学习回顾 Socket.io 结合Express创建Socket.io服务器 const app = require('express')() const http ...
- MySQL5.7高版本 疑似内存泄露
I have an instance running on mysql-5.7.19. However, I found that its physical memory and virtual me ...
- HDU 5117:Fluorescent(状压DP + 思维)***
题目链接 题意 给出n个灯,m个开关,每个开关控制一些灯,如果打开这个开关,这个开关控制的灯如果本来灭的就会亮,如果本来亮的就会灭.问在每个开关按下与否的一共2^m情况下,每种状态下亮灯的个数的立方的 ...
- 基于C#的机器学习--我应该接受这份工作吗-使用决策树
决策树 要使决策树完整而有效,它必须包含所有的可能性.事件序列也必须提供,并且是互斥的,这意味着如果一个事件发生,另一个就不能发生. 决策树是监督机器学习的一种形式,因为我们必须解释输入和输出应该是什 ...
- 基数排序的可复用实现(C++11/14/17/20)
基数排序,是对整数类型的一种排序方法,有MSD (most significant digit)和LSD (least significant digit)两种.MSD将每个数按照高位分为若干个桶(按 ...
- 如何编写无须人工干预的shell脚本
在使用基本的一些shell命令时,机器需要与人进行互动来确定命令的执行.比如 cp test.txt boo/test.txt,会询问是否覆盖?ssh远程登陆时,需要输入人工密码后,才可以继续执行ss ...
- JS获取display为none的隐藏元素的宽度和高度的解决方案
有时候,我们一进入页面,就需要获取display为none元素的物理尺寸(宽高),或获取display为none元素的子元素的物理尺寸(宽高),本篇文章就如何解决以上问题给出自己的解决方案 <h ...