MyBatis 之 使用五 动态SQL

MyBatis 提供使用 ognl 表达式动态生成 SQL的功能。

1. if

2. where

where 可以自动处理掉第一个拼接条件里的 and

<!-- 动态 sql 查询用户信息 -->
  <select id="findUserByDynamicSql" parameterType="user" resultType="user">
  	select * from users
  	<!-- where 可以自动处理第一个 and -->
  	<where>
  	  <!-- 注意:要做不为null 和 ‘‘ 校验 -->
	  <if test="userId != null and userId != ‘‘">
 	  	and users.userId = #{userId}
 	  </if>
 	  
 	  <if test="username != null and username != ‘‘">
 	  	and users.username like ‘%${username}%‘
 	  </if>
 	</where>
  </select>

3. foreach

foreach 可以向 SQL 传递数组、List<E> 等,下面介绍一个数组的例子

<!-- 动态 sql 查询用户信息 -->
  <select id="findUserByDynamicSql" parameterType="user" resultType="user">
  	select * from users
  	<!-- where 可以自动处理第一个 and -->
  	<where>
  	  <!-- 解析传递过来的多个 userId -->
  	  <!-- ids 是结合;item 是集合中的项目变量 -->
  	  <foreach collection="ids" item="id" open="userId in(" close=")" separator=",">
  	  	#{id}
  	  </foreach>
  
  	  <!-- 注意:要做不为null 和 ‘‘ 校验 -->
	  <if test="userId != null and userId != ‘‘">
 	  	and users.userId = #{userId}
 	  </if>
 	  
 	  <if test="username != null and username != ‘‘">
 	  	and users.username like ‘%${username}%‘
 	  </if>
 	</where>
  </select>

运行结果:

如果上面传递的数组中是 pojo 对象,那么collection 属性名称必须是 array(如果是传递 List 对象,

那么collection属性名称是 list),而传递到 sql 当中的参数则是

pojo.属性名。如下:

index:为数组的下标, item:数组中每个元素的名称,随便定义

open:循环开始, close:循环结束         separator:中间分隔输出

<select id="selectUserByArray" parameterType="Object[]" resultType="user">
    select * from user 
    <where> 
        <!-- 传递数组 -->
        <if test="array!=null">
          <foreach collection="array" index="index" item="item" 
              open="and id in(" separator="," close=")" >
            #{item.id} 
          </foreach>
        </if>
    </where>
</select>

4. SQL 片段

按照上面的 动态SQL 写法,可能会产生很多重复的 sql。为了重用这些 sql 减少代码,可以将重复的 sql

提取出来,使用时通过 include 引用。

a. 定义 SQL 片段,将 重复的 sql 抽取出来

<!-- 定义SQL 片段 -->
  <sql id="query_user_ByUserName">
  	<if test="username != null and username != ‘‘">
 	  and users.username like ‘%${username}%‘
 	</if>
  </sql>
  
  <sql id="query_user_ByUserId">
  	<!-- 注意:要做不为null 和 ‘‘ 校验 -->
	<if test="userId != null and userId != ‘‘">
 	  and users.userId = #{userId}
 	</if>
  </sql>
  
  <sql id="query_user_ByIdArray">
  	<!-- 解析传递过来的多个 userId -->
  	<!-- ids 是结合;item 是集合中的项目变量 -->
  	<foreach collection="ids" item="id" open="userId in(" close=")" separator=",">
  	  #{id}
  	</foreach>
  </sql>

b. 在 SQL 中引用 sql 片段,可以对比上面所举得例子中的代码

<!-- 动态 sql 查询用户信息 -->
  <select id="findUserByDynamicSql" parameterType="user" resultType="user">
  	select * from users
  	<!-- where 可以自动处理第一个 and -->
  	<where>
  	  <include refid="query_user_ByIdArray"></include>
  
  	  <include refid="query_user_ByUserId"></include>
 	  
 	  <include refid="query_user_ByUserName"></include>
 	  
 	</where>
  </select>
时间: 2024-08-11 09:35:22

MyBatis 之 使用五 动态SQL的相关文章

mybatis入门基础(五)----动态SQL

一:动态SQL 1.1.定义 mybatis核心对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 1.2.案例需求 用户信息综合查询列表这个statement的定义使用动态sql,对查询条件进行判断,如果输入参数不为空才进行查询拼接. 1.3.UserMapper.xml 1 <!-- 用户信息综合查询 2 #{userCustom.sex}:取出pojo包装对象中性别值 3 ${userCustom.username}:取出pojo对象中用户名称 4 --> 5 &

MyBatis学习总结_11_MyBatis动态Sql语句

MyBatis中对数据库的操作,有时要带一些条件,因此动态SQL语句非常有必要,下面就主要来讲讲几个常用的动态SQL语句的语法 MyBatis中用于实现动态SQL的元素主要有: if choose(when,otherwise) trim where set foreach 1.if 对属性进行判断,如果不为空则执行判断条件 [html] view plaincopy <select id="selectByCriteria" parameterType="com.mu

MyBatis学习总结(六)——动态SQL

MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. MyBatis中用于实现动态SQL的元素主要有: if choose(when,otherwise) foreach where set trim 下面我们主要说 where set trim 这三个标签 1,where标签 <!-- 查询学生list,like姓名,=性别 --> <select id="getStudentListWhere" parameterTy

Spring+SpringMVC+MyBatis深入学习及搭建(五)——动态sql

转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6908763.html 前面有讲到Spring+SpringMVC+MyBatis深入学习及搭建(四)——MyBatis输入映射与输出映射 mybatis核心:对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. mybatis提供各种标签方法实现动态拼接sql. 1. if&where 1.2 需求 用户信息综合查询列表和用户信息查询列表总数这两个statement的定义使用动态sq

Spring+SpringMVC+MyBatis深入学习及搭建(五)——动态sql(转发同上)

原地址:http://www.cnblogs.com/shanheyongmu/p/7121807.html mybatis核心:对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. mybatis提供各种标签方法实现动态拼接sql. 1. if&where 1.2 需求 用户信息综合查询列表和用户信息查询列表总数这两个statement的定义使用动态sql. 对查询条件进行判断,如果输入参数不为空才进行查询条件拼接. 1.3 mapper.xml <select id

MyBatis(五) 动态SQL

1.动态查询(动态生成Where语句): 这篇文章中叙述的功能,统一使用下表测试: CREATE TABLE `test_order_detail_mm` (   `id` int(20) NOT NULL AUTO_INCREMENT,   `order_id` int(20) NOT NULL,   `goods_name` varchar(50) DEFAULT NULL,   `single_price` decimal(19,2) DEFAULT NULL,   `num` int(2

Mybatis映射原理,动态SQL,log4j

1.理清mybatis中的#和$之间的区别? #{ }:表示一个预处理参数,参数类型不定,是根据传入的参数类型来设定的. 类似于JDBC中的? 特例使用,模糊查询:(针对oracle): and username like concat(concat('%',#{username}),'%') 采取的$的方式传入参数,所有采取$的方式传入的参数都只是字符串(无论传入的是什么,都会当成字符串处理),潜在的危险就是SQL注入的问题. and username like '%${value}%' 注意

MyBatis 源码分析——动态SQL语句

有几年开发经验的程序员应该都有暗骂过原生的SQL语句吧.因为他们不能一句就搞定一个业务,往往还要通过代码来拼接相关的SQL语句.相信大家会理解SQL里面的永真(1=1),永假(1=2)的意义吧.所以mybatis动态SQL功能在笔者看来是最引吸人的.为了更好的区别XML映射文件上的SQL语句.mybatis把SQL语句分为四类.那么这个笔者已经在前面的章节里面讲过了.但是我们在开发过程中常常用到的也就俩种:静态和动态. 关于静态和动态的定义,笔者是这样子理解的--静态SQL语句显示就是里面没有相

(转)Mybatis高级映射、动态SQL及获得自增主键

原文:http://www.cnblogs.com/edwinchen/p/4105278.html?utm_source=tuicool&utm_medium=referral 一.动态SQL 相信大家在用mybatis操作数据库时时都会碰到一个问题,假如现在我们有一个关于作者的list authorList,需要根据authorList里已有的作者信息在数据库中查询相应作者的博客信息.那么最容易想到的做法就是遍历authorList,获取相应的信息查询数据库. for(int i=0;I &l