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!=&quot;&quot;">
and last_name like concat('%',#{name},'%')
</if>
<if test="gender==0 or gender == 1">
and gender = #{gender}
</if>
<if test="email!=null and email!=&quot;&quot;">
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!=&quot;&quot;">
last_name like concat('%',#{name},'%') and
</if>
<if test="gender==0 or gender == 1">
gender = #{gender} and
</if>
<if test="email!=null and email!=&quot;&quot;">
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!=&quot;&quot;">
last_name like concat('%',#{name},'%')
</when>
<when test="email!=null and email!=&quot;&quot;">
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!=&quot;&quot;">
last_name = #{name},
</if>
<if test="gender==0 or gender == 1">
gender = #{gender},
</if>
<if test="email!=null and email!=&quot;&quot;">
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的更多相关文章

  1. Mybatis入门之动态sql

    Mybatis入门之动态sql 通过mybatis提供的各种标签方法实现动态拼接sql. 1.if.where.sql.include标签(条件.sql片段) <sql id="sel ...

  2. mybatis入门基础----动态SQL

    原文:http://www.cnblogs.com/selene/p/4613035.html 阅读目录 一:动态SQL 二:SQL片段 三:foreach 回到顶部 一:动态SQL 1.1.定义 m ...

  3. Mybatis框架基础入门(六)--动态sql

    主要是通过mybatis提供的各种标签方法实现动态拼接sql. 1.if标签 <!-- 根据条件查询用户 --> <select id="queryUserByWhere& ...

  4. 【mybatis深度历险系列】mybatis中的动态sql

    最近一直做项目,博文很长时间没有更新了,今天抽空,学习了一下mybatis,并且总结一下.在前面的博文中,小编主要简单的介绍了mybatis中的输入和输出映射,并且通过demo简单的介绍了输入映射和输 ...

  5. mybatis 详解------动态SQL

    mybatis 详解------动态SQL   目录 1.动态SQL:if 语句 2.动态SQL:if+where 语句 3.动态SQL:if+set 语句 4.动态SQL:choose(when,o ...

  6. mybatis中的动态SQL

    在实际开发中,数据库的查询很难一蹴而就,我们往往要根据各种不同的场景拼接出不同的SQL语句,这无疑是一项复杂的工作,我们在使用mybatis时,mybatis给我们提供了动态SQL,可以让我们根据具体 ...

  7. Mybatis映射文件动态SQL语句-01

    因为在很多业务逻辑复杂的项目中,往往不是简单的sql语句就能查询出来自己想要的数据,所有mybatis引入了动态sql语句, UserMapper.xml <?xml version=" ...

  8. Mybatis学习笔记(六) —— 动态sql

    通过mybatis提供的各种标签方法实现动态拼接sql. 需求:根据性别和名字查询用户 查询sql: SELECT id, username, birthday, sex, address FROM ...

  9. MyBatis实战之动态SQL

    如果使用JDBC或者其他框架,很多时候你得根据需要去拼接SQL,这是一个麻烦的事情,而MyBatis提供对SQL语句动态的组装能力,而且它只有几个基本的元素,非常简单明了,大量的判断都可以在MyBat ...

随机推荐

  1. 微软2016校园招聘在线笔试 [Recruitment]

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 A company plans to recruit some new employees. There are N ca ...

  2. [Codeforces 1037D] Valid BFS?

    [题目链接] http://codeforces.com/problemset/problem/1037/D [算法] 首先求出每个点的父节点 , 每棵子树的大小 然后判断BFS序是否合法即可 时间复 ...

  3. 洛谷 P1965 转圈游戏 —— 快速幂

    题目:https://www.luogu.org/problemnew/show/P1965 居然真的就只是 ( x + m * 10k % n ) % n 代码如下: #include<ios ...

  4. Could not find modernizr-2.6.2 in any of the sources

  5. vs code 快速生成vue 模板

    vs code 快速生成vue 模板 1.使用快捷Ctrl + Shift + P唤出控制台,然后输入snippets并选择.(或 文件>首选项>用户代码片断里面,输入 vue.json ...

  6. SP1557 GSS2 - Can you answer these queries II(线段树)

    传送门 线段树好题 因为题目中相同的只算一次,我们可以联想到HH的项链,于是考虑离线的做法 先把所有的询问按$r$排序,然后每一次不断将$a[r]$加入线段树 线段树上维护四个值,$sum,hix,s ...

  7. [App Store Connect帮助]五、管理构建版本(1)上传构建版本概述

    在您添加 App 至您的帐户之后,您可以使用 Xcode 或 Application Loader 来上传构建版本.稍后,您可以随着您 App 的更改上传更多构建版本.分发构建版本以供测试,或提交您的 ...

  8. 【转】@Controller和@RestController的区别

    知识点:@RestController注解相当于@ResponseBody + @Controller合在一起的作用. 1) 如果只是使用@RestController注解Controller,则Co ...

  9. echart 参数 vue配置 图文展示

    https://blog.csdn.net/she_lover/article/details/51448967 https://blog.csdn.net/n_meng/article/detail ...

  10. [BZOJ1331]魔板

    Description 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 我们知道魔板的每一个方格都有一种颜色.这8 ...