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 ...
随机推荐
- Kubernetes的安装部署
前言:简述kubernetes(k8s)集群 k8s集群基本功能组件由master和node组成. master节点上主要有kube-apiserver.kube-scheduler.kube-con ...
- 前后端数据交互(六)——ajax 、fetch 和 axios 优缺点及比较
一.ajax.fetch 和 axios 简介 1.1.ajax ajax是最早出现发送后端请求的技术,属于原生 js .ajax使用源码,请点击<原生 ajax 请求详解>查看.一般使用 ...
- Windows下安装Apollo时的几个常见问题
今天在本地安装Apollo时遇到几个问题,觉得还是记录下来,希望能给有需要的朋友提供帮助. 安装的过程参考这篇教程,https://www.jianshu.com/p/6cf4b15ba82f.流程基 ...
- markdown的骚气操作(一)
markdown 系列其他内容 markdown的骚气操作(一)✓ latex的骚气操作(二) 本文目标 主要介绍markdown锚点.索引脚注.对勾及选择框.表格显示位置和符号显示位置.绘制 ...
- 简说yuv
最近弄了一个读取y4m文件转成yuv的流的事情,记录一些yuv相关的细节 为什么会有yuv 因为我们目前的显示器显示的原理都是三原色,几乎所有的视频数据最后都要转为rgb格式才能渲染到显示屏上,而原始 ...
- c# 递归树形菜单
首先创建模型类Menus public class Menus { //菜单Id public int Id { get; set; } //菜单名 public string MenuName { ...
- (6)java Spring Cloud+Spring boot+mybatis企业快速开发架构之SpringCloud-Spring Boot项目详细搭建步骤
在 Spring Tools 4 for Eclipse 中依次选择 File->New->Maven Project,然后在出现的界面中按图所示增加相关信息. <paren ...
- Dockerfile 自动制作 Docker 镜像(三)—— 镜像的分层与 Dockerfile 的优化
Dockerfile 自动制作 Docker 镜像(三)-- 镜像的分层与 Dockerfile 的优化 前言 a. 本文主要为 Docker的视频教程 笔记. b. 环境为 CentOS 7.0 云 ...
- Node.js躬行记(10)——接口日志查询
当运营向我们上报BUG时,我们第一时间是捕获相关的接口.从监控系统中,就可以查到用户使用时接口的请求和响应数据. 若接口的请求正常,那么就需要深入到接口代码中,查看相关的日志,通常会先浏览数据库查询语 ...
- django中csrf_token处理方式
第一:先在HTML中加入{% csrf_token %} $.ajax({ url: '{% url "ceshi:list" %}', type: 'post', dataTyp ...