<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 ...
随机推荐
- aarch64-linux-gnu交叉编译Qt4.7.3
到 Qt 官网下载合适的 Qt 版本,地址:http://download.qt-project.org/archive/qt/ 1.环境搭建: 1.安装automake.libtool 和主机上的 ...
- 依赖注入【转自知乎 PHP】
第一章:小明和他的手机 从前有个人叫小明 小明有三大爱好,抽烟,喝酒…… 咳咳,不好意思,走错片场了.应该是逛知乎.玩王者农药和抢微信红包 <img src="https://pic1 ...
- Linux在终端启动程序关闭终端不退出的方法
一般情况下关闭终端时,那么在这个终端中启动的后台程序也会终止,要使终端关闭后,后台程序保持执行,使用这个指令: nohup 命令 & 如:nohup ./studio.sh & 网上其 ...
- android压力测试命令monkey详解【转】
本文转载自:http://www.jb51.net/article/48557.htm 作者: 字体:[增加 减小] 类型:转载 时间:2014-03-29我要评论 这篇文章主要介绍了android ...
- [ZJOI 2007] 矩阵游戏
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1059 [算法] 二分图最大匹配 时间复杂度 : O(N^3) [代码] #inclu ...
- ZOJ 3955 Saddle Point 校赛 一道计数题
ZOJ3955 题意是这样的 给定一个n*m的整数矩阵 n和m均小于1000 对这个矩阵删去任意行和列后剩余一个矩阵为M{x1,x2,,,,xm;y1,y2,,,,,yn}表示删除任意的M行N列 对于 ...
- redis之简单动态字符串(SDS)
O(N):时间复杂度 N的值越大 时间复杂度随N的平方增大 O(1):就是说N很大的时候,复杂度基本不增长了.基本就是常量了. 在Redis数据库里 包含字符串值的键值对 在底层都是由SDS实现的. ...
- 为什么要用Go语言做后端
FMZ数字货币量化平台 www.fmz.com, 后端使用Go语言,这里是创始人Zero谈论使用Go语言所带了的便利.原帖地址:https://www.zhihu.com/question/27172 ...
- redis在linux安装和开机启动和结合php运用方法一
第一部分:安装redis 希望将redis安装到此目录 1 /usr/local/redis 希望将安装包下载到此目录 1 /usr/local/src 那么安装过程指令如下: 1 2 3 4 5 6 ...
- 转 MySQL实验(三) 过程式数据库对象的使用
转 http://blog.csdn.net/anne999/article/details/70432558