开发中,sql拼接很常见,所以说一下动态sql:

1 if
2 chose,when,otherwise
3 where,set
4 foreach

用法解析(现有一张users表 内有id username age 三个字段):

    <!--查询所有用户,传递参数type,如果值为0,按照年龄升序排序,如果为1则按照年龄降序排序,否则按照ID排序-->
<!--choose when otherwise的用法 大致相当于case when default-->
<select id="getUserListByType" resultType="User">
select * from users
<choose>
<when test="type==0">order by age asc</when>
<when test="type==1">order by age desc</when>
<otherwise>order by id asc</otherwise>
</choose>
</select> <!--根据多个id查询用户 if where foreach的用法-->
<select id="getUserListByIds" resultMap="User">
select * from users
<where>
-- if判断 如果传进去的参数ids不为空
<if test="ids!=null and ids==''">
and id in
-- foreach循环ids 以逗号为分隔符 以ids这个字符串中的'('为开始 ')'为结果
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</if>
</where>
</select> <!--修改用户信息,如果某字段为null,则不修改这个字段 set的用法-->
<select id="updateUser">
update users
<set>
<if test="username!=null and username!=''">
username = #{username}
</if>
<if test="age!=null">
age = #{age}
</if>
</set>
</select>

我们还可以把重复的sql抽取出来,作为公用的sql片段:

定义sql片段:

    <!-- sql片段
建议:对单表进行sql片段的抽取,方便重用
抽取时不包含where
-->
<sql id="findUserSql">
<if test="userCustomer!=null">
<if test="userCustomer.age!=null">
and user.age=#{userCustomer.age}
</if>
<if test="userCustomer.username!=null and userCustomer.username!=''">
and user.username like '$%{userCustomer.username}%'
</if>
</if>
</sql>

使用sql片段:

    <!-- 动态sql -->
<select id="findUserCount" parameterType="com.zy.domain.User" resultType="int">
select count(*) from users
-- where 可自动去除条件中的第一个and
<where>
<include refid="findUserSql"></include>
</where>
</select>

MyBatis总结七:动态sql和sql片段的更多相关文章

  1. 6.Mybatis中的动态Sql和Sql片段(Mybatis的一个核心)

    动态Sql是Mybatis的核心,就是对我们的sql语句进行灵活的操作,他可以通过表达式,对sql语句进行判断,然后对其进行灵活的拼接和组装.可以简单的说成Mybatis中可以动态去的判断需不需要某些 ...

  2. 【mybatis深度历险系列】mybatis中的动态sql

    最近一直做项目,博文很长时间没有更新了,今天抽空,学习了一下mybatis,并且总结一下.在前面的博文中,小编主要简单的介绍了mybatis中的输入和输出映射,并且通过demo简单的介绍了输入映射和输 ...

  3. Mybatis入门之动态sql

    Mybatis入门之动态sql 通过mybatis提供的各种标签方法实现动态拼接sql. 1.if.where.sql.include标签(条件.sql片段) <sql id="sel ...

  4. mybatis 详解------动态SQL

    mybatis 详解------动态SQL   目录 1.动态SQL:if 语句 2.动态SQL:if+where 语句 3.动态SQL:if+set 语句 4.动态SQL:choose(when,o ...

  5. mybatis教程4(动态SQL)

    动态SQL语句 MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句的痛苦.例如拼接时要确保不能忘记添加必要的空 ...

  6. mybatis学习 十 动态 SQL

    1.  根据方法传入的参数不同执行不同的 SQL 命令.称为动态 SQL, MyBatis 中动态 SQL 就是在 mapper.xml 中添加逻辑判断等. 2. <if>标签 <s ...

  7. mybatis第二天——动态SQL与关联查询

    大纲摘要: 1.输入映射和输出映射 a) 输入参数映射 b) 返回值映射 2.动态sql a) If b) Where c) Foreach d) Sql片段 3.关联查询 a) 一对一关联 b) 一 ...

  8. mybatis入门基础----动态SQL

    原文:http://www.cnblogs.com/selene/p/4613035.html 阅读目录 一:动态SQL 二:SQL片段 三:foreach 回到顶部 一:动态SQL 1.1.定义 m ...

  9. mybatis中的动态SQL语句

    有时候,静态的SQL语句并不能满足应用程序的需求.我们可以根据一些条件,来动态地构建 SQL语句. 例如,在Web应用程序中,有可能有一些搜索界面,需要输入一个或多个选项,然后根据这些已选择的条件去执 ...

随机推荐

  1. JavaScript数组和函数的使用

    数组 数组:一个有顺序,有长度的数据集合 作用:存储大量数据 一.数组的定义 1.构造函数法:使用构造函数法的时候,都会使用new关键字 var arr=new Array(): 当长度为0 的时候, ...

  2. php的http数据传输get/post...

    php的http数据传输get/post... 一般有:file_get_contents,curl,fsockopen.... 下面介绍fsockopen: //构造要post的字符串 $argv ...

  3. 【LABVIEW到C#】1》ini的操作

    using System; using System.IO; using System.Drawing; using System.Collections; using System.Componen ...

  4. ComboBoxStyle和ToggleButton

    <Style x:Key="ComboBoxStyle" TargetType="{x:Type ComboBox}">   <Setter ...

  5. CSS媒体查询 width VS device-width

    In CSS media the difference between width and device-width can be a bit muddled, so lets expound on ...

  6. MySql按周/月/日分组统计数据的方法

    知识关键词:DATE_FORMAT select DATE_FORMAT(create_time,'%Y%u') weeks,count(caseid) count from tc_case grou ...

  7. CSS 文本实例

    1.设置文本的颜色 color:red; color:#00ff00 color:rgb(0,0,255) 2.增加或减少字符间距 letter-spacing:-0.5em letter-spaci ...

  8. len(),range()函数

    len()函数返回字符串.列表.字典.元组等长度 eg1:计算字符串的长度: >>>s='hello good boy doiido' >>>len(s) > ...

  9. CodeForces - 651D:Image Preview (双指针&)

    Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed ...

  10. Linux-CentOS7 安装VMware Workstation 12

    转自:http://blog.csdn.net/aoshilang2249/article/details/48656107 1.下载VMware 衔接地址 http://www.vmware.com ...