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 ...
随机推荐
- 几个开源faas 框架
funktion open source event based lambda programming for kubernetes 官方地址: funktion.fabric8.io serverl ...
- ORA-12541:无监听错误解决办法
http://jingyan.baidu.com/article/03b2f78c7a0ab75ea237ae33.html 1. 从开始菜单中打开“Oracle Net Configuratio ...
- tensorflow dropout
我们都知道dropout对于防止过拟合效果不错dropout一般用在全连接的部分,卷积部分不会用到dropout,输出曾也不会使用dropout,适用范围[输入,输出)1.tf.nn.dropout( ...
- 找到div下的第一个ul
$("div#div的id ul li a")//选择的是div下 ul下所有li下的所有a标签 $("div#div的id").children(" ...
- Eclipse执行import命令导入maven项目时报错:Add a version or custom suffix using "Name template" in "Advanced" settings
新建了两个maven项目在E盘workspace目录,后面移到workspace/app_engine目录下提交svn,再通过Eclipse的File->import导入时报错了: Projec ...
- 转 : 配置 mysql-advanced-5.6.21-winx64 免安装版
mySQL包:mysql-advanced-5.6.21-winx64.zip 下载地址:https://edelivery.oracle.com/EPD/Search/handle_go 服务器版本 ...
- laravel 中条件查询 function模式
当需要条件查找时,可以使用下面的注入方法: //我要预约 yudoc_name yudoc_keshi yudoc_jibing yudoc_hospital 这是需要帅选的条件 public fun ...
- Linux环境下安装XAMPP的PHP的PDF扩展
安装pdf扩展1. wget http://pecl.php.net/get/pdflib-4.1.2.tgz2. tar zxvf pdflib-4.1.2.tgz3. cd pdflib-4.1. ...
- 登陆验证系统实例-三种(cookie,session,auth)
登陆验证 因为http协议是无状态协议,但是我们有时候需要这个状态,这个状态就是标识 前端提交from表单,后端获取对应输入值,与数据库对比,由此对象设置一个标识,该对象 在别的视图的时候,有此标识, ...
- USB设备驱动总结
现象:把USB设备接到PC (韦老师总结) 1. 右下角弹出"发现android phone" 2. 跳出一个对话框,提示你安装驱动程序 问1. 既然还没有" ...