java开发

技术学习笔记

MyBatis动态SQL

1: if标签

<if test="boolean判断结果(条件)">
    数据库语句
</if> 

实例说明:

<!--
    条件中的where 1=1 是为了防止where 与<if>标签中的AND直接拼接导致出现sql语法错误
    -->
<select id="queryStudent" parameterType="Student" resultType="Student">
    SELECT * FROM student WHERE 1==1
<!--
如果name有值即可执行下属<if>标签中的拼接
-->
    <if test="name != null AND name != ''">
        AND name LIKE concat('%',#{name,jdbcType=VARCHAR},'%')
    </if>
<!--
如果score的值>0那么执行下述<if>标签中的内容
-->
    <if test="score > 0">
        AND score &gt;= #{score,jdbcType=VARCHAR}
    </if>
</select> 

在上述实例说明中展示了两个例子,其中因为在xml文件中是禁止出现<符号的,那么在xml文件标签中的判断类符号通常使用实体符号去代替。

判断类符号 实体符号
>>
<<
>=>=
<=<=

2: where标签

在我们使用标签的时候我们很容易引起SQL的语法错误,在我们同时使用标签的时候可以很好地解决这一问题,在where标签中存放一个或多个标签,当其中一个标签为true,标签会变成SQL语句中的where关键字添加在基本SQL语句后,同时将标签中多余的and和or清除,如果为false则忽略此标签。

<where>
<if test="条件1"> SQL语句1 </if>
<if test="条件2"> SQL语句2 </if>
</where> 

实例说明:

<select id="queryTest2" resultMap="Student" parameterType="Student">
    select * from student
    <where>
    <if test="name != null AND name != ''"> name LIKE concat('%',#{name,jdbcType=VARCHAR},'%') </if>
    <if test="score > 0"> score &gt;= #{score,jdbcType=VARCHAR} </if>
    </where>
</select> 

3:foearch标签

使用可以实现数组和list集合之间的遍历,一般使用在in语句中,主要包含以下参数:

item:循环体中的具体对象,自定义的变量。

index:在list和数组中,index是元素的序号,在map中,index是元素的key,该参数为可选。

open:表示该语句以什么内容开始

close:表示该语句以什么内容结束

separator:表示循环体具体对象的分隔符,例如在in()中,separator=","会自动在元素中用","隔开,避免手动输入逗号导致SQL报错,该参数可选。

<foreach collection="集合类型" open="开始的字符" close="结束的字符" item="集合中需要遍历的成员" separator="分隔符">
        #{item的值}
</foreach> 

实例说明:

 <!--
循环简单类型的List,直接获取
-->
 
<select id="querytest3" parameterType="Student" resultType="Student">
    select * from student
    <if test="list!= null amd list.size > 0 ">
    where id in
    <foreach collection="List" open="(" close=")" item="stuId" separator=",">
    #{stuId}
    </foreach>
    </if>
</select>
 
<!--
循环整个对象List<Student>(引用类型),遍历获取对象下的id属性
-->
 
<select id="queryTest3" resultMap="Student" parameterType="Student">
    select * from student
    <if test="list != null and list.size >0">
     where id in
     <foreach collection="List" separator="," item="student" open="(" close=")">
        #{student.id}
      </foreach>
      </if>
</select>

4:sql标签

sql标签代表该内容是一段sql代码,可以是表名、字段、where等条件,可以在其他地方复用sql标签中的内容。

首先定义sql标签中的sql片段,之后在其他的位置上使用include标签引用该代码片段sql语句

<sql id="为该片段命名">sql语句</sql> 

<!--
定义sql标签中的sql片段
-->
<sql id="queryTest4">
    select * from student
</sql>
<select id="queryTest3" resultMap="Student" parameterType="Student">
<!--
使用include标签引用该代码片段
-->
    <include refid="queryTest4" />
    <if test="list != null and list.size >0">
    where id in
    <foreach collection="List" separator="," item="student" open="(" close=")">
     #{student.id}
     </foreach>
     </if>
</select> 

5:choose where  otherwise 标签

三个标签一般一起使用,类似与Java中的switch、case、default语句,当只有其中的一个条件生效的时候只执行满足这个条件的标签中的语句,类似于Java中的case,当其中没有满足条件的语句时就执行标签中的语句,表示默认条件,类似于java中的default。在测试中即使同时添加name和score的值,最终sql也会只添加第一个属性值。

实例说明

<select id="queryTest4" resultMap="Student" parameterType="Student">
    select * from student
    <where>
    <choose>
    <when test="name != null and name!=''">
          and `name`=#{name}
    </when>
    <when test="score != null and score > 0">
          and score=#{score}
    </when>
    <otherwise>
          and id=#{id}
    </otherwise>
    </choose>
    </where>
</select> 

6:set标签

set标签可以动态的配置SET关键字,并剔除其内部标签的末尾逗号。使用if+set标签进行修改后,再进行更新操作,通过标签中的条件可以确定哪个值更新哪个值不更新。

事列说明

<update id="queryTest5" parameterType="Student">
        update student
        <set>
            <if test="name!=null and name !=''">
                `name`=#{name},
            </if>
            <if test="score!= null and score > 0">
                score=#{score},
            </if>
        </set>
        where id=#{id}
    </update> 

7:trim标签

标签是一个格式化标签,可以更灵活的去除多余关键字的标签,可以达到标签和标签的效果,主要包含以下参数:

prefix:前缀

prefixOverrides:去掉第一个and或者or

suffix:后缀

suffixOverrides:去掉最后一个逗号

实例说明:


   <select id="queryTest6" parameterType="Student">
        select * from student
        <trim prefix="where" suffix="order by age" prefixOverrides="and 
or" suffixOverrides=",">
        <if test="name!= null and name!= ''">
            AND `name`=#{name}
        </if>
        <if test="score!= null and score > 0">
            and score=#{score}
        </if>
        </trim>
    </select> 

发表评论:

Powered By Z-BlogPHP 1.7.1

唐云飞个人日记