<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.nuch.edu.mapper.EmployeeDynamicSql"> <!--
• if:判断
• choose (when, otherwise):分支选择;带了break的swtich-case
如果带了id就用id查,如果带了lastName就用lastName查;只会进入其中一个
• trim 字符串截取(where(封装查询条件), set(封装修改条件))
• foreach 遍历集合
-->
<!-- 查询员工,要求,携带了哪个字段查询条件就带上这个字段的值 -->
<select id="getEmpsByConditionIf" resultType="com.nuch.edu.domain.Employee">
SELECT * FROM tbl_employee
<where>
<!-- test:判断表达式(OGNL)
OGNL参照PPT或者官方文档。
-->
<if test="id != null ">
id = #{id}
</if>
<!-- ognl会进行字符串与数字的转换判断 "0"==0 -->
<if test="gender==0 or gender ==1">
and gender = #{gender}
</if>
<if test="email != null and email.trim() != ''">
and email = #{email}
</if>
<if test="lastName != null and lastName.trim() != ''">
and last_name like #{lastName}
</if>
</where>
</select> <select id="getEmpsByConditionTrim" resultType="com.nuch.edu.domain.Employee">
SELECT * FROM tbl_employee
<!-- 后面多出的and或者or where标签不能解决
prefix="":前缀:trim标签体中是整个字符串拼串 后的结果。
prefix给拼串后的整个字符串加一个前缀
prefixOverrides="":
前缀覆盖: 去掉整个字符串前面多余的字符
suffix="":后缀
suffix给拼串后的整个字符串加一个后缀
suffixOverrides=""
后缀覆盖:去掉整个字符串后面多余的字符 -->
<!-- 自定义字符串的截取规则 -->
<trim prefix="where" prefixOverrides="and" suffix="" suffixOverrides="and">
<if test="id != null ">
id = #{id} and
</if>
<if test="gender==0 or gender ==1">
gender = #{gender} and
</if>
<if test="email != null and email.trim() != ''">
email = #{email} and
</if>
<if test="lastName != null and lastName.trim() != ''">
last_name like #{lastName} and
</if>
</trim>
</select> <select id="getEmpsByConditionChoose" resultType="com.nuch.edu.domain.Employee">
SELECT * FROM tbl_employee
<where>
<!-- 如果带了id就用id查,如果带了lastName就用lastName查;只会进入其中一个 -->
<choose>
<when test="id != null" >id = #{id}</when>
<when test="email != null and email.trim() != ''"> email = #{email}</when>
<when test="lastName != null and lastName.trim() !=''" >last_name = #{lastName}</when>
<otherwise>
gender = 1
</otherwise>
</choose>
</where>
</select> <update id="UpdateEmpsBySet">
update tbl_employee
<!-- Set标签的使用 -->
<set>
<if test="gender==0 or gender ==1">
gender = #{gender} ,
</if>
<if test="email != null and email.trim() != ''">
email = #{email} ,
</if>
<if test="lastName != null and lastName.trim() != ''">
last_name = #{lastName} ,
</if>
</set>
<where>
<if test="id != null ">
id = #{id}
</if>
</where>
</update> <select id="getEmpsByConditionCollection" resultType="com.nuch.edu.domain.Employee">
SELECT * from tbl_employee
WHERE id in
<!--
collection:指定要遍历的集合:
list类型的参数会特殊处理封装在map中,map的key就叫list
item:将当前遍历出的元素赋值给指定的变量
separator:每个元素之间的分隔符
open:遍历出所有结果拼接一个开始的字符
close:遍历出所有结果拼接一个结束的字符
index:索引。遍历list的时候是index就是索引,item就是当前值
遍历map的时候index表示的就是map的key,item就是map的值 #{变量名}就能取出变量的值也就是当前遍历出的元素
-->
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select>
<!-- 批量保存 -->
<!--MySQL下批量保存:可以foreach遍历 mysql支持values(),(),()语法-->
<!-- <insert id="addEmps">
INSERT INTO tbl_employee (gender,email,last_name,dept_id) VALUES
<foreach collection="emps" item="emp" separator=",">
(#{emp.gender},#{emp.email},#{emp.lastName},#{emp.dept.id})
</foreach>
</insert>--> <!-- 一次执行多个sql需要数据库连接属性allowMultiQueries=true;
这种分号分隔多个sql可以用于其他的批量操作(删除,修改) -->
<!--url=jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true-->
<insert id="addEmps">
<foreach collection="emps" item="emp" separator=";">
INSERT INTO tbl_employee (gender,email,last_name,dept_id) VALUES
(#{emp.gender},#{emp.email},#{emp.lastName},#{emp.dept.id})
</foreach>
</insert>
</mapper>

Mbatis——动态SQL的更多相关文章

  1. mbatis动态sql中传入list并使用

    <!--Map:不单单forech中的collection属性是map.key,其它所有属性都是map.key,比如下面的departmentId --> <select id=&q ...

  2. 值得注意的ibatis动态sql语法格式

    一.Ibatis常用动态sql语法,简单粗暴用一例子 <select id="iBatisSelectList" parameterClass="java.util ...

  3. Mysql - 游标/动态sql/事务

    游标这个在我目前的项目里面用的还不多, 但是其功能还是很强大的. 动态sql以前都没用过, 是跟着富士康(不是张全蛋的富土康哦)过来的同事学的. 还是挺好用的. 我的数据库方面, 跟他学了不少. 在此 ...

  4. MyBatis4:动态SQL

    什么是动态SQL MyBatis的一个强大特性之一通常是它的动态SQL能力.如果你有使用JDBC或其他相似框架的经验,你就明白条件串联SQL字符串在一起是多么地痛苦,确保不能忘了空格或者在列表的最后的 ...

  5. 分享公司DAO层动态SQL的一些封装

    主题 公司在DAO层使用的框架是Spring Data JPA,这个框架很好用,基本不需要自己写SQL或者HQL就能完成大部分事情,但是偶尔有一些复杂的查询还是需要自己手写原生的Native SQL或 ...

  6. MySQL存储过程动态SQL语句的生成

    用Mysql存储过程来完成动态SQL语句,使用存储过程有很好的执行效率: 现在有要求如下:根据输入的年份.国家.节假日类型查询一个节假日,我们可以使用一般的SQL语句嵌入到Java代码中,但是执行效率 ...

  7. 【Java EE 学习 79 下】【动态SQL】【mybatis和spring的整合】

    一.动态SQL 什么是动态SQL,就是在不同的条件下,sql语句不相同的意思,曾经在“酒店会员管理系统”中写过大量的多条件查询,那是在SSH的环境中,所以只能在代码中进行判断,以下是其中一个多条件查询 ...

  8. 自定义函数执行动态sql语句

    --函数中不能调用动态SQL,使用用存储过程吧.如果还要对函数做其他操作,换成存储过程不方便,可以考虑把其他操作一起封装在存储过程里面.如:   create proc [dbo].[FUN_YSCL ...

  9. mybatis入门基础(五)----动态SQL

    一:动态SQL 1.1.定义 mybatis核心对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 1.2.案例需求 用户信息综合查询列表这个statement的定义使用动态s ...

随机推荐

  1. 十、python沉淀之路--高阶函数初识

    一.高阶函数:分两种:一种是返回值中包含函数体:另一种是把一个函数体当作了参数传给了另一个函数 1.返回值中包含函数体 例1. def test(): print('这是一个测试') return t ...

  2. serf  简单使用

    1. 介绍 // 以下为官方介绍,说白了就是进行系统的集群节点管理 Serf uses an efficient gossip protocol to solve three major proble ...

  3. FastAdmin 在 CRUD 时出现 exec() has been disabled for security reasons 怎么办?

    FastAdmin 在 CRUD 时出现 exec() has been disabled for security reasons 怎么办? 有小伙伴提问 FastAdmin 在 CRUD 时出现 ...

  4. Java-Runoob:Java 基础语法

    ylbtech-Java-Runoob:Java 基础语法 1.返回顶部 1. Java 基础语法 一个 Java 程序可以认为是一系列对象的集合,而这些对象通过调用彼此的方法来协同工作.下面简要介绍 ...

  5. (转)Docker - 使用 compose 编排服务

    在园子里看到了这篇关于 compose 的文章,非常好!!转过来,先附上原文地址 http://www.cnblogs.com/52fhy/p/5991344.html ----- 入门示例 一般步骤 ...

  6. Py修行路 python基础 (二十一)logging日志模块 json序列化 正则表达式(re)

    一.日志模块 两种配置方式:1.config函数 2.logger #1.config函数 不能输出到屏幕 #2.logger对象 (获取别人的信息,需要两个数据流:文件流和屏幕流需要将数据从两个数据 ...

  7. PHP函数(一)-变量

    1.全局变量 <?php $a = 1; $b = 2; function test(){ echo $a + $b."<br>"; //运行结果为0 } tes ...

  8. java成神之——数值操作BigDecimal,BigInteger,Random,SecureRandom

    数值操作 数值新特性 包装类 浮点 BigDecimal BigInteger 数值本地化 随机数 假随机数 真随机数 播种 结语 数值操作 数值新特性 123_456 等价于 123456,增加可读 ...

  9. app端内容播放,web端的玩具,app通过websocket远程遥控玩具播放内容,玩具管理页面

    一.app端内容播放 下载代码 https://github.com/987334176/Intelligent_toy/archive/v1.0.zip 注意:由于涉及到版权问题,此附件没有图片和音 ...

  10. oracle——存储过程参数

    oracle 存储过程类型: 1.in:输入类型,即由应用程序将数据传入oracle存储过程中:这种参数在存储过程中是只读参数,在存储过程中无法对该类型的参数进行修改: 2.out:输出参数,是在存储 ...