首先动态sql简单来讲就是可以根据传入参数的不同来动态的生成sql语句,拼接where语句,这样你就不用写很多个sql语句了,并且它里面有一些特性也可以帮助你避免sql语句的拼接错误,主要分为4个:

if,chooose,trim,foreach

IF:

对参数进行判断,拼接不同的sql语句,看一个例子

    <select id="selectByIf" parameterType="person" resultType="person">
select *from person where sex !=
<if test="name != null">
AND name = #{name}
</if>
</select>

很简单,test=".."里面的是条件,当传入的name属性不为空时,就将if标签体当中的sql语句拼接到其后,当然你后面也可以写很多个if,满足条件的都会拼接。另外test里面的条件也是可以进行复合判断的,比如:

    <select id="selectByIf" parameterType="person" resultType="person">
select *from person where sex !=
<if test="name != null and name!='温鸿飞'">
AND name = #{name}
</if>
</select>

这时候如果传入对象的name为温鸿飞,那就不会拼接sql语句了。

Choose:

这个语句其实即使if,else,从第一个开始向下找,找到满足条件的就停止,比如;

    <select id="selectByIf" parameterType="person" resultType="person">
select *from person where sex !=
<choose>
<when test="name!=null">
and name = #{name}
</when>
<when test="name=='温鸿飞'">
and name = ''
</when>
<otherwise></otherwise>
</choose>
</select>

很容易理解,不过说明一点,比如你传入的name值为温鸿飞,那么拼接的sql语句是and name = #{name},而不是第二个条件,即多个条件都满足时,拼接第一个。

,where,

是为了解决出现sql语法错误的情况:比如将上面if语句后面的sex!=0去掉,

  <select id="selectByIf" parameterType="person" resultType="person">       
select *from person where
    <if test="sex!=null">
      sex != #{sex}
    </if>
<if test="name != null">
AND name = #{name}
</if>
</select>

这时如果满足条件,很明显拼接出来的sql语句是不正确的,这时候只要给if外面加上<where>标签就可以了,加上之后会自动去除and,并且会加上where(如果满足其中一个if条件的话),如下:

    <select id="selectByIf" parameterType="person" resultType="person">
select *from person
<where>
<if test="sex!=null">
sex != #{sex}
</if>
<if test="name!=null">
and name = #{name}
</if>
</where>
</select>

set,

这个标签是在动态更新的时候用的,可以帮助我们加上set,并去除无关的逗号,如下

    <update id="updateByIf" parameterType="person">
update person
<set>
<if test="name!=null">name=#{name },</if>
<if test="sex!=null">sex=#{sex}</if>
</set>
where id =#{id}
</update>

如果只传入了name,那么name=#{name},  后面的这个逗号也会帮我们去掉,这个意思

Foreach

一般用于in标识符后面,如下:

    <select id="selectByForeach" parameterType="list" resultType="person">
select * from person where id in
<foreach collection="list" item="id" close=")" open="(" separator=",">
#{id}
</foreach>
</select>

item是别名,collection是类型,list和array一般,后面三个是拼接时候的开始结尾分隔符,接口这样写:

List<Person> selectByForeach(List list);

传入的是List类型,

例,插入多条数据,传入list,每一项都是Person对象,构建sql语句即可

    <insert id="insertAll" parameterType="list">
insert into person (`name`,`loves`,`sex`) values
<foreach collection="list" separator="," open="" close="" item="person">
(#{person.name},#{person.loves},#{person.sex})
</foreach>
</insert>

规则就这些,用的时候肯定会很复杂

(二)Mybatis动态sql的更多相关文章

  1. 二 mybatis 动态sql

    动态sql应用  一 .什么是动态sql 1.where条件  动态查询 根据姓名或年龄或地址查询 UserMapper.xml 1 <select id="findUser" ...

  2. MyBatis 动态SQL(十二)

    动态条件查询 以下是我们数据库表 tb_user 的记录: 假设现在有一个需求,就是根据输入的用户年龄和性别,查询用户的记录信息.你可能会说,这太简单了,脑袋里立马蹦出如下的 SQL 语句: SELE ...

  3. MyBatis动态SQL之一使用 if 标签和 choose标签

    bootstrap react https://segmentfault.com/a/1190000010383464 xml 中 < 转义 to thi tha <if test=&qu ...

  4. Mybatis动态sql及分页、特殊符号

    目的: mybatis动态sql(案例:万能查询) 查询返回结果集的处理 mybatis的分页运用 mybatis的特殊符号 mybatis动态sql(案例:万能查询) 根据id查询 模糊查询 (参数 ...

  5. Mybatis 动态sql(转载)

    原文地址:http://www.cnblogs.com/dongying/p/4092662.html 传统的使用JDBC的方法,相信大家在组合复杂的的SQL语句的时候,需要去拼接,稍不注意哪怕少了个 ...

  6. mybatis实战教程(mybatis in action)之八:mybatis 动态sql语句

    mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类:1. if 语句 (简单的条件判断)2. c ...

  7. 9.mybatis动态SQL标签的用法

    mybatis动态SQL标签的用法   动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么 ...

  8. 自己动手实现mybatis动态sql

    发现要坚持写博客真的是一件很困难的事情,各种原因都会导致顾不上博客.本来打算写自己动手实现orm,看看时间,还是先实现一个动态sql,下次有时间再补上orm完整的实现吧. 用过mybatis的人,估计 ...

  9. Mybatis动态SQL单一基础类型参数用if标签

    Mybatis动态SQL单一基础类型参数用if标签时,test中应该用 _parameter,如: 1 2 3 4 5 6 <select id="selectByName" ...

  10. 超全MyBatis动态SQL详解!( 看完SQL爽多了)

    MyBatis 令人喜欢的一大特性就是动态 SQL. 在使用 JDBC 的过程中, 根据条件进行 SQL 的拼接是很麻烦且很容易出错的. MyBatis 动态 SQL 的出现, 解决了这个麻烦. My ...

随机推荐

  1. Sorted 内置函数的排序使用

    Sorted 内置函数的排序使用 Sorted 排序列表 1.倒序对列表进行排序 # 对列表进行降序序排序 list = [1,3,4,23,6,7] list = sorted(list,rever ...

  2. 程序员必知的技术官网系列--mysql篇

    mysql 官网 https://www.mysql.com/ 官网布局很简单, 其中常用的两块就是下载和文档这两块, 其中下载没什么可讲的, 本次重点依旧是文档. 首页 mysql 文档导航页 ht ...

  3. openjudge 7622 求排列的逆序数(归并)

    7622:求排列的逆序数 总时间限制:  1000ms   内存限制:  65536kB 描述 在Internet上的搜索引擎经常需要对信息进行比较,比如可以通过某个人对一些事物的排名来估计他(或她) ...

  4. kubespy 用bash实现的k8s动态调试工具

    原文位于 https://github.com/huazhihao/kubespy/blob/master/implement-a-k8s-debug-plugin-in-bash.md 背景 Kub ...

  5. 前端页面表格排序 jQuery Table 基础

    通常来说, 排序的方式有两种, 一种是我们在查询的时候就排好序,然后将数据渲染到前台页面上, 但是这样做有个弊端,就是在争对做好了缓存处理的系统, 在查询相同数据的时候进行排序,可能不能成功, 因为进 ...

  6. OpenStack Identity API v3

    Table Of Contents OpenStack Identity API v3 What’s New in Version 3.7 What’s New in Version 3.6 What ...

  7. php5.6.39 源码安装

    1 安装依赖库 yum install -y autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel li ...

  8. intellij idea svn 切换分支

    原文地址:https://blog.csdn.net/wangjun5159/article/details/75137964 切换分支 更新/切换svn的快捷键是ctrl+T,这个快捷键还是很好用的 ...

  9. 提供程序模式 提供 coding 一点点

    放个图先,预则立码

  10. 靶机-SickOs 1.2 Walkthrough

    SickOs:1.2 https://www.vulnhub.com/entry/sickos-12,144/ 参考:https://www.cnblogs.com/yuzly/p/10854392. ...