<MyBatis>入门六 动态sql
package org.maple.mapper; import org.apache.ibatis.annotations.Param;
import org.maple.pojo.Employee; import java.util.List;
import java.util.Map; /**
* @author mapleins
* @Date 2018-12-13 17:39
* @Desc 动态sql的接口
**/
public interface EmployeeMapperDynamicSQL { /**
* 测试if和where
*/
List<Employee> getEmpByConditionIf(Employee employee); /**
* 测试trim
*/
List<Employee> getEmpByConditionTrim(Employee employee); /**
* 测试choose
*/
List<Employee> getEmpByConditionChoose(Employee employee); /**
* 测试set
*/
void updateEmp(Employee employee); /**
* 测试foreach循环list
*/
List<Employee> getEmpByConditionForeachList(@Param("ids") List ids); /**
* 测试foreach循环map
*/
List<Employee> getEmpByConditionForeachMap(@Param("map") Map map); /**
* 测试foreach插入数据
*/
Integer addEmps(@Param("emps") List<Employee> emps);
}
<?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="org.maple.mapper.EmployeeMapperDynamicSQL"> <!-- =================================sql片段的使用====================================== -->
<sql id="selectEmp">
SELECT id,last_name name,gender,email from tbl_employee
</sql> <!-- =================================if标签的使用====================================== -->
<!--if test="" 进行判断,如果为true,则拼装,否则不拼装-->
<!--where 后可以跟 1=1 -->
<!-- =================================where标签的使用====================================== -->
<!--3.ognl
1.where 1=1
2.使用where标签,只会去掉第一个多出来的and或者or
-->
<!-- 查询员工,要求:携带了哪个字段查询条件就带上这个字段的值 -->
<select id="getEmpByConditionIf" resultType="org.maple.pojo.Employee">
<include refid="selectEmp"/>
<where>
<if test="id!=null">
id =#{id}
</if>
<if test="name!=null and name!=""">
and last_name like concat('%',#{name},'%')
</if>
<if test="gender==0 or gender == 1">
and gender = #{gender}
</if>
<if test="email!=null and email!=""">
and email = #{email}
</if>
</where>
</select> <!-- =================================trim标签的使用====================================== -->
<!--4.trim标签
prefix="" 给trim标签拼串后的结果 最前面加一个字符
prefixOverrides="" 去掉整个字符串前面多余的字符
suffix="" 给trim标签拼串后的结果 最后面加一个字符
prefixOverrides="" 去掉整个字符串最后面多余的字符
-->
<select id="getEmpByConditionTrim" resultType="org.maple.pojo.Employee">
SELECT id,last_name name,gender,email from tbl_employee
<trim prefix="where" prefixOverrides="" suffixOverrides="and">
<if test="id!=null">
id =#{id} and
</if>
<if test="name!=null and name!=""">
last_name like concat('%',#{name},'%') and
</if>
<if test="gender==0 or gender == 1">
gender = #{gender} and
</if>
<if test="email!=null and email!=""">
email = #{email}
</if>
</trim>
</select> <!-- ==============================choose when,otherwise标签的使用====================================== -->
<!--如果带了id,就用id查,如果带了name,就用name查,只会进入其中一个-->
<select id="getEmpByConditionChoose" resultType="org.maple.pojo.Employee">
SELECT id,last_name name,gender,email from tbl_employee
<where>
<choose>
<when test="id!=null">
id = #{id}
</when>
<when test="name!=null and name!=""">
last_name like concat('%',#{name},'%')
</when>
<when test="email!=null and email!=""">
email = #{email}
</when>
<otherwise>
gender = 0;
</otherwise>
</choose>
</where>
</select> <!-- =============================set标签的使用====================================== -->
<!--会把多余的逗号去掉,也可以使用trim来做-->
<update id="updateEmp">
update tbl_employee
<set>
<if test="name!=null and name!=""">
last_name = #{name},
</if>
<if test="gender==0 or gender == 1">
gender = #{gender},
</if>
<if test="email!=null and email!=""">
email = #{email},
</if>
</set>
<where>
id = #{id}
</where>
</update> <!-- =============================foreach标签的使用====================================== -->
<!--list的遍历 item是当前值,index是list的索引-->
<select id="getEmpByConditionForeachList" resultType="org.maple.pojo.Employee">
SELECT id,last_name name,gender,email
FROM tbl_employee
WHERE id
<foreach collection="ids" item="id" open="in (" close=")" separator="," index="i">
#{id}
</foreach>
</select> <!--map的遍历 index是map的key,item是map的值-->
<select id="getEmpByConditionForeachMap" resultType="org.maple.pojo.Employee">
SELECT id,last_name name,gender,email
FROM tbl_employee
WHERE id
<foreach collection="map" item="id" open="in (" close=")" separator=",">
#{id}
</foreach>
</select> <!--foreach 的批量插入-->
<insert id="addEmps">
INSERT INTO tbl_employee(last_name, gender, email)
VALUES
<foreach collection="emps" item="emp" separator=",">
(#{emp.name},#{emp.gender},#{emp.email})
</foreach>
</insert> </mapper>
<MyBatis>入门六 动态sql的更多相关文章
- Mybatis入门之动态sql
Mybatis入门之动态sql 通过mybatis提供的各种标签方法实现动态拼接sql. 1.if.where.sql.include标签(条件.sql片段) <sql id="sel ...
- mybatis入门基础----动态SQL
原文:http://www.cnblogs.com/selene/p/4613035.html 阅读目录 一:动态SQL 二:SQL片段 三:foreach 回到顶部 一:动态SQL 1.1.定义 m ...
- Mybatis框架基础入门(六)--动态sql
主要是通过mybatis提供的各种标签方法实现动态拼接sql. 1.if标签 <!-- 根据条件查询用户 --> <select id="queryUserByWhere& ...
- 【mybatis深度历险系列】mybatis中的动态sql
最近一直做项目,博文很长时间没有更新了,今天抽空,学习了一下mybatis,并且总结一下.在前面的博文中,小编主要简单的介绍了mybatis中的输入和输出映射,并且通过demo简单的介绍了输入映射和输 ...
- mybatis 详解------动态SQL
mybatis 详解------动态SQL 目录 1.动态SQL:if 语句 2.动态SQL:if+where 语句 3.动态SQL:if+set 语句 4.动态SQL:choose(when,o ...
- mybatis中的动态SQL
在实际开发中,数据库的查询很难一蹴而就,我们往往要根据各种不同的场景拼接出不同的SQL语句,这无疑是一项复杂的工作,我们在使用mybatis时,mybatis给我们提供了动态SQL,可以让我们根据具体 ...
- Mybatis映射文件动态SQL语句-01
因为在很多业务逻辑复杂的项目中,往往不是简单的sql语句就能查询出来自己想要的数据,所有mybatis引入了动态sql语句, UserMapper.xml <?xml version=" ...
- Mybatis学习笔记(六) —— 动态sql
通过mybatis提供的各种标签方法实现动态拼接sql. 需求:根据性别和名字查询用户 查询sql: SELECT id, username, birthday, sex, address FROM ...
- MyBatis实战之动态SQL
如果使用JDBC或者其他框架,很多时候你得根据需要去拼接SQL,这是一个麻烦的事情,而MyBatis提供对SQL语句动态的组装能力,而且它只有几个基本的元素,非常简单明了,大量的判断都可以在MyBat ...
随机推荐
- GeHost powershell
PS C:\Users\clu\Desktop> [System.Net.Dns] | Get-Member -Static | Format-Table -AutoSize TypeName: ...
- iOS 开发 公司开发者账号,在多台Mac上合作开发,共用一个账号和证书--图文详解
参考 导出证书申请的MAC里的Xcode的开发者账号 .developerprofile 导出的开发者账号文件.developerprofile 导出PKCS12既是.p12文件 所拷贝的资料, .d ...
- 利用JFreeChart生成区域图 (5) (转自 JSP开发技术大全)
利用JFreeChart生成区域图 (5) (转自 JSP开发技术大全) 14.5 利用JFreeChart生成区域图 通过JFreeChart插件只能生成普通效果的区域图,利用工厂类ChartFac ...
- luence优化速度
一. .索引优化背景 很多网站都有自己的搜索引擎,比如百度,搜狗等等,而他们每天添加的索引量可想而知多么庞大,所以为了能提升用户的搜索响应速度,好的优化方案必不可少:当然对于一些网站的站内搜索也很有必 ...
- Tensorflow学习笔记——张量、图、常量、变量(一)
1 张量和图 TensorFlow是一种采用数据流图(data flow graphs),用于数值计算的开源软件库.其中 Tensor 代表传递的数据为张量(多维数组),Flow 代表使用计算图进行运 ...
- [Codeforces 623A] Graph and String
[题目链接] http://codeforces.com/contest/623/problem/A [算法] 首先 , 所有与其他节点都有连边的节点需标号为'b' 然后 , 我们任选一个节点 , 将 ...
- 深入理解JMM(Java内存模型) --(三)顺序一致性
数据竞争与顺序一致性保证 当程序未正确同步时,就会存在数据竞争.Java内存模型规范对数据竞争的定义如下: 在一个线程中写一个变量, 在另一个线程读同一个变量, 而且写和读没有通过同步来排序. 当代码 ...
- Java Graphics2D类的绘图方法
Graphics2D继承自Graphics,它扩展了Graphics的绘图功能,拥有更强大的二维图形处理能力,提供对几何形状.坐标转换.颜色管理以及文字布局等更精确的控制. Graphics2D定义了 ...
- python实现汉诺塔程序
# 汉诺塔思想笔记# 认识汉诺塔的目标:把A柱子上的N个盘子移动到C柱子# 递归的思想就是把这个目标分解成三个子目标# 子目标1:将前n-1个盘子从a移动到b上# 子目标2:将最底下的最后一个盘子从a ...
- 237 Delete Node in a Linked List 删除链表的结点
编写一个函数,在给定单链表一个结点(非尾结点)的情况下,删除该结点. 假设该链表为1 -> 2 -> 3 -> 4 并且给定你链表中第三个值为3的节点,在调用你的函数后,该链表应变为 ...