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 ...
随机推荐
- c# 统计运行时间
long startTime = Environment.TickCount; long endTime = Environment.TickCount; long totalTime = endTi ...
- 十、python沉淀之路--高阶函数初识
一.高阶函数:分两种:一种是返回值中包含函数体:另一种是把一个函数体当作了参数传给了另一个函数 1.返回值中包含函数体 例1. def test(): print('这是一个测试') return t ...
- [樹莓派]用mkusb来制作U盘启动安装Ubuntu 15.04
之前實踐過這文章的描述,還可以成功:http://www.linuxdiyf.com/linux/12719.html,轉記錄餘下: 官方英文文档,教你在Ubuntu 15.04下使用mkusb来制作 ...
- 获取DOS命令的返回值.
procedure CheckResult(b: Boolean); begin if not b then raise Exception.Create(SysErrorMessage(GetLas ...
- 使用Spring和Tomcat发布CXF SOAP WebService
上一节中使用代理工厂JaxWsProxyFactoryBean来发布WebService, 这种方式必须指定运行的端口,如果端口被占用,就会发布失败. cxf的WebService也可利用Tomcat ...
- SQL万能密码:' or 1='1
select name,pass from tbAdmin where name='admin' and pass='123456' 输入用户名:' or 1='1 SQL变成下面这个样子: sele ...
- WebSocket实战之JavaScript例子
一.详细代码案例 详细解读一个简单html5 WebSocket的Js实例教程,附带完整的javascript websocket实例源码,以及实例代码效果演示页面,并对本实例的核心代码进行了深入解读 ...
- DEV CheckComboboxEdit、CheckedListBoxControl(转)
CheckComboboxEdit //先清空所有,若在窗体Load事件中,也可以不清空 //cbRWYs.Properties.Items.Clear(); var RwyList = tspro. ...
- js(react.js) button click 事件无法触发
今天遇到一个诡异的问题.button 上的点击事件触发不了. 找个几个小时,原因是 js 报错了. <Button type="primary" htmlType=" ...
- Linux系统SCSI磁盘扫描机制解析及命令实例(转)
转载请在文首保留原文出处:EMC中文支持论坛 介绍 Linux系统扫描SCSI磁盘有几种方式?Linux新增LUN之后,能否不重启主机就认出设备?如果安装了PowerPath,动态添加/删除LUN的命 ...