mybatis where、set、trim、sql、foreach标签的使用
mybatis where标签的使用
where后面跟查询条件 简化sql语句中判断条件的书写
例:
<select id="user" parameterType="user" resultType="User">
select * from user
<where>
<if test="id!=null and id!=''">
id=#{id}
</if>
<if test="name!=null and name!=''">
and name=#{name}
</if>
<if test="gender!=null and gender!=''">
and gender=#{gender}
</if>
</where>
</select>
MyBatis会智能的把首个and 或 or 给忽略
如果id为null 执行的sql语句为 :
select * from user where name='xxx' and gender='xxx'
mybatis set标签的使用
例:
<update id="userUpdate" parameterType="user">
update user
<set>
<if test="id!=null and id!=''">
id=#{id},
</if>
<if test="name!=null and name!=''">
name=#{name},
</if>
<if test="gender!=null and gender!=''">
gender=#{gender},
</if>
</set>
</update>
如上片段假如都不为null的情况下执行SQL为: update user set id="xx",name='xxx',gender='xxx';
在如上sql中最后一个逗号会被set标记自动忽略不计
mybatis trim标签的使用
trim 属性
prefix:前缀
suffix:后缀
prefixOverrides:忽略第一个指定分隔符
suffixOverrides:会略最后一个分隔符
例:
1)
<select id="user" parameterType="user" resultType="User">
select * from user
<trim prefix="WHERE" prefixoverride="and | or">
<if test="id!=null and id!=''">
id=#{id}
</if>
<if test="name!=null and name!=''">
and name=#{name}
</if>
<if test="gender!=null and gender!=''">
and gender=#{gender}
</if>
</trim>
</select>
如果id为null执行的sql语句为:
select * from user where name="xxx" and gender="xxx"
2)
<update>
update user
<trim prefix="set" suffixoverride=",">
<if test="id!=null and id!=''">
id=#{id},
</if>
<if test="name!=null and name!=''">
name=#{name},
</if>
<if test="gender!=null and gender!=''">
gender=#{gender}
</if>
</trim>
</update>
如果gender为null 执行的sql语句为:
update user set id="xx",name="xx"
mybatis foreach标签的使用
可以利用<foreach>标签实现sql条件的循环,可完成类似批量的sql
mybatis接受的参数分为:
(1)基本类型(2)对象(3)List(4)数组(5)Map
参数:
collection:要循环的集合
index:定一个名字,用于表示在迭代过程中,每次迭代到的位置
item:集合中每一个元素进行迭代时的别名
open:以什么开始
close:以什么结束
separator:循环内容之间以什么分隔
例:
1)批量添加
<insert id="add" parameterType="List">
insert into user(id,name,gender) values
<foreach collection="list" item="user" open="(" close=")" separator=",">
#{user.id},#{user.name},#{user.gender}
</foreach>
</insert>
若list有两条数据 则执行的sql语句为:
insert into user(id,name,gender) values ("1","张三","男"),("2","李四","女")
mapper接口:
void add(List<User> users);
2)批量删除
<delete id="deleteUser" parameterType="array">
delete from user where id in
<foreach collection="array" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
如果有两条数据要删除 执行的sql语句为:
delete from user where id in(1,2)
mybatis sql标签的使用
通过sql片段达到代码重复利用
例:
<sql id="sqlcont">
select count(*)
</sql>
<sql id="sqlselect">
select *
</sql>
<sql id="sqlcontent">
from user
</sql>
<select id="findUser" parameterType="user" resultType="User">
<include refid="sqlcont"/>
<include refid="sqlcontent"/>
</select>
mybatis where、set、trim、sql、foreach标签的使用相关推荐
- mysql批量用trim限定_mybatis中批量更新sql语句,trim、foreach标签,varchar定义理解
准备工作 现在有一张表为:student,相关字段如下: mapper层方法 int batchUpdateStudent(List studentList); xml语句 update studen ...
- Mybatis配置及动态SQL语句标签
#Mybatis resources: ###db.properties: jdbc.driver = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://lo ...
- 【MyBatis框架】mapper配置文件-foreach标签
foreach标签 下面介绍一下一个mapper配置文件中的foreach标签(注意,要跟着前面的总结来看,这里使用的例子是结合前面的工程写的,大部分代码没有再赘述) foreach的作用是向sql传 ...
- 动态SQL( if, choose, when, otherwise, trim, where, set, foreach标签讲解)
1 动态SQL# 那么,问题来了: 什么是动态SQL? 动态SQL有什么作用? 传统的使用JDBC的方法,相信大家在组合复杂的的SQL语句的时候,需要去拼接,稍不注意哪怕少了个空格,都会导致错误.My ...
- Mybatis动态sql中的foreach标签的使用
foreach 标签一般用于遍历集合,构建in条件语句或者批量操作语句 <insert id="batchSave" parameterType="java.uti ...
- Mybatis系列:解决foreach标签内list为空的问题
我把之前发布在简书的两篇文章通过拦截器Interceptor优化Mybatis的in查询 和Mybatis中foreach标签内list为空的解决方案进行了整合,整理为本文的内容.此外,我还对代码部分 ...
- Mybatis——mapper.xml中常用的SQL相关标签简介
题外话:使用Map方式用于传参,也是一种比较方便的方法 Map<String,Object> params = new HashMap<>(); params.put(&quo ...
- Mybatis常用的动态SQL标签讲解
先让我们看看mybatis常用动态SQL的标签有哪一些 如果mybatis和SSM整合环境不会搭建请看这篇 https://blog.csdn.net/weixin_44012722/article/ ...
- MyBatis中动态sql语句标签详解
动态 SQL 通常写在mapper包下面的地址映射配置文件(.xml)中.根据条件的不同, SQL 语句也会随之动态的改变. MyBatis 中,提供了一组标签用于实现动态 SQL. 动态SQL语句标 ...
最新文章
- shell里面的参数
- 只读字符串的c语言命令,C语言只读空间 - C 语言程序设计
- 第三篇 - EOS DAWN-V3.0.0 智能合约开发之Hello World
- Python内置函数min(iterable, *[, key, default])
- 数据库零碎---常用的mysql命令,收藏了,方便查阅
- lesson 4 communication pattern
- oc快速生成单例头文件解析
- python怎样编写定时程序_如何用Python写一个每分每时每天的定时程序
- AI头发笔刷_笔刷不够用?给你送1000款~快来拿走
- 什么是存储过程?用什么来调用?
- 网络直播对现代社会的影响
- 紫外光谱分析的基本原理是什么
- 计算机无法读取tf卡,tf卡修复工具不好用教你电脑不识别TF卡的解决办法
- 大厂对ChatGPT的开发利用和评估案例收录
- 应届生职场入门毒打指南
- Gson:GitHub 标星 18K 的 JSON 解析器,Google 出品的 Java JSON 解析器,强烈推荐!
- 软件测试面试题之如何对web系统进行全面测试(持续更新中,求关注)
- 【单片机】简单I/O口扩展
- 国楼市调控一周年透视:部分房价松动 整体格局僵持
- 贝加莱ACOPOSACOPOS1022,1010,1045,1022,1090,1180,1320系列伺服驱动器故障号,故障信息及解决方法