<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 ...
随机推荐
- 洛谷P1514 引水入城——dfs
题目:https://www.luogu.org/problemnew/show/P1514 搜索+DP: 自己想出来的方法第一次80分好高兴! 再改了改就A了,狂喜乱舞: 也就是 dfs,仔细一想第 ...
- 如何给mysql用户分配权限+增、删、改、查mysql用户
在mysql中用户权限是一个很重析 参数,因为台mysql服务器中会有大量的用户,每个用户的权限需要不一样的,下面我来介绍如何给mysql用户分配权限吧,有需要了解的朋友可参考. 1,Mysql下创建 ...
- ubuntu16.04 查看系统可用内存
free -m 查看内存情况 (单位MB) mem 行显示了从系统角度看来内存使用的情况, total是系统可用的内存大小, 数量上等于系统物理内存减去内核保留的内存. buffers和cach ...
- bzoj 1828: [Usaco2010 Mar]balloc 农场分配【贪心+线段树】
长得挺唬人的贪心,按照右端点排序,用最小值线段树的询问判断当前牛是否能放进去,能的话更新线段树,ans++ 来自https://www.cnblogs.com/rausen/p/4529245.htm ...
- [Swift]经典解题思路:联合查找Union Find
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [转]c 语言中 %d,%lu等区别
转载至:http://blog.sina.com.cn/s/blog_7d94c35c01019f96.html %d 有符号10进制整数 %ld 长整型 %hd短整型 %hu 无符号短整形 %u无符 ...
- linux C编程 gdb的使用
linux C编程 gdb的使用 通常来说,gdb是linux在安装时自带的,在命令行键入"gdb"字符并按回车键会启动gdb调试环境. 1.gdb的基本命令 命令 说明 file ...
- Linux命令(008) -- yum
yum命令(全称为Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包管理,能够从指定的服务器自动下 ...
- 一行python能做什么!
主要收集了平常遇到的代码和网上的简单题目,然后尝试将代码压缩到一行,仅仅是娱乐一下~~~ −−−−−(1)−−−−−−−−−−−(1)−−−−−− 用一行python写出一个嵌套的字符串. def p ...
- 240 Search a 2D Matrix II 搜索二维矩阵 II
编写一个高效的算法来搜索 m x n 矩阵中的一个目标值.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列.例如,考虑下面的矩阵:[ [1, 4, 7 ...