动态sql(8)

来源:互联网 发布:乐高ev3编程视频教程 编辑:程序博客网 时间:2024/05/16 18:08

前记:这是很早之前自学学习myBatis时的笔记,内容比较基础,适合新手,内容基本是来自网络,如有雷同,敬请谅解!

     动态Sql

                                     mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装。

   if的使用

!-- 传递pojo综合查询用户信息 -->

    <selectid="findUserList”parameterType="user"resultType="user">

       select * from user

       where 1=1

       <if test="id!=null and id!=''">

       and id=#{id}

       </if>

       <if test="username!=nulland username!=''">

       and username like '%${username}%'

       </if>

    </select>

注意要做不等于空字符串校验

    where的使用

上面的sql也可以改为:

 

<selectid="findUserList"parameterType="user"resultType="user">

       select * from user

       <where>

       <if test="id!=nulland id!=''">

       and id=#{id}

       </if>

       <if test="username!=nulland username!=''">

       and username like '%${username}%'

       </if>

       </where>

    </select>

<where />可以自动处理第一个and

     foreach的使用

向sql传递数组或List,mybatis使用foreach解析

         通过pojo传递list

                 在pojo中定义list属性ids存储多个用户id,并添加getter/setter方法


               mapper.xml

<iftest="ids!=null and ids.size>0">

       <foreachcollection="ids"open=" and idin("close=")" item="id"  separator=",">

               #{id}

     </foreach>

</if>

或者:

 传递单个list

                 传递List类型在编写mapper.xml没有区别,唯一不同的是只有一个List参数时它的参数名为list。

如下:

        Mapper.xml

<selectid="selectUserByList"

        parameterType="java.util.List"

        resultType="user">

       select * from user

       <where>

       <!-- 传递ListList中是pojo -->

       <if test="list!=null">

       <foreach collection="list"item="item"open="and id in("separator=","close=")">

           #{item.id}

       </foreach>

       </if>

       </where>

    </select>

       Mapper接口

public List<User>selectUserByList(List userlist) throws Exception;

  传递单个数组(数组中是pojo)

                Mapper.xml

<!-- 传递数组综合查询用户信息 -->

    <selectid="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>

sql只接收一个数组参数,这时sql解析参数的名称mybatis固定为array

如果数组是通过一个pojo传递到sql则参数的名称为pojo中的属性名。

index:为数组的下标。

item:为数组每个元素的名称,名称随意定义

open:循环开始

close:循环结束

separator:中间分隔输出

  传递单个数组(数组中是字符串类型)

            Mapper.xml

<!-- 传递数组综合查询用户信息 -->

    <selectid="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}

       </foreach>

       </if>

       </where>

    </select>

如果数组中是简单类型则写为#{item},不用再通过ognl获取对象属性值了。

     Sql片段

Sql中可将重复的sql提取出来,使用时用include引用即可,最终达到sql重用的目的,如下:

<!-- 传递pojo综合查询用户信息 -->

    <selectid="findUserList"parameterType="user"resultType="user">

       select * from user

       <where>

       <if test="id!=null and id!=''">

       and id=#{id}

       </if>

       <if test="username!=nul land username!=''">

       and username like '%${username}%'

       </if>

       </where>

    </select>

 

   将where条件抽取出来:

 

<sqlid="query_user_where">

    <iftest="id!=null and id!=''">

       and id=#{id}

    </if>

    <iftest="username!=null and username!=''">

       and username like '%${username}%'

    </if>

</sql>

   

     使用include引用:

 

<selectid="findUserList"parameterType="user"resultType="user">

       select * from user

       <where>

       <include refid="query_user_where"/>

       </where>

    </select>

 

注意:如果引用其它mapper.xmlsql片段,则在引用时需要加上namespace,如下:

<includerefid="namespace.sql片段”/>