【串线篇】Mybatis之动态sql
一、if标签
<select id="getTeacherByCondition" resultMap="teacherMap">
select * from t_teacher where
<if test="id!=null">
id > #{id} and
</if>
<if test="name!=null && !name.equals("")">
teacherName like #{name} and
</if>
<if test="birth!=null">
birth_date < #{birth} and
</if>
<!--"" “”的转义字符-- >
<!--&& &&的转义字符,或者直接写and妥了 -- >
<!-- <小于号的转义字符 -->
</select>
【注】
test属性指定JavaBean里的属性名
teacherName like #{name} and:teacherName是数据库表里的字段名
二、while标签
where可以帮我们去除掉前面的and;
id > #{id},and teacherName like #{name},and birth_date < #{birth}
意思是万一有比如第一句不满足,来到了第二句但第二局前面多了个and这时候where标签就会自动去除
三、trim标签
<!-- trim:截取字符串
prefix="":前缀;为我们下面的sql整体添加一个前缀
prefixOverrides="": 去除出整体字符串前面可能多余的字符
suffix="":为整体添加一个后缀
suffixOverrides="":后面可能哪个多了可以去掉; -->
<!-- 我们的查询条件就放在where标签中;每个and写在前面,
where帮我们自动取出前面多余的and -->
<trim prefix="where" prefixOverrides="and" suffixOverrides="and">
<if test="id!=null">
id > #{id} and
</if>
<if test="name!=null && !name.equals("")">
teacherName like #{name} and
</if>
<if test="birth!=null">
birth_date < #{birth} and
</if>
</trim>
</select>
四、foreach标签
<!-- public List<Teacher> getTeacherByIdIn(@Param("ids")List<Integer> ids);
<select id="getTeacherByIdIn" resultMap="teacherMap">
SELECT * FROM t_teacher WHERE id IN
<!-- 帮我们遍历集合的;
collection="":指定要遍历的集合的key
close="":以什么结束
index="i":索引;
如果遍历的是一个list;
index:指定的变量保存了当前索引
item:保存当前遍历的元素的值
如果遍历的是一个map:
index:指定的变量就是保存了当前遍历的元素的key
item:就是保存当前遍历的元素的值
item="变量名":每次遍历出的元素起一个变量名方便引用
open="":以什么开始
separator="":每次遍历的元素的分隔符
(#{id_item},#{id_item},#{id_item} -->
<if test="ids.size >0">
<foreach collection="ids" item="id_item" separator="," open="("
close=")">
#{id_item}
</foreach>
</if>
</select>
五、choose-when标签
<!--public List<Teacher> getTeacherByConditionChoose(Teacher teacher); -->
<select id="getTeacherByConditionChoose" resultMap="teacherMap">
select * from t_teacher
<where>
<choose>
<when test="id!=null">
id=#{id}
</when>
<when test="name!=null and !name.equals("")">
teacherName=#{name}
</when>
<when test="birth!=null">
birth_date = #{birth}
</when>
<otherwise>
1=1
</otherwise>
</choose>
</where>
</select>
【注】
1)、与if不同的是,当满足一个条件的时候when只会进一个,而if都会判断
2)、<otherwise>1=1</otherwise>执行所有查询,有几条返回几条
六、set标签完成mybatis动态更新
!-- public int updateTeacher(Teacher teacher); -->
<update id="updateTeacher">
UPDATE t_teacher
<set>
<if test="name!=null and !name.equals("")">
teacherName=#{name},
</if>
<if test="course!=null and !course.equals("")">
class_name=#{course},
</if>
<if test="address!=null and !address.equals("")">
address=#{address},
</if>
<if test="birth!=null">
birth_date=#{birth}
</if>
</set>
<where>
id=#{id}
</where>
</update>
【注】set就是sql语句中的set,他会帮我们自动去除可能多余的逗号
七、sql标签与include
1)、抽取sql到外面
<sql id=”selectSql”>select * from t_teacher</sql>
2)、内调用
<select id="getTeacherById" resultMap="teacherMap">
<include refid=”selectSql”></include>
where id=#{id}
</select>
【串线篇】Mybatis之动态sql的更多相关文章
- Mybatis解析动态sql原理分析
前言 废话不多说,直接进入文章. 我们在使用mybatis的时候,会在xml中编写sql语句. 比如这段动态sql代码: <update id="update" parame ...
- MyBatis 示例-动态 SQL
MyBatis 的动态 SQL 包括以下几种元素: 详细的使用参考官网文档:http://www.mybatis.org/mybatis-3/zh/dynamic-sql.html 本章内容简单描述这 ...
- MyBatis的动态SQL详解
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑,本文详解mybatis的动态sql,需要的朋友可以参考下 MyBatis 的一个强大的特性之一通常是它 ...
- mybatis 使用动态SQL
RoleMapper.java public interface RoleMapper { public void add(Role role); public void update(Role ro ...
- MyBatis框架——动态SQL、缓存机制、逆向工程
MyBatis框架--动态SQL.缓存机制.逆向工程 一.Dynamic SQL 为什么需要动态SQL?有时候需要根据实际传入的参数来动态的拼接SQL语句.最常用的就是:where和if标签 1.参考 ...
- 使用Mybatis实现动态SQL(一)
使用Mybatis实现动态SQL 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 写在前面: *本章节适合有Mybatis基础者观看* 前置讲解 我现在写一个查询全部的 ...
- MyBatis探究-----动态SQL详解
1.if标签 接口中方法:public List<Employee> getEmpsByEmpProperties(Employee employee); XML中:where 1=1必不 ...
- mybatis中的.xml文件总结——mybatis的动态sql
resultMap resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功. 如果sql查询字段名和pojo的属性名不一致,可以通过re ...
- mybatis.5.动态SQL
1.动态SQL,解决关联sql字符串的问题,mybatis的动态sql基于OGNL表达式 if语句,在DeptMapper.xml增加如下语句; <select id="selectB ...
- MyBatis的动态SQL详解-各种标签使用
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. MyBatis中用于实现动态SQL的元素主要有: if choose(when,otherwise) ...
随机推荐
- JavaScript中数组的操作方法总汇
Array(数组)是JavaScript中最为常用的类型了.ECMAScript中的数组都是数据的有序列表.数组中可以保存任何类型的数据.数组的大小是可以动态调整的,既可以随着数据的添加自动增长以容纳 ...
- Codeforces 850A - Five Dimensional Points(暴力)
原题链接:http://codeforces.com/problemset/problem/850/A 题意:有n个五维空间内的点,如果其中三个点A,B,C,向量AB,AC的夹角不大于90°,则点A是 ...
- 【Java】Java调用第三方接口
Get请求与Http请求 https://www.w3school.com.cn/tags/html_ref_httpmethods.asp HttpClient HTTP 协议可能是现在 Inter ...
- 30个有关Python的小技巧,给程序员的 30 个基本 Python 贴士与技巧
30个有关Python的小技巧 2013/07/04 · Python, 开发 · 4 评论 · Python 分享到: 66 本文由 伯乐在线 - Kevin Sun 翻译.未经许可,禁止转载!英文 ...
- (\w+)\s*, \s*(\w+)
\s表示空格 \w表示任何字符,字母数字下划线 _就表示下划线
- Git015--标签管理
Git--标签管理 本文来自于:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/ ...
- 关于VMWare的几种网络模式
具体的可以参考这个博文:http://zhenyaliu.blog.163.com/blog/static/2377571920103775447527/
- 《STL源码剖析》——第五、六:关联容器与算法
第五章.关联容器 5.0.关联容器 标准的STL关联式容器分为set(集合)和map(映射表)两大类,以及这两大类的衍生体multiset(多键集合)和multimap(多键映射表).这些容器的底层 ...
- jQuery基础--插件
1. 插件 1.1. 常用插件 插件:jquery不可能包含所有的功能,我们可以通过插件扩展jquery的功能. jQuery有着丰富的插件,使用这些插件能给jQuery提供一些额外的功能. 1.1. ...
- PHPer面试指南-laravel 篇
简述 Laravel 的生命周期 Laravel 采用了单一入口模式,应用的所有请求入口都是 public/index.php 文件. 注册类文件自动加载器 : Laravel通过 composer ...