动态sql & 抽取可重用sql
抽取可重用的sql片段
抽取:<sql id="xx"></sql>
使用:<include refid="xx"></inculde>
<select id="getEmpsByDid" resultType="com.atguigu.mybatis.beans.Employee">
<include refid="selectEmployeeSQL"></include> from tbl_employee where d_id = #{did}
</select>
//抽取可重用的SQL片段
<sql id="selectEmployeeSQL">
select id ,last_name,email,gender
</sql>
动态sql
MyBatis 采用功能强大的基于 OGNL 的表达式来简化操作
OGNL( Object Graph Navigation Language )对象图导航语言,这是一种强大的
表达式语言,通过它可以非常方便的来操作对象属性。 类似于我们的EL,SpEL等
访问对象属性:person.name
调用方法: person.getName()
调用静态属性/方法:
调用构造方法:
运算符: +,-*,/,%
逻辑运算符: in ,not in ,> ,>= ,< ,<= ,== ,!= ,or,and
注意:xml中特殊符号如”,>,<等这些都需要使用转义字符
if where
1) If用于简单的判断.
2) Where用于解决SQL语句中where关键字以及条件中第一个and或者or的问题
// public List<Employee> getEmpsByConditionIfWhere(Employee Condition);
<select id="getEmpsByConditionIfWhere" resultType="com.atguigu.mybatis.beans.Employee">
select id, last_name, email, gender from tbl_employee
// where 1=1 处理所有条件不满足时where后为空的方法
<where>
//现在使用where标签也可完美解决
// 在SQL语句中提供WHERE关键字,并且要解决第一个条件就出现的and 或者是 or的问题
<if test="id!=null">
and id = #{id }
</if>
<if test="lastName!=null&&lastName!=""">
and last_name = #{lastName}
</if>
<if test="email!=null and email.trim()!=''">
and email = #{email}
</if>
<if test="gender==0 or gender==1">
and gender = #{gender}
</if>
</where>
</select>
choose(when,otherwise)
用于分支判断,类似于java中的switch case,只会满足所有分支中的一个
//public List<Employee> getEmpsByConditionChoose(Employee Condition);
<select id="getEmpsByConditionChoose" resultType="com.atguigu.mybatis.beans.Employee">
select id ,last_name, email,gender from tbl_employee
<where>
<choose>
<when test="id!=null">
id = #{id}
</when>
<when test="lastName!=null">
last_name = #{lastName}
</when>
<when test="email!=null">
email = #{email}
</when>
<otherwise>
gender = 'm'
</otherwise>
</choose>
</where>
</select>
choose 类似 switch
when 类似 case
otherwise 类似 default
trim
trim 可以在条件判断完的SQL语句前后 添加或者去掉指定的字符
| prefix | 添加前缀 |
|---|---|
| prefixOverrides | 去掉前缀 |
| suffix | 添加后缀 |
| suffixOverrides | 去掉后缀 |
//public List<Employee> getEmpsByConditionTrim(Employee Condition);
<select id="getEmpsByConditionTrim" resultType="com.atguigu.mybatis.beans.Employee">
select id , last_name ,email , gender
from tbl_employee
//添加where前缀,若后缀是and则删除
<trim prefix="where" suffixOverrides="and">
<if test="id!=null">
id = #{id} and
</if>
<if test="lastName!=null && lastName!=""">
last_name = #{lastName} and
</if>
<if test="email!=null and email.trim()!=''">
email = #{email} and
</if>
<if test=""m".equals(gender) or "f".equals(gender)">
gender = #{gender}
</if>
</trim>
</select>
set
用于解决修改操作中SQL语句中可能多出逗号的问题
//public void updateEmpByConditionSet(Employee Condition);
<update id="updateEmpByConditionSet">
update tbl_employee
<set>
<if test="lastName!=null and lastName!=''">
last_name = #{lastName},
</if>
<if test="email!=null and email.trim()!=''">
email = #{email} ,
</if>
<if test="gender==0 or gender==1">
gender = #{gender}
</if>
</set>
where id =#{id}
</update>
foreach
动态 SQL 的另外一个常用的必要操作是需要对一个集合进行遍历,通常是在构建 IN 条件语句的时候。
| foreach | 主要用于循环迭代 |
|---|---|
| item | 当前从集合中迭代出的元素 |
| collection | 要迭代的集合 |
| open | 开始字符 |
| close | 结束字符 |
| separator | 元素与元素之间的分隔符 |
| index | 迭代的是List集合: index表示的当前元素的下标 迭代的Map集合: index表示的当前元素的key |
// public List<Employee> getEmpsByIds(@Param("ids")List<Integer> ids );
<select id="getEmpsByIds" resultType="com.atguigu.mybatis.beans.Employee">
/*
select * from tbl_employee where id in(?,?,?);
select * from tbl_employee where id = ? or id = ? or id = ? */
select id ,last_name ,email, gender from tbl_employee
where id in
<foreach collection="ids" item="currId" open=" (" close=")" separator=",">
#{currId}
</foreach>
</select>
批量操作
添加:insert into tbl_employee(x,x,x) values(?,?,?),(?,?,?),(?,?,?)
删除:delete from tbl_employee where id in(?,?,?)
修改:待更新(麻烦一点,可自己寻找好的办法)
修改: update tbl_employee set last_name = #{lastName} ...where id = #{id};
update tbl_employee set last_name = #{lastName} ...where id = #{id};
update tbl_employee set last_name = #{lastName} ...where id = #{id};
默认情况下, JDBCB不允许将多条SQL通过;拼成一个字符串。
可以在连接的url后面加上一个参数: allowMultiQueries=true
//public void addEmps(@Param("emps")List<Employee> emps );
<insert id="addEmps">
insert into tbl_employee(last_name, email,gender ) values
<foreach collection="emps" item="emp" separator=",">
(#{emp.lastName},#{emp.email},#{emp.gender})
</foreach>
</insert>
动态sql & 抽取可重用sql的更多相关文章
- MyBatis中关于SQL标签的用法(重用SQL 代码段)
一. 没用sql标签前的SQL映射代码: <select id="findById" resultType="cn.tedu.mybatis.entity.User ...
- MyBatis_tp50_动态sql_sql标签_抽取可重用的sql片段_使用include标签进行引用
笔记要点出错分析与总结 include内部使用自定的属性,之能使用$ {}来取值 ,#{}不能用 工程组织数据库组织0.重新修改Bean类1.定义接口 public interface Employe ...
- mybatis动态sql中的sql标签——抽取可重用的sql片段
1.用<sql>标签抽取可重用的sql片段 <!-- 抽取可重用的SQL片段,方便后面引用 1.sql抽取,经常将要查询的列名,或者插入用的列名,之后方便引用 ...
- mapper.xml中动态sql抽取重复项
mabatis重点是通过标签对sql灵活的组织,通过配置的方式完成输入 输出映射. 1.对mapper.xml中重复的sql抽取统一维护,以及foreach使用 UserMapperCustom.xm ...
- 黑马MyBatisday2 MyBatis Dao层实现 接口代理实现&传统实现 动态SQL和SQL抽取 自定义类型处理 分页插件PageHelper
package com.itheima.mapper; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelp ...
- 利用MyBatis的动态SQL特性抽象统一SQL查询接口
1. SQL查询的统一抽象 MyBatis制动动态SQL的构造,利用动态SQL和自定义的参数Bean抽象,可以将绝大部分SQL查询抽象为一个统一接口,查询参数使用一个自定义bean继承Map,使用映射 ...
- 在Delphi中动态地使用SQL查询语句 Adoquery sql 参数 冒号
在Delphi中动态地使用SQL查询语句 在一般的数据库管理系统中,通常都需要应用SQL查询语句来提高程序的动态特性.下面介绍如何在Delphi中实现这种功能.在Delphi中,使用SQL查询语句的途 ...
- 阶段3 1.Mybatis_08.动态SQL_03.mybatis中动态sql语句-foreach和sql标签
foreach标签 in的查询 sql语句好写,但是传参在映射文件里面改怎么传呢 定义一个List<Integer>成员变量,然后生成get和set 定义一个新的查询方法 open:开始符 ...
- 使用mybatis的动态sql解析能力生成sql
需求: 计算平台,有很多表,打算提供一个基于sql的服务接口, sql不能完全在配置页面写死, 要能根据参数不同执行不同的语义,防止sql个数爆炸 把mybatis原码down下来, 改造一下测试用例 ...
随机推荐
- c++ 的学习 第3集-默认参数
1.默认参数的意思就是 予以形参默认值 2. C++允许函数设置默认参数,在调用时可以根据情况省略实参.规则如下: 默认参数只能按照右到左的顺序 如果函数同时有声明.实现,默认参数只能放在函数声 ...
- CF891B-Gluttony【构造】
正题 题目链接:https://www.luogu.com.cn/problem/CF891B 题目大意 给出\(n\)个数字互不相同的一个序列\(a\),求它的一个排列\(b\),使得选出任意一个\ ...
- YbtOJ#532-往事之树【广义SAM,线段树合并】
正题 题目链接:https://www.ybtoj.com.cn/problem/532 题目大意 给出\(n\)个点的一个\(Trie\)树,定义\(S_x\)表示节点\(x\)代表的字符串 求$$ ...
- IdentityServer4[4]使用密码保护API资源
使用密码保护API资源(资源所有者密码授权模式) 资源所有者(Resource Owner)就是指的User,也就是用户.所以也称为用户名密码模式.相对于客户端凭证模式,增加了一个参与者User.通过 ...
- RabbitMQ持久化机制、内存磁盘控制(四)
一.持久化 如果看到这一篇文章的朋友,都是有经验的开发人员,对持久化的概念就不用再做过多的解析了,经过前面的几篇文章,其实不难发现RabbitMQ 的持久化其实就只分交换器持久化.队列持久化和消息持久 ...
- 通过ideviceinstaller获取IOS APP bundleId
查看ios设备udid: idevice_id -l 查看ios应用的bundleId: # 安装ideviceinstaller brew install ideviceinstaller # 查看 ...
- 从0到1使用Kubernetes系列(三):使用Ansible安装Kubernetes集群
前两期的文章介绍了Kubernetes基本概念和架构,用Kubeadm+Ansible搭建Kubernetes集群所需要的工具及其作用.本篇介绍怎么使用Ansible安装Kubernetes集群. 启 ...
- 未来云原生 | CIF 论坛精彩看点
当下云原生技术正在飞速发展,那么如何准确理解「云原生」?在发展不够成熟,行业认知差异大的情况下,不论是云原生计算基金会(CNCF),还是行业的任何大咖,都不能给出精确的.便于理解的定义.我们要理解的逻 ...
- 命名空间、作用域、LEGB法则、垃圾回收机制
一.命名空间.作用域.LEGB法则. 1.命名空间和作用域 : 命名空间:变量名称与值的映射关系 作用域:变量作用的区域,即范围. 注意:class/def/模块会产生作用域:分支语句,循环语句,异常 ...
- TCC分布式事务的实现原理
目录 一.写在前面 二.业务场景介绍 三.进一步思考 四.落地实现TCC分布式事务 (1)TCC实现阶段一:Try (2)TCC实现阶段二:Confirm (3)TCC实现阶段三:Cancel 五.总 ...