建表

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. springMVC学习总结(二) --springMVC表单处理、标签库、静态文件处理

    根据springMVC学习总结(一) --springMVC搭建 搭建项目 一.表单处理 1.创建两个java类 Student.java, StudentController.java. 2.在js ...

  2. Linux - 安装 ant

    官方下载地址 https://ant.apache.org/bindownload.cgi 旧版下载地址 https://archive.apache.org/dist/ant/binaries/ 挑 ...

  3. Identity基于角色的访问授权

    详情访问官方文档 例如,以下代码将访问权限限制为属于角色成员的用户的任何操作 AdministrationController Administrator : [Authorize(Roles = & ...

  4. 数据治理中Oracle SQL和存储过程的数据血缘分析

    数据治理中Oracle SQL和存储过程的数据血缘分析   数据治理中的一个重要基础工作是分析组织中数据的血缘关系.有了完整的数据血缘关系,我们可以用它进行数据溯源.表和字段变更的影响分析.数据合规性 ...

  5. 缩减Centos7xfs磁盘空间

    问题描述:df -h查看 root目录仅有20G空间,其余300G空间全在home目录下.xfs不可以直接缩减,所以只能删除xfs盘然后重新添加. 解决办法: 1. 注释想要删除的磁盘,此处以cent ...

  6. Python脚本运行出现语法错误:IndentationError:unexpected indent

    对于py来说典型错误就是缩进,,烦不胜烦,整理一下解决方法:一个python脚本,本来都运行好好的,然后写了几行代码,而且也都确保每行都对齐了,但是运行的时候,却出现语法错误: Indentation ...

  7. Java中int和short的转化

    例子[1]: 第一种情况: short a = 1; a = a + 1; // 这一步会报错 System.out.print(a); 编译器会报错,原因如下: 第二种情况: short a = 1 ...

  8. 38KHz,NEC红外模拟发送和接收程序

    /*************************************************************************************************/ ...

  9. PHP的Hash信息摘要扩展框架

    今天我们主要学习的是 PHP 中一些 Hash 散列加密相关的扩展函数的使用,而不是 Hash 算法,这种加密其实也只是一种更复杂一些的密钥算法,与 Hash 算法类似的是,我们输入的一串字符串,就像 ...

  10. python学习笔记(十四)python实现发邮件

    import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart u ...