mybatis学习二动态SQL:

来源:互联网 发布:http5网络协议 编辑:程序博客网 时间:2024/06/05 04:53

mybatis 的动态sql语句是基于OGNL表达式的。可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类:
1. if 语句 (简单的条件判断)
2. choose (when,otherwize) ,相当于java 语言中的 switch ,与 jstl 中的choose 很类似.
3. trim (对包含的内容加上 prefix,或者 suffix 等,前缀,后缀)
4. where (主要是用来简化sql语句中where条件判断的,能智能的处理 and or ,不必担心多余导致语法错误)
5. set (主要用于更新时)
6. foreach (在实现 mybatis in 语句查询时特别有用)

if语句

<!--         需求:更具姓名查询用户信息        参数:String username        返回值类型:List<User>     -->     <select id="findUserByUsername" parameterType="user" resultType="string">        select * from user         <if test="username!=null and username!='' ">            where username=#{username}        </if>     </select>

where标签

<!--         <!-- 查询性别为男的张姓用户 --><select id="findUserBygenderAndusername" parameterType="map" resultType="user">        select * from user         <where>            <if test="sex!=null and sex!=''">sex=#{sex}</if>            <if test="username!=null and username!=''">and username like "%"#{username}"%"</if>        </where>        <!-- where sex=#{sex} and username like "%"#{username}"%" -->    </select>

choose(when,otherwize)
when元素表示当when中的条件满足的时候就输出其中的内容,跟JAVA中的switch效果差不多的是按照条件的顺序,当when中有条件满足的时候,就会跳出choose,即所有的when和otherwise条件中,只有一个会输出,当所有的我很条件都不满足的时候就输出otherwise中的内容。所以上述语句的意思非常简单, 当title!=null的时候就输出and titlte = #{title},不再往下判断条件,当title为空且content!=null的时候就输出and content = #{content},当所有条件都不满足的时候就输出otherwise中的内容。

<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">        select * from t_blog where 1 = 1         <choose>            <when test="title != null">                and title = #{title}            </when>            <when test="content != null">                and content = #{content}            </when>            <otherwise>                and owner = "owner1"            </otherwise>        </choose>    </select>

trim标签
trim元素的主要功能是可以在自己包含的内容前加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是prefix和suffix;可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容覆盖,对应的属性是prefixOverrides和suffixOverrides;正因为trim有这样的功能,所以我们也可以非常简单的利用trim来代替where元素的功能

    <select id="dynamicTrimTest" parameterType="Blog" resultType="Blog">        select * from t_blog         <trim prefix="where" prefixOverrides="and |or">            <if test="title != null">                title = #{title}            </if>            <if test="content != null">                and content = #{content}            </if>            <if test="owner != null">                or owner = #{owner}            </if>        </trim>    </select>

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

<!-- 查询id为25,26,27的用户 -->    <select id=",findUserForeach" parameterType="usercustomer" resultType="user">        select * f,rom user        <where>            <foreach  separator="or" collection="list" item="id"  open="(" close=")">                id=#{id}            </foreach>        </where>    </select>
原创粉丝点击