建表

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练习的更多相关文章

  1. mybatis实战教程(mybatis in action)之八:mybatis 动态sql语句

    mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类:1. if 语句 (简单的条件判断)2. c ...

  2. 9.mybatis动态SQL标签的用法

    mybatis动态SQL标签的用法   动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么 ...

  3. 自己动手实现mybatis动态sql

    发现要坚持写博客真的是一件很困难的事情,各种原因都会导致顾不上博客.本来打算写自己动手实现orm,看看时间,还是先实现一个动态sql,下次有时间再补上orm完整的实现吧. 用过mybatis的人,估计 ...

  4. Mybatis动态SQL单一基础类型参数用if标签

    Mybatis动态SQL单一基础类型参数用if标签时,test中应该用 _parameter,如: 1 2 3 4 5 6 <select id="selectByName" ...

  5. 超全MyBatis动态SQL详解!( 看完SQL爽多了)

    MyBatis 令人喜欢的一大特性就是动态 SQL. 在使用 JDBC 的过程中, 根据条件进行 SQL 的拼接是很麻烦且很容易出错的. MyBatis 动态 SQL 的出现, 解决了这个麻烦. My ...

  6. Mybatis动态SQL简单了解 Mybatis简介(四)

    动态SQL概况 MyBatis 的强大特性之一便是它的动态 SQL 在Java开发中经常遇到条件判断,比如: if(x>0){ //执行一些逻辑........ }   Mybatis应用中,S ...

  7. mybatis原理分析学习记录,mybatis动态sql学习记录

    以下个人学习笔记,仅供参考,欢迎指正. MyBatis 是支持定制化 SQL.存储过程以及高级映射的持久层框架,其主要就完成2件事情: 封装JDBC操作 利用反射打通Java类与SQL语句之间的相互转 ...

  8. mybatis 动态sql和参数

    mybatis 动态sql 名词解析 OGNL表达式 OGNL,全称为Object-Graph Navigation Language,它是一个功能强大的表达式语言,用来获取和设置Java对象的属性, ...

  9. MyBatis动态SQL之一使用 if 标签和 choose标签

    bootstrap react https://segmentfault.com/a/1190000010383464 xml 中 < 转义 to thi tha <if test=&qu ...

  10. 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 ...

随机推荐

  1. Vs code自动生成Doxygen格式注释

    前言 ​ 程序中注释的规范和统一性的重要性不言而喻,本文就推荐一种在用vscode编写代码时自动化生成标准化注释格式的方法,关于Doxygen规范及其使用可查看博文 代码注释规范之Doxygen. ​ ...

  2. MongoDB(15)- 查询操作里面的游标 cursor

    db.collection.find() 方法里面的游标 该方法最后会返回一个 cursor 正常情况下,访问文档必须迭代游标 重点事项 当调用 find() 函数时,Shell 并不立即查询数据库, ...

  3. jvm学习笔记:栈帧

    栈帧内的数据结构 局部变量表(Local Variables):记录非静态方法的this指针.方法参数.局部变量 操作数栈(Operand Stack):用于计算的栈结构 动态链接(Dynamic L ...

  4. Git:git commit时退出报错解决(Error45、Error325)

    Git 报错 在输入git commit编辑注释日志时强制退出git程式,文件会变成只读文件,于是出现下述报错: 解决方法(ERROR45) 我们提交代码的正常操作流程一般是: 输入git commi ...

  5. Sentry Web 前端监控 - 最佳实践(官方教程)

    系列 1 分钟快速使用 Docker 上手最新版 Sentry-CLI - 创建版本 快速使用 Docker 上手 Sentry-CLI - 30 秒上手 Source Maps Sentry For ...

  6. Flex语法和常用鼠标手势

    Flex弹性和模型 1.display : flex/inline-flex ;(设置给氟元素) flex : 将对象作为弹性伸缩盒显示: inline-flex : 将对象作为内联块级弹性伸缩显示: ...

  7. (2)java Spring Cloud+Spring boot+mybatis企业快速开发架构之SpringCloud-Spring Cloud是什么?Spring Cloud版本介绍

    ​ Spring Cloud 是一系列框架的有序集合.它利用 Spring Boot 的开发便利性,巧妙地简化了分布式系统基础设施的开发,如服务注册.服务发现.配置中心.消息总线.负载均衡.断路器.数 ...

  8. node实战小例子

    第一章 2020-2-6 留言小本子 思路(由于本章没有数据库,客户提交的数据放在全局变量,接收请求用的是bodyParser, padyParser使用方法 app.use(bodyParser.u ...

  9. C++快读讲解

    C++快读讲解 inline int read(){ int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-') ...

  10. Android——菜单(Menu)

    菜单的运用在Android中很常见,今天就两节体育课,闲下来也想认真的学一学,正好项目中也会有应用.我是跟着菜鸟教程进行学习的,我相应的粘了一些我自己认为比较重要的,以供方便记录学习. 本章给大家带来 ...