mybaits之动态sql

来源:互联网 发布:怎么用u盘安装ubuntu 编辑:程序博客网 时间:2024/05/15 23:43

mybaits除了提供连接数据库,使java和数据库语句分离之外,还有一个显著的特点就是使用动态sql语句。这些sql语句均写在map映射文件中,并通过一系列标记来完成。
1.if标记。常用形式:

<select id="**" resultType="**"     parameterType="**">    select * from table where 1=1        <if test="attr!=null">            and attrColumn>#{attr}        </if></select>

在where后面加上1=1,是为了与后面的and连接,否则会出现语法错误(不过利用后面的where标记便不再需要),若attr属性不为null,执行语句为:

select * from table where 1=1 and attrColumn>?

if是动态sql中最最常用的标记

2.choose标记。常用形式:

select * from table where 1=1<choose>       <when test="attr1!=null">           and attrColumn like #{attr}       </when>        ……            <otherwise>           and attr2 is not null      </otherwise></choose> 

choose标记类似于java或者c语言中的switch-case语句的作用,至上而下执行,满足when语句则执行后面相应语句,否则跳到下一条执行语句。若数据库中attr1属性不为null,则执行语句为:

select * from table where 1=1  and attrColumn like ?

若为null,则执行语句为:

select * from table where 1=1  and attr2 is not null

3.where标记。常用形式:

select * from table <where>    <if test="attr1!=null">        attr1Column like #{attr1}    </if>    <if test="attr2!=null">        and attr2Column =#{attr2}    </if></where>

可以看到,使用where标记有两个好处:1. 不再需要在where后面加上1=1这个条件;2.会智能处理and,若attr1,attr2不为null,执行语句为:

select * from table where attr1Column like ? and attr2Column =?

若attr1为null,attr2不为null,则执行语句为:

select * from table where attr2Column = ? 

这个时候where标记的智能就体现出来了

4.set标记。常用形式:

update table<set>        <if test="attr1!= null">attr1Column=#{attr1},</if>        <if test="attr2!= null">attr2Column=#{attr2},</if></set>where attr3Column=#{attr3}

显然,set标记主要用于更新数据表,使用set标记是一种智能的赋值,智能处理后面的“,”,若attr1,attr2不为null,执行语句为:

update table set attr1Column=?,attr2Column=? where attr3Column = ?

可以看到最后一个if判断的“,”已经被智能去掉。

5.trim标记。trim标记一般可以与where,set标记组合使用。trim标记的核心是4个元素,

prefix suffix prefixOverrides suffixOverrides 前缀 后缀 自动判断前置 自动判断后置

利用trim代理set:

<trim prefix="SET"  suffix="WHERE attr2Column = #{attr2}"                                           suffixOverrides=",">  <if test="attr1 != null and attr1 != '' ">  attr1Column = #{attr1},</if>……</trim>

等同于:

<set><if test="attr1!= null and attr1!=''">attr1Column=#{attr1},</if>……</set>where attr2Column=#{attr2}

利用trim代理where标记:

select * from table <trim prefix="where"   prefixOverrides="and|or">          <if test="attr1!=null">            and attr1Column like #{attr1}        </if>        <if test="attr2!=null">            and attr2Column =#{attr2}        </if></trim>

等同于:

select * from table <where>    <if test="attr1!=null">        attr1Column like #{attr1}    </if>    <if test="attr2!=null">        and attr2Column =#{attr2}    </if></where>

6.foreach标记。foreach标记是最为强大的一个标记,最基本的功能是用来实现遍历查询,循环赋值。foreach标记主要是使用一下一些属性标记来实现:
item:迭代的内容
collection:循环的集合或指定的类型
separator:元素之间的分隔符
open:开始符,可选属性
close:闭合符,可选属性,一般与open联合使用
index:list的序号,可选属性
使用foreach进行遍历查询的常用形式:

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

该语句的意思便是对list进行遍历,每个元素的分隔符为”,”,list中为程序中赋的id的初始值,通过该语句查询指定的id

使用foreach进行循环赋值的常用形式:

insert into table(attr1, attr2) values   <foreach item="item"  collection="list"  separator="," >(#{item.attr1}, #{item.attr2}) </foreach>  

以上便是mybaits框中常用到几个动态标记,十分方便和智能

0 0