mybatis中的动态sql语句

Mybatis的动态SQL语句是基于 OGNL表达式的,可以方便的在SQL语句中实现逻辑判断,总体来说Mybatis动态SQL语句主要分为一下几类

  1. if语句
  2. choose(when、otherwize),相当于Java语言中的switch语句和if-else语句
  3. where语句
  4. set语句
  5. foreach语句

mybatis会把每个SQL标签封装成SqlSource对象,然后再根据动态SQL语句或者静态SQL语句来生成对应的SqlSource对象

动态SQL
静态SQL
SqlSource
DynameicSqlSource
StaticSqlSource
String sql=
SqlNode
ifSqlNode
ForEachSqlNode
TrimSqlNode
WhereSqlNode
.......

对于动态SQL语句 ,其 SqlSource对象结构如下图所示

对于SQL语句

<select id="getUserById" resultType="user">
	select * from user 
	<where>
		<if test="uid!=null">
			and uid=#{uid}
		if>
	where>
select>	

它对应的SqlSource对象如图所示Mybatis会根据对应的逻辑 判断 生成 对应的SQLNode结点来执行

DynamicSqlSource
staticTextSqlNode
select *from user
WhereSqlNode
ifSqlNode
test:uid!=null
uid等于参数中传递过来的
StaticTextSqlNode
\n\t

在动态SQL的逻辑判断中 if判断是最基本的 其结构为

<if test="逻辑代码">
	表达式
if>

当test条件成立时 才会执行到表达式的SQLNode结点 注:test中的逻辑代码里==和equals一样比较的是地址

对于choose when otherwise 也就是相当于java中的if-else语句

<select id="example" resultMap="xxx" parameterType="xxx">
    select
    <include refid="Base_Column_List"/>
    from xxx
    where xxx=xxx
    <choose>
        <when test="xxx !=null and xxx != ''">
            and xxx like concat(concat('%', #{xxx}), '%')
        when>
        <otherwise>
            and xxx like '**%'
        otherwise>
    choose>
select>

其中choose标签内的代表一个整体 when相当于if otherwise相当于else

对于where语句

其主要用来简化SQL语句中的where条件判断,能够智能的处理and,or
例如对于如下SQL语句

<select  id="XX" resultMap="XX" paramenterType="XXX">
    select
    <include refid="Base_Column_List"/>
    from XXX
    where username LIKE "%#{XXX}%"
    and sex = #{XXX}
select>

在username和sex都有值得情况下我们使用where和and,,但是如果这两个值只有其中一个上面的语句就会报错了这个时候就可以使用where标签来进行

<select  id="XX" resultMap="XX" paramenterType="XXX">
    select
    <include refid="Base_Column_List"/>
    from XXX
    <where>
        <if test="username!=null">
            username = #{username}
        if>
        <if test="sex != null">
            and sex=#{sex}
        if>
    where>
select>

现在 问题又来了:那么如果第一个if条件不成立第二个if成立了 怎么办?

MyBatis会智能的把首个and或or给忽略。如果输出后是and开头的,MyBatis会把第一个and忽略,当然如果是or开头的,MyBatis也会把它忽略;此外,在where标签的作用是在where标签的位置输出一个where,并且在where标签中中不需要考虑空格的问题,MyBatis会智能的帮你加上。

对于set语句

set和where标签的用法基本相同,set主要 使用在更新操作的时候,在该标签位置输出一个set
注:如果set包含的 语句是以都好结束的画,该逗号会被忽略,,如果set中包含的内容为空的话,在生成SQLNode过程中会出错。通过set标签可以动态的更新需要修改的字段

<update id="XXX" parameterType="XXX">
        update article
        <set>
            <if test="title != null">
                title = #{title},
            if>
            <if test="content != null">
                content = #{content}
            if>
        set>
        where id = #{id}
    update>

对于foreach语句

foreach主要用于实现in语句查询 ,它可以在SQL语句中进行迭代一个集合 foreach元素的属性主要有item,index,collection,open,separator,close

item表示集合中每一个元素进行迭代时的别名,
index即是下标,用于表示在迭代过程中,每次迭代到的位置,
open表示该语句以什么开始,
separator表示在每次进行迭代之间以什么符号作为分隔符,
close表示以什么结束,
在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下3种情况:

  • 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
  • 如果传入的是单参数且参数类型是一个Array数组的时候,collection的属性值为array
  • 如果传入的参数是多个的时候,就需要把它们封装成一个Map,当然单参数也可以封装成Map,实际上如果在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,Map的key就是参数名,所以这个时候collection属性值就是传入的List或Array对象在自己封装的Map里面的key

对于list类型单参数

<select id="dynamicForeachTest" resultType="Article">
    select * from article where id in
    <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
    	#{item}
    foreach>
select>

对于数组类型参数

<select id="dynamicForeach2Test" resultType="Article">
    select * from article where id in
    <foreach collection="array" index="index" item="item" open="(" separator="," close=")">
  	  #{item}
    foreach>
select>

对于Map类型参数

<select id="dynamicForeach3Test" resultType="Article">
    select * from article where title like "%"#{title}"%" and id in
    <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
    	#{item}
    foreach>
select>

你可能感兴趣的:(mybatis中的动态sql语句)