Mybatis 动态Sql练习
建表
CREATE TABLE `student` (
`s_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`s_name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '',
`s_birth` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '',
`s_sex` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '',
PRIMARY KEY (`s_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

1. if语句
根据 name和 sex 来查询数据。如果name为空,那么将只根据sex来查询;反之只根据name来查询
<select id="findStudentByNameAndSex" resultType="StudentEntity">
select * from student where 1 = 1
<if test="sName != null">
and s_name = #{sName}
</if>
<if test="sSex != null">
and s_sex = #{sSex}
</if>
</select>
可以看到必须要加个1 = 1 ,不然的会就会多个and, 如果去掉第一个sql也有可能第一个刚好没执行,执行的第二个, 又多个and
2. if + where
<select id="findStudentByNameAndSex2" resultType="StudentEntity">
select * from student
<where>
<if test="sName != null">
s_name = #{sName}
</if>
<if test="sSex != null">
and s_sex = #{sSex}
</if>
</where>
</select>
这个“where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。
3. set
<update id="updateStudentById" parameterType="StudentEntity">
update student
<set>
<if test="sName != null and sName != ''">
s_name = #{sName},
</if>
<if test="sSex != null and sSex != ''">
s_sex = #{sSex}
</if>
</set>
where s_id = #{sId}
</update>
4. choose
只想选择其中的一个,查询条件有一个满足即可,
<select id="findStudentByChoose" parameterType="StudentEntity" resultType="StudentEntity">
select * from student
<where>
<choose>
<when test="sId != null and sId != ''">
s_id = #{sId}
</when>
<when test="sName != null and sName != ''">
and s_name = #{sName}
</when>
<otherwise>
and s_sex = #{sSex}
</otherwise>
</choose>
</where>
</select>
5. trim
<select id="findStudentByNameAndSex3" resultType="StudentEntity">
select * from student
<trim prefix="where" prefixOverrides="and | or">
<if test="sName != null and sName != ''">
and s_name = #{sName}
</if>
<if test="sSex != null and sSex != ''">
and s_sex = #{sSex}
</if>
</trim>
</select>
prefix:前缀
prefixoverride:去掉第一个and或者是or
6. sql片段
<sql id="findStudentByNameAndSex4SQL">
<if test="sName != null and sName != ''">
s_name = #{sName}
</if>
<if test="sSex != null and sSex != ''">
and s_sex = #{sSex}
</if>
</sql>
<select id="findStudentByNameAndSex4" resultType="StudentEntity">
select * from student where
<include refid="findStudentByNameAndSex4SQL"></include>
</select>
7. foreach
@Data
public class StudentVO {
private List<String> ids;
}
<select id="findStudentsByIds" parameterType="com.wang.vo.StudentVO" resultType="StudentEntity">
select * from student
<where>
s_id in
<foreach collection="ids" item="item" open="(" close=")" separator=",">
#{item}
</foreach>
</where>
</select>
Mybatis 动态Sql练习的更多相关文章
- mybatis实战教程(mybatis in action)之八:mybatis 动态sql语句
mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类:1. if 语句 (简单的条件判断)2. c ...
- 9.mybatis动态SQL标签的用法
mybatis动态SQL标签的用法 动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么 ...
- 自己动手实现mybatis动态sql
发现要坚持写博客真的是一件很困难的事情,各种原因都会导致顾不上博客.本来打算写自己动手实现orm,看看时间,还是先实现一个动态sql,下次有时间再补上orm完整的实现吧. 用过mybatis的人,估计 ...
- Mybatis动态SQL单一基础类型参数用if标签
Mybatis动态SQL单一基础类型参数用if标签时,test中应该用 _parameter,如: 1 2 3 4 5 6 <select id="selectByName" ...
- 超全MyBatis动态SQL详解!( 看完SQL爽多了)
MyBatis 令人喜欢的一大特性就是动态 SQL. 在使用 JDBC 的过程中, 根据条件进行 SQL 的拼接是很麻烦且很容易出错的. MyBatis 动态 SQL 的出现, 解决了这个麻烦. My ...
- Mybatis动态SQL简单了解 Mybatis简介(四)
动态SQL概况 MyBatis 的强大特性之一便是它的动态 SQL 在Java开发中经常遇到条件判断,比如: if(x>0){ //执行一些逻辑........ } Mybatis应用中,S ...
- mybatis原理分析学习记录,mybatis动态sql学习记录
以下个人学习笔记,仅供参考,欢迎指正. MyBatis 是支持定制化 SQL.存储过程以及高级映射的持久层框架,其主要就完成2件事情: 封装JDBC操作 利用反射打通Java类与SQL语句之间的相互转 ...
- mybatis 动态sql和参数
mybatis 动态sql 名词解析 OGNL表达式 OGNL,全称为Object-Graph Navigation Language,它是一个功能强大的表达式语言,用来获取和设置Java对象的属性, ...
- MyBatis动态SQL之一使用 if 标签和 choose标签
bootstrap react https://segmentfault.com/a/1190000010383464 xml 中 < 转义 to thi tha <if test=&qu ...
- MyBatis动态SQL(认真看看, 以后写SQL就爽多了)
目录 0 一起来学习 mybatis 1 数据准备 2 if 标签 2.1 在 WHERE 条件中使用 if 标签 2.1.1 查询条件 2.1.2 动态 SQL 2.1.3 测试 2.2 在 UPD ...
随机推荐
- 第20篇-加载与存储指令之ldc与_fast_aldc指令(2)
ldc指令将int.float.或者一个类.方法类型或方法句柄的符号引用.还可能是String型常量值从常量池中推送至栈顶. 这一篇介绍一个虚拟机规范中定义的一个字节码指令ldc,另外还有一个虚拟机内 ...
- el-upload + accept限制上传的文件格式
/** * kevin 2021/1/4 * @description el-upload + accept限制上传的文件格式 * @param e 校验的类型 * @returns {str ...
- Linux下运行bash脚本显示“: /usr/bin/env: "bash\r": 没有那个文件或目录
用 ./ 运行bash脚本文件出现 报错信息 /usr/bin/env: "bash\r": 没有那个文件或目录 错误原因:这主要是因为bash后面多了\r这个字符的原因.在lin ...
- nodejs安装 Later version of Node.js is already installed. Setup will now exit 及 node与npm版本不符
暴力删除nodejs导致无法重新安装 Later version of Node.js is already installed. Setup will now exit 1.电脑全局搜索nodej ...
- CodeForce-792C Divide by Three(数学)
Divide by Three CodeForces - 792C 有一个正整数 n 写在黑板上.它有不超过 105 位. 你需要通过删除一些位使得他变成一个美丽的数,并且需要删除尽量少的位数.删除的 ...
- webpack learn2-vue的jsx写法和postcss 1
首先输入命令安装 npm i postcss-loader autoprefixer babel-loader babel-core 在根目录创建文件 .babelrc和postcss.config. ...
- CentOS Linux 简单安装 clickhouse
本文只是仅仅的介绍安装 至于更多介绍请自信百度 1.本人 linux版本 [root@localhost /]# cat /etc/redhat-releaseCentOS Linux release ...
- dede5.7 给栏目添加上缩略图
如我们一个栏目列表都用缩略图来表示,而不仅仅只是文字,如果没有这项功能会非常麻烦,网上有很多这方面的资料,但是都试过了有很多问题,自己研究一下,测试基本通过.新加字段 typeimg后台执行SQL: ...
- Django边学边记—静态文件
概念 项目中的CSS.图片.js都是静态文件 一般会将静态文件放到一个单独的目录中,以方便管理 在html页面中调用时,也需要指定静态文件的路径,Django中提供了一种解析的方式配置静态文件路径 静 ...
- 如何理解 jmeter 的线程数与并发数之间的关系
https://blog.csdn.net/weixin_39955351/article/details/110548162 多个线程组的并发是如何计算的?