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 ...
随机推荐
- list类型数据的操作指令
1. 结果是 3 2 1 还可以继续追加如下: 2. 3. 4.删除表头元素(最左侧的元素),并返回该元素 5. 6. 7.删除表尾的元素(最右侧的元素),并返回该元素 8.
- NOIP模拟「random·string·queen」
T1:random 我又来白剽博客了: 详细证明请看土哥 土哥写的不是很详细,我在这里详细写一下: 首先,对于f[n]的式子: 加一是那一个对的贡献,大C是选其余的几个数,\(2^ ...
- VS Code 搭建stm32开发环境
MCU免费开发环境 一般芯片厂家会提供各种开发IDE方案,通常其中就包括其自家的集成IDE,如: 意法半导体 STM32CubeIDE NXP Codewarrior TI CCS 另外也可以用ecl ...
- 学习PHP中好玩的Gmagick图像操作扩展的使用
在 PHP 的图像处理领域,要说最出名的 GD 库为什么好,那就是因为它不需要额外安装的别的什么图像处理工具,而且是随 PHP 源码一起发布的,只需要在安装 PHP 的时候添加上编译参数就可以了. G ...
- 一起学习PHP中GD库的使用(一)
又到了一个大家非常熟悉的库了,对于图像图形的处理来说,GD 库是 PHPer 们绕不过去的一道坎.从很早很早的 CMS 或者 Discuz 时代,各类开源软件在安装的时候就会明确地指出 GD 库是它们 ...
- jmeter监控linux服务器资源
https://blog.csdn.net/weixin_38102592/article/details/100136375 https://blog.csdn.net/liuqiuxiu/arti ...
- javascript wchar_t 宽字符 转化为 ascii字符码数组
String.prototype.charCodeAt String.fromCharCode() String.prototype.toUtfArray = function() { return ...
- python+selenium之浏览器滚动条操作
from selenium import webdriver import time #访问百度 driver=webdriver.Ie() driver.get("http://www.b ...
- pyQt5设计无边框窗口(二)
无边框,自定义窗口背景 from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * impor ...
- docker network 参数
一. 格式 docker network COMMAND 二.COMMAND 讲解 2.1 .docker network connect 格式 docker network connect [OPT ...