Mbatis——动态SQL
<?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的更多相关文章
- mbatis动态sql中传入list并使用
<!--Map:不单单forech中的collection属性是map.key,其它所有属性都是map.key,比如下面的departmentId --> <select id=&q ...
- 值得注意的ibatis动态sql语法格式
一.Ibatis常用动态sql语法,简单粗暴用一例子 <select id="iBatisSelectList" parameterClass="java.util ...
- Mysql - 游标/动态sql/事务
游标这个在我目前的项目里面用的还不多, 但是其功能还是很强大的. 动态sql以前都没用过, 是跟着富士康(不是张全蛋的富土康哦)过来的同事学的. 还是挺好用的. 我的数据库方面, 跟他学了不少. 在此 ...
- MyBatis4:动态SQL
什么是动态SQL MyBatis的一个强大特性之一通常是它的动态SQL能力.如果你有使用JDBC或其他相似框架的经验,你就明白条件串联SQL字符串在一起是多么地痛苦,确保不能忘了空格或者在列表的最后的 ...
- 分享公司DAO层动态SQL的一些封装
主题 公司在DAO层使用的框架是Spring Data JPA,这个框架很好用,基本不需要自己写SQL或者HQL就能完成大部分事情,但是偶尔有一些复杂的查询还是需要自己手写原生的Native SQL或 ...
- MySQL存储过程动态SQL语句的生成
用Mysql存储过程来完成动态SQL语句,使用存储过程有很好的执行效率: 现在有要求如下:根据输入的年份.国家.节假日类型查询一个节假日,我们可以使用一般的SQL语句嵌入到Java代码中,但是执行效率 ...
- 【Java EE 学习 79 下】【动态SQL】【mybatis和spring的整合】
一.动态SQL 什么是动态SQL,就是在不同的条件下,sql语句不相同的意思,曾经在“酒店会员管理系统”中写过大量的多条件查询,那是在SSH的环境中,所以只能在代码中进行判断,以下是其中一个多条件查询 ...
- 自定义函数执行动态sql语句
--函数中不能调用动态SQL,使用用存储过程吧.如果还要对函数做其他操作,换成存储过程不方便,可以考虑把其他操作一起封装在存储过程里面.如: create proc [dbo].[FUN_YSCL ...
- mybatis入门基础(五)----动态SQL
一:动态SQL 1.1.定义 mybatis核心对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 1.2.案例需求 用户信息综合查询列表这个statement的定义使用动态s ...
随机推荐
- LG4779 【模板】单源最短路径(标准版)
题意 给定一个 \(N\) 个点,\(M\) 条有向边的带非负权图,请你计算从 \(S\) 出发,到每个点的距离. 数据保证你能从 \(S\) 出发到任意点. \(1≤N≤100000\): \(1≤ ...
- Maven无法上传到到私服
在pom.xml中插入如下代码: <distributionManagement> <repository> <id>nexus</id> <ur ...
- Oracle Stream配置详细步骤
1 引言 Oracle Stream功能是为提高数据库的高可用性而设计的,在Oracle 9i及之前的版本这个功能被称为Advance Replication.Oracle Stream利用高级队列技 ...
- webservice有关application/xop+xml的异常
今天同事调用一个webservice时返回类似错误 响应消息的内容类型 multipart/related; type="application/xop+xml"; boundar ...
- Mybatis拦截器介绍及分页插件
1.1 目录 1.1 目录 1.2 前言 1.3 Interceptor接口 1.4 注册拦截器 1.5 Mybatis可拦截的方法 1.6 利用拦截器进行分页 1.2 前言 拦截器的一 ...
- PHP下的命令行执行 php -S localhost -t public(public是根目录,也是入口文件所在目录,是LARAVEL的)
PHP 的命令行模式 以下是 PHP 二进制文件(即 php.exe 程序)提供的命令行模式的选项参数,您随时可以通过 PHP -h 命令来查询这些参数. Usage: php [option ...
- 在rac集群上开启OEM
由于安装rac的时候没有开启oem,这里开启oem,方便管理 [oracle@rac01 ~]$ emca -config dbcontrol db -repos create -cluster ST ...
- ubuntu 14.04 no valid active connections found
ubuntu 14.04 强制重启后出现不能上网,点击connection information 后出现error: no valid active connections found 解决办法是在 ...
- Handler消息传送机制
一.什么是UI线程 当程序第一次启动的时候,Android会同时启动一条主线程( Main Thread). 主要负责处理与UI相关的事件. 二.UI线程存在的问题 出于性能优化考虑,Android的 ...
- gevent异步,io自动切换
#!/usr/bin/env python # encoding: utf-8 # Date: 2018/6/19 # # from gevent import monkey # 这俩行必须放在首 ...