首页 > 数据库 > 动态SQL

动态SQL

时间:2023-02-08 19:00:48浏览次数:36  
标签:userName smbms 参数 user SQL 动态 userRole

一、where+if:
<where>自动判断其内部是否有返回值,如果有,则插入一个where并自动删除SQL语句前多余的and或or
<select id="getUserList" resultType="User">
select * from smbms_user
<where>
<if test="name !=null and name != ''">
userName like concat('%',#{name},'%')
</if>
<if test="role != null">
and userRole=#{role}
</if>
</where>
</select>
二、set+if:
<se>t标签自动判断其内部是否有返回值,如果有则插入一个set,并且删除SQL语句最后一个逗号
<update id="updateUser">
update smbms_user
<set>
<if test="userCode != null and userCode != ''">userCode=#{userCode},</if>
<if test="userPassword != null and userPassword != ''">userPassword=# {userPassword},</if>
<if test="userName != null and userName != ''">userName=#{userName},</if>
<if test="gender != null">gender=#{gender},</if>
<if test="birthday != null!= ''">birthday=#{birthday},</if>
<if test="phone != null">phone=#{phone},</if>
<if test="address != null and address != ''">address=#{address},</if>
<if test="userRole != null">userRole=#{userRole},</if>
<if test="createdBy != null">createdBy=#{createdBy},</if>
<if test="creationDate != null">creationDate=#{creationDate},</if>
<if test="modifyBy != null">modifyBy=#{modifyBy},</if>
<if test="modifyDate != null">modifyDate=#{modifyDate},</if>
</set>
where id=#{id}
</update>
三、foreach:
<foreach>自动遍历参数数组或今后。
属性:
• collection:参数(Map集合)的键名,如果是数组,键名自动为array;如果是List,键名自动是list;如果是Map,键名需要于定义的键名保持相同。
• item:参数名
• oppen/close:以oppen开始,close结束
• separator:一般是逗号
<select id="getUserListByRole" resultType="User">
select * from smbms_user
where userRole in
<foreach collection="array" item="roles" open="(" close=")" separator=",">
#{roles}
</foreach>
</select>
四、MyBatis参数小结:
1、MyBatis可以接收的参数类型有:基本数据类型、对象、List、数组、Map
2、无论是哪种类型,MyBatis都会将它封装一个Map集合
3、如果是单个参数有一下几种情况:
• 参数是基本数据类型:变量名为key,变量值为value;
• 参数是对象:对象的属性名为key,属性值为value;
• 参数是List:默认将“list”为key,该List本身为value;
• 参数数数组:默认将“array”为key,该数组本身为value;
• 参数是Map:MyBatis就使用我们自定义的键值对
五、choose+when+otherwise:
类似于Java中的switch结构,和JSTL中的choose基本一样;
<choose>以此判断<when>中的语句是否有返回值,如果有,则执行,并跳出整个<choose>结构;如果没有,则默认执行<otherwise>中的语 句,并且<otherwise>可省略
六、分页:
limit x,y: x从哪一行开始显示(下标从0开始),y显示y条
(m-1)*n

七、trim+if:
<trim>自动判断内部是否有返回值,如果有,则插入一个“前缀”或“后缀”,并且自动覆盖SQL语句中的指定内容:“前缀覆盖”“后缀覆盖”。
prefix:前缀 suffix:后缀
prefixOverrides:前缀覆盖
<select id="getUserList" resultType="User">
select * from smbms_user
<trim prefix="where" prefixOverrides="and | or">
<if test="name !=null and name != ''">
userName like concat('%',#{name},'%')
</if>
<if test="role != null">
and userRole=#{role}
</if>
</trim>
</select>
生成的SQL语句:select * from smbms_user where userName like concat('%',?,'%')
seffixOverrides:后缀覆盖
<update id="updateUser">
update smbms_user
<trim suffix="where id=#{id}" suffixOverrides="," prefix="set">
<if test="userCode != null and userCode != ''">userCode=#{userCode},</if>
<if test="userPassword != null and userPassword != ''">userPassword=#{userPassword},</if>
<if test="userName != null and userName != ''">userName=#{userName},</if>
<if test="gender != null">gender=#{gender},</if>
<if test="birthday != null!= ''">birthday=#{birthday},</if>
<if test="phone != null">phone=#{phone},</if>
<if test="address != null and address != ''">address=#{address},</if>
<if test="userRole != null">userRole=#{userRole},</if>
<if test="createdBy != null">createdBy=#{createdBy},</if>
<if test="creationDate != null">creationDate=#{creationDate},</if>
<if test="modifyBy != null">modifyBy=#{modifyBy},</if>
<if test="modifyDate != null">modifyDate=#{modifyDate},</if>
</trim>
</update>
生成的SQL语句:update smbms_user set userPassword=?, userName=? where id=?

标签:userName,smbms,参数,user,SQL,动态,userRole
From: https://blog.51cto.com/u_15907719/6044739

相关文章

  • Vue动态绑定内联样式( v-bind:style )
    <button@click="change"type="button">改变Box大小data(){return{imgStyle:{}}},computed:{imgStyle(){retur......
  • 【SQL Server】中的日期函数和日期数据类型
    SQLServerDate函数SQLServer的重要日期函数包括:函数描述参数含义GETDATE()返回当前的日期和时间 DATEPART(datepart,date) 返回日期/时间的单独部分......
  • Linux安装Mysql 5.7
    1. 查看CentOS自带MySQL5.1组件并卸载;rpm -qa | grep -i mysqlrpm e -nodeps mysql-libs-5.1.61-4.el6.x86_64 2. 安装依赖(MySQL5.5并不需要这个依赖,但是5.7......
  • MYSQL5.7索引异常引发的锁超时处理记录
    原始sql:updateaseta.x=xwherea.xidin(selectb.xidfrombinnerjoinconb.xxx=c.xxx)anda.xdate='xxxxxxxx'现象sql执行中,b表会偶尔提示锁超时。处理......
  • element表单嵌套检验+动态添加
    在写表单的时候,容易碰到这种嵌套表单的数据校验,并且这种表单是动态添加的,网上大部分的做法是表单套表单,实际上只需要一个表单就可以了。为了方便观看,这里只列举了两条数......
  • SQL Server中cross apply和inner join的区别
    Sqlserver中CrossApply和innerjoin的区别crossapply和innerjoin有很多相同处,都是为了联表查询数据,而且取两张表之间的交集,不同在于,crossapply可以在右表中加......
  • springboot 动态获取配置信息完成启动
    架构说设计到数据量较大的应用要从k8s中迁出单独机器部署于是将8节点的服务准备迁出,且端口号在数据库中保存在不引入springcloud的方式下启动spring容器中对args进行配......
  • RPM 安装好19c后SQLPLUS登录报错ORA-12547
    问题描述:RPM安装好19c后SQLPLUS登录报错ORA-12547,如下所示:1、异常重现2、问题排查--查?/bin/oracle权限,确认无问题.--查/home/oracle/.bash_profile文件,发现ORACLE_HOME目......
  • MySQL DISTINCT关键字详解:用法、实际应用和与GROUP BY的区别
    "Successisnothowhighyouhaveclimbed,buthowyoumakeapositivedifferencetotheworld."成功不在于你爬得多高,而在于你对世界做出了多大的积极影响。-Roy......
  • Mysql数据库备份恢复
    一、使用musqldump对数据库、数据库表、一个数据库多个表、所以数据库备份、模拟数据丢失二、配置mysql数据库开启二进制日志功能,配置二进制日志切割,配置跟二进制恢复数据,居......