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. [JSOI 2016] 最佳团体

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4753 [算法] 很明显的分数规划 可以用树形动态规划(树形背包)检验答案 时间复杂度 ...

  2. js数值型遇0开始自动转换为8进制

    如题,今天在项目更新时发现了js的这个自动转换问题,代码如下: var num = 0110; render:function(num){       var html="<a hre ...

  3. Objective-C 声明属性

    创建: 2018/01/24 完成: 2018/01/25 遗留: TODO 声明属性(declared property)  属性的声明与功能  属性的声明 @property 读写 @proper ...

  4. MySQL基础 — 详细安装

    MySQL--安装 打开MySQL 5.5 安装文件开始: 点击Next 打上勾,再点击Next 点击Custom,说明如下: Typical(典型安装)        Installs the mo ...

  5. Java多线程(三)SimpleDateFormat

    多线程报错:java.lang.NumberFormatException: multiple points SimpleDateFormat是非线程安全的,在多线程情况下会有问题,在每个线程下得各自 ...

  6. OpenSSL 1.0.0生成p12、jks、crt等格式证书的命令个过程

    OpenSSL 1.0.0生成p12.jks.crt等格式证书的命令个过程   此生成的证书可用于浏览器.java.tomcat.c++等.在此备忘!     1.创建根证私钥命令:openssl g ...

  7. 20 如何在C#中存一批数据,数组

    使用软件的一个重要原因,是因为软件可以帮我们重复处理很多事情.在前面我们已经讲到了循环.循环就是为了重复处理一个事情.那么我们有没有想过,我们要重复处理的一批数据怎么在程序里存放呢? 举个例子吧. 我 ...

  8. mysql timeout expired处理

    一.发现问题 二.分析问题 .net长时间连接mysql导致超时: 方式一:连接用完后,就关闭连接 方式二:增加C#的执行sqlcommand时间 三.解决问题 增加了这一句,问题解决了 using ...

  9. poj2289 Jamie's Contact Groups

    思路: 二分+最大流.实现: #include <stdio.h> #include <stdlib.h> #include <limits.h> #include ...

  10. Angular JS (2)

    通过Angular JS的官方教学文档,了解 routeProvider 的用法, angular.module('aaa').config(['$locationProvider','$routeP ...