动态sql语句主要为以下语句

1、动态SQL:if 语句
2、动态SQL:if+where 语句
3、动态SQL:if+set 语句
4、动态SQL:choose(when,otherwise) 语句
5、动态SQL:trim 语句
6、动态SQL: SQL 片段
7、动态SQL: foreach 语句

之前的几篇学习博客我用mybatis对一张表进行的CRUD操作,但是 SQL 语句都比较简单,如果有比较复杂的 SQL 语句,经常需要拼接,而拼接 SQL稍微不注意就容易出错此时我们可以用mybatis 动态SQL,通过 if, choose, when, otherwise, trim, where, set, foreach等标签来解决这个问题,这几个标签可组合成非常灵活的SQL语句,用起来也是很舒服

  1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
4 <!-- namespace:表示名称空间。现在的目的是区分id的. -->
5 <mapper namespace="com.zhiyou100.zhl.dao.UsersDao">
6 <sql id="detail">
7 id,name,age,sex,day
8 </sql>
9
10 <select id="selByWhere" resultType="com.zhiyou100.zhl.bean.Users">
11 select * from users
12 <where>
13 <if test="name!=null">
14 and name=#{name}
15 </if>
16 <if test="sex!=null and sex!=''">
17 and sex=#{sex}
18 </if>
19 </where>
20 </select>
21
22 <update id="updateWhere">
23 update users
24 <set>
25 <if test="name!=null">
26 name=#{name},
27 </if>
28 <if test="sex!=null">
29 sex=#{sex},
30 </if>
31 <if test="age>0">
32 age=#{age},
33 </if>
34 <if test="day!=null">
35 day=#{day}
36 </if>
37 </set>
38 where id=#{id}
39 </update>
40
41 <select id="selByWhere2" resultType="com.zhiyou100.zhl.bean.Users">
42 select
43 <include refid="detail"/>
44 from users
45 <where>
46 <choose>
47 <when test="name!=null and name!=''">
48 name like concat('%',#{name},'%')
49 </when>
50 <when test="sex!=null and sex!=''">
51 sex=#{sex}
52 </when>
53 <otherwise>
54 age>=#{age}
55 </otherwise>
56 </choose>
57 </where>
58 </select>
59
60 <select id="selByWhere3" resultType="com.zhiyou100.zhl.bean.Users">
61 select
62 <include refid="detail"/>
63 from users
64 <trim prefix="where" prefixOverrides="and | or">
65 <if test="name!=null and name!=''">
66 and name=#{name}
67 </if>
68 <if test="sex!=null and sex!=''">
69 and sex=#{sex}
70 </if>
71 <if test="age>0">
72 and age=#{age}
73 </if>
74 <if test="day!=null and day!=''">
75 and day=#{day}
76 </if>
77 </trim>
78 </select>
79
80 <update id="updateWhere2">
81 update users
82 <trim prefix="set" suffixOverrides=",">
83 <if test="name!=null">
84 name=#{name},
85 </if>
86 <if test="sex!=null">
87 sex=#{sex},
88 </if>
89 <if test="age>0">
90 age=#{age},
91 </if>
92 <if test="day!=null">
93 day=#{day}
94 </if>
95 </trim>
96 where id=#{id}
97 </update>
98
99 <delete id="deleteByIds">
100 delete from users
101 <where>
102 <foreach collection="ids" open="id in(" close=")" separator="," item="id">
103 #{id}
104 </foreach>
105 </where>
106 </delete>
107
108
109 <delete id="deleteByIds2">
110 delete from users where id in
111 <foreach collection="ids" open="(" close=")" separator="," item="id">
112 #{id}
113 </foreach>
114 </delete>
115
116
117 </mapper>

Mybatis学习三(动态sql语句)的更多相关文章

  1. mybatis 学习五 动态SQL语句

    3.1 selectKey 标签 在insert语句中,在Oracle经常使用序列.在MySQL中使用函数来自动生成插入表的主键,而且需要方法能返回这个生成主键.使用myBatis的selectKey ...

  2. Mybatis映射文件动态SQL语句-01

    因为在很多业务逻辑复杂的项目中,往往不是简单的sql语句就能查询出来自己想要的数据,所有mybatis引入了动态sql语句, UserMapper.xml <?xml version=" ...

  3. mybatis学习之动态sql

    mybatis的动态sql语句很强大,在mapper映射文件中使用简单的标签即可实现该效果,下面一个个记录: 1.select查询 简单的select类似如下: <select id=" ...

  4. mybatis中的动态SQL语句

    有时候,静态的SQL语句并不能满足应用程序的需求.我们可以根据一些条件,来动态地构建 SQL语句. 例如,在Web应用程序中,有可能有一些搜索界面,需要输入一个或多个选项,然后根据这些已选择的条件去执 ...

  5. MyBatis之五:动态sql语句

    在mybatis 3 或以上的版本提供了4类标签,分别是:if,choose(when,otherwise),rim(where,set),foreach.接下来将分别介绍这几种标签的具体用法,映射x ...

  6. mybatis学习 十 动态 SQL

    1.  根据方法传入的参数不同执行不同的 SQL 命令.称为动态 SQL, MyBatis 中动态 SQL 就是在 mapper.xml 中添加逻辑判断等. 2. <if>标签 <s ...

  7. Mybatis学习笔记-动态SQL

    概念 根据不同环境生成不同SQL语句,摆脱SQL语句拼接的烦恼[doge] 本质:SQL语句的拼接 环境搭建 搭建数据库 CREATE TABLE `blog`( `id` VARCHAR(50) N ...

  8. 阶段3 1.Mybatis_08.动态SQL_01.mybatis中的动态sql语句-if标签

    创建新的工程 复制到新建的项目里面 pom.xml依赖部分复制过来 dao中整理代码 只保留四个查询 映射文件也只保留四个查询方法 增加一个根据条件查询的方法. 由于用了别名,所以parpameter ...

  9. Mybatis映射文件动态SQL语句-02

    foreach UserMapper.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYP ...

  10. MyBatis学习 之 三、动态SQL语句

    目录(?)[-] 三动态SQL语句 selectKey 标签 if标签 if where 的条件判断 if set 的更新语句 if trim代替whereset标签 trim代替set choose ...

随机推荐

  1. MongoDB4.0.11服务没有响应控制功能解决办法

    如图 MongDB安装好后启动服务失败 解决办法 进入到你的MongDB bin目录下执行 mongod.exe --remove --serviceName "MongoDB" ...

  2. 线段树(SegmentTree)

    对于数组应用于区间染色实现为On,而线段树是O(logn) 什么是线段树:对于一个二叉树,每一个节点存储的是一个线段或是一个区间相应的信息. 查询 更新 #pragma once #include & ...

  3. [Unity3D] 使用LineRenderer实现尾迹与虚线效果

    Unity3D 使用LineRenderer绘制尾迹与虚线 1.添加LineRenderer组件 先创建一个3D对象,然后点击Add Component选项 搜索并添加LineRenderer组件 添 ...

  4. list集合中的实现类LinkedList

    LinkedList: 底层是一个双向链表,方便数据的频繁出入.便于快速插入,删除元素,不太方便进行查询 toArray(): 以正确的顺序(从第一个到最后一个素)返回一个包含此列表中所有元素的数组 ...

  5. #交互,鸽笼原理#CF1776C Library game

    题目 有一个长度为 \(m\) 的书架,以及 \(n\) 个长度 \(a_1,a_2,\dots,a_n\) Alessia 和 Bernardo 从书架上取书.每次由 Alessia 选择一个之前没 ...

  6. #树状数组,并查集#CF920F SUM and REPLACE

    题目 分析 由于\(a_i=1或2\)时\(d(a_i)=a_i\),且其余情况修改后答案只会越来越小, 考虑用树状数组维护区间和,用并查集跳过\(a_i=1或2\)的情况 代码 #include & ...

  7. jcmd:JDK14中的调试神器

    目录 简介 jcmd的语法 列出运行的JVM 打印stack信息 打印heap info 打印heap dump 统计heap使用情况 JFR功能 总结 简介 jcmd是JDK自带的调试工具,具有非常 ...

  8. OpenHarmony设备截屏的5种方式

      本文转载自<OpenHarmony设备截屏的5种方式 >,作者westinyang 目录 ● 方式1:系统控制中心 ● 方式2:OHScrcpy投屏工具 `推荐` ● 方式3:DevE ...

  9. HDC2021技术分论坛:HarmonyOS内核技术大揭秘!

    作者:jikecheng,miaoxie,HarmonyOS内核技术专家 HarmonyOS整体框架分为四个层级,如图1所示.从上到下,依次为:第一层是应用层,主要涵盖系统应用.Launcher.设置 ...

  10. 简单3步,OpenHarmony上跑起ArkUI分布式小游戏

    转自:OpenAtom OpenHarmony 在9月30日更新的 OpenHarmony3.0 LTS 上,标准系统新增支持了方舟开发框架(ArkUI).分布式组网和 FA 跨设备迁移能力等新特性, ...