Mybatis学习三(动态sql语句)
动态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语句)的更多相关文章
- mybatis 学习五 动态SQL语句
3.1 selectKey 标签 在insert语句中,在Oracle经常使用序列.在MySQL中使用函数来自动生成插入表的主键,而且需要方法能返回这个生成主键.使用myBatis的selectKey ...
- Mybatis映射文件动态SQL语句-01
因为在很多业务逻辑复杂的项目中,往往不是简单的sql语句就能查询出来自己想要的数据,所有mybatis引入了动态sql语句, UserMapper.xml <?xml version=" ...
- mybatis学习之动态sql
mybatis的动态sql语句很强大,在mapper映射文件中使用简单的标签即可实现该效果,下面一个个记录: 1.select查询 简单的select类似如下: <select id=" ...
- mybatis中的动态SQL语句
有时候,静态的SQL语句并不能满足应用程序的需求.我们可以根据一些条件,来动态地构建 SQL语句. 例如,在Web应用程序中,有可能有一些搜索界面,需要输入一个或多个选项,然后根据这些已选择的条件去执 ...
- MyBatis之五:动态sql语句
在mybatis 3 或以上的版本提供了4类标签,分别是:if,choose(when,otherwise),rim(where,set),foreach.接下来将分别介绍这几种标签的具体用法,映射x ...
- mybatis学习 十 动态 SQL
1. 根据方法传入的参数不同执行不同的 SQL 命令.称为动态 SQL, MyBatis 中动态 SQL 就是在 mapper.xml 中添加逻辑判断等. 2. <if>标签 <s ...
- Mybatis学习笔记-动态SQL
概念 根据不同环境生成不同SQL语句,摆脱SQL语句拼接的烦恼[doge] 本质:SQL语句的拼接 环境搭建 搭建数据库 CREATE TABLE `blog`( `id` VARCHAR(50) N ...
- 阶段3 1.Mybatis_08.动态SQL_01.mybatis中的动态sql语句-if标签
创建新的工程 复制到新建的项目里面 pom.xml依赖部分复制过来 dao中整理代码 只保留四个查询 映射文件也只保留四个查询方法 增加一个根据条件查询的方法. 由于用了别名,所以parpameter ...
- Mybatis映射文件动态SQL语句-02
foreach UserMapper.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYP ...
- MyBatis学习 之 三、动态SQL语句
目录(?)[-] 三动态SQL语句 selectKey 标签 if标签 if where 的条件判断 if set 的更新语句 if trim代替whereset标签 trim代替set choose ...
随机推荐
- Loto实践干货(3) 测量CAN总线通讯数据
Loto实践干货(3) 测量CAN总线通讯数据 最近在做运动控制卡的项目,调试样机的过程中,需要验证CAN总线通讯功能的正确性.以前只限于理论上认识CAN总线,使用的CANbus的通讯卡也是有上位机例 ...
- C# 二维码生成、识别,去除白边、任意颜色
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- salesforce零基础学习(一百三十五)项目中的零碎知识点小总结(七)
本篇参考: https://trailhead.salesforce.com/content/learn/modules/flow-implementation-2/debug-flows-as-an ...
- KingbaseES 实现 MySQL 函数 last_insert_id
用户从mysql迁移到金仓数据库过程中,应用中使用了mysql函数last_insert_id()来获取最近insert的那行记录的自增字段值. mysql文档中关于函数的说明和例子: LAST_IN ...
- Python---time对象的使用获取时间
https://www.jb51.net/article/49326.htm 1.time.time() 它返回当前时间的时间戳(1970纪元后经过的浮点秒数). 如果调用这个函数,如: print ...
- Go 实战|使用 Wails 构建轻量级的桌面应用:仿微信登录界面 Demo
概述 本文探讨 Wails 框架的使用,从搭建环境到开发,再到最终的构建打包,本项目源码 GitHub 地址:https://github.com/mazeyqian/go-run-wechat-de ...
- C++ While 和 For 循环:流程控制全解析
C++ Switch 语句 使用 switch 语句选择要执行的多个代码块之一. 语法 switch(expression) { case x: // 代码块 break; case y: // 代码 ...
- C# 关于e.Handled 的说明
e.Handled = false; KeyPressEventArgs.Handled 属性bai获取或设置一个值duzhi,该值指示是否dao处理zhuan过 KeyPress 事件.属性值类型: ...
- win10系统,软件不可用,无法调用摄像头
现象描述: 客户电脑是win10,定制带版权的电脑,安装的有卡巴斯基安全软件(最开始并不知道有这么个玩意),使用客户端软件,软件可以正常打开,但是软件无法打开摄像头画面(*:软件在其他电脑都是正常使用 ...
- HarmonyOS 设备管理开发:USB 服务开发指导
基本概念 USB服务是应用访问底层的一种设备抽象概念.开发者根据提供的USB API,可以获取设备列表.控制设备访问权限.以及与连接的设备进行数据传输.控制命令传输等. 运作机制 USB服务系统包 ...