mybatis 动态标签详解

if 标签
if标签中有一个test属性,test属性值是一个符合OGNL要求的判断表达式,表达式的结果可以使true或者false, 除此之外所有的非0值都为true

(1)、数字类型

1.1 例如:  如果参数为数字类型的时候没有特俗需求的情况只需要判断是否为null即可。
<if test="id != null"></if>
 1.2 例如:如果有特俗需求,例如判断是否大于某个数的时候才行。只需要加上对应的条件判断即可。
<if test='id != null and id > 28'></if>
 1.3 例如:mybatis对于这种大于小于等等还有另一种形式。

<if test='id != null and id gt 28'></if>
<if test='id != null and id > 28'></if> 这两种一样

<if test='id != null and id gte 28'></if>
<if test='id != null and id >= 28'></if> 这两种一样

<if test='id != null and id lt 28'></if> 正常
<if test='id != null and id < 28'></if> 报错

<if test='id != null and id lte 28'></if> 正常
<if test='id != null and id <= 28'></if> 报错

对应关系:

gt          对应             >

gte         对应              >=

lt           对应             <(会报错  相关联的 "test" 属性值不能包含 '<' 字符)

lte          对应             <=(会报错  相关联的 "test" 属性值不能包含 '<=' 字符)

(2) 字符串类型

2.1 例如: 如果不需要过滤空串的情况 仅仅判断null即可

<if test="username != null"></if>
2.2 例如:如果需要过滤空串,添加空串判断即可  不支持 &&|| ,所以这里用 and  or 来做逻辑与或的判断 

<if test="username != null and '' != username"></if> 或者 <if test="username != null and ''  neq username"></if>
2.3 例如:如果判断字符串是否已某个特俗字符开头,结尾等。直接调用String的对应方法即可

    <if test="username != null and username.indexOf('ji') == 0"> </if> <!-- 是否以什么开头 -->
    <if test="username != null and username.indexOf('ji') >= 0"> </if> <!-- 是否包含某字符 -->
    <if test="username != null and username.lastIndexOf('ji') > 0"></if>  <!-- 是否以什么结尾 -->
2.4 例如: 是否是某个特定字符串,某些业务有此需要。

<if test="username != null and 'hello' == username"></if> 或者<if test="username != null and 'hello' eq username"></if>

注意:

     <if test="username != null and 'hello' == username"></if>这种形式的写法在参数类型是字符串的时候是没有问题的,

但是参数类型为非字符串类型的时候就需要写成 <if test="username != null and 'hello'.toString() == username.toString()"></if>

仅仅写成<if test="username != null and 'hello'.toString() == username"></if>也会有很大可能会挂。

也许你会说非字符串的为什么要写成这样。这就要看特殊需要了。

对应关系:

eq                  对应                ==

neq               对应                 !=
 3 判断list是否为空

if条件判断可以直接调用对象自身的方法进行逻辑判断,所以list判空。可以调用.size()>0或者.isEmpty()

例如:<if test="userList != null and userList.isNotEmpty()"></if> , <if test="userList != null and userList.size()>0"></if>
4 map参数同同理  取值的话 map.key(map中的key名字)即可

where标签
标签会进行自动判断, 如果任何条件都不成立,那么就在sql语句里就不会出现where关键字
如果有任何条件成立,会自动去掉多出来的 and 或者 or。(就不需要我们追加1=1之类的入侵性代码了)

用法:
<select id="listProduct" resultType="Product">
    select * from product_
    <where>
        <if test="name!=null">
            and name like concat('%',#{name},'%')
        </if>        
        <if test="price!=null">
            and price > #{price}
        </if>
    </where>     
</select>

set标签
与where标签类似的,在update语句里也会碰到多个字段相关的问题。 在这种情况下,就可以使用set标签。 其效果与where标签类似,有数据的时候才进行设置。 set 元素可以用于动态包含需要更新的列,忽略其它不更新的列,set 元素会动态地在行首插入 SET 关键字,并会删掉额外的逗号。

用法:

<update id="updateProduct" parameterType="Product" >
    update product_
    <set>
        <if test="name != null">name=#{name},</if>
        <if test="price != null">price=#{price}</if> 
    </set>
     where id=#{id}   
</update>

trim标签
trim 有四个参数,分别是:
prefix:前缀(以什么开头), 
prefixoverride:去掉第一个(比如 “and”或者是“or”)
suffix:后缀(以什么结尾)
suffixoverride:去掉最后标记的字符(比如“,”)

用法:
<select id="listProduct" resultType="Product">
    select *from product_
    <trim prefix="WHERE" prefixOverrides="AND |OR ">
        <if test="name!=null">
            and name like concat('%',#{name},'%')
        </if>        
        <if test="price!=null and price!=0">
            and price > #{price}
        </if>
    </trim>      
</select>

trim 用来定制想要的功能,比如where标签就可以用trim 来替换

<trim prefix="WHERE" prefixOverrides="AND |OR ">
  ... 
</trim>  
<update id="updateProduct" parameterType="Product" >
    update product_
    <trim prefix="SET" suffixOverrides=",">
        <if test="name != null">name=#{name},</if>
        <if test="price != null">price=#{price},</if>   
    </trim>
     where id=#{id}   
</update>

set标签就可以用trim来替换 ,运行set标签中的代码,其效果是一样的。
<trim prefix="SET" suffixOverrides=",">
  ...
</trim>

choose when otherwise 标签
有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个。MyBatis提供了choose 元素,按顺序判断when中的条件出否成立,如果有一个成立,则choose结束。当choose中所有when的条件都不满则时,则执行 otherwise中的sql。类似于Java 的switch 语句,choose为switch,when为case,otherwise则为default。if是与(and)的关系,而choose是或(or)的关系

<select id="getUserList" resultType="com.it.bean.User" parameterType="com.it.bean.User">  
    SELECT <include refid="resultParam"></include> FROM User u   
    <where>  
        <choose>  
            <when test="username !=null and username != ''">  
                u.username LIKE CONCAT(CONCAT('%', #{username}),'%')  
            </when >  
            <when test="sex != null">  
                AND u.sex = #{sex}  
            </when >  
            <when test="birthday != null ">  
                AND u.birthday = #{birthday}  
            </when >  
            <otherwise> 
 	            AND u.age = #{age}
            </otherwise>  
        </choose>  
    </where>    
</select>

foreach标签
foreach标签通常用于in 这样的语法里。
collection :collection属性的值有三个分别是list、array、map三种,分别对应的参数类型为:List、数组、map集合,我在上面传的参数为数组,所以值为array

item : 表示在迭代过程中每一个元素的别名

index :表示在迭代过程中每次迭代到的位置(下标)

open :前缀

close :后缀

separator :分隔符,表示迭代时每个元素之间以什么分隔

<select id="listProduct" resultType="Product">
      SELECT * FROM product_
        WHERE ID in
            <foreach item="item" index="index" collection="list"
                open="(" separator="," close=")">
                #{item}
            </foreach>
</select>

bind标签
bind标签中,value对应传入实体类的某个字段,name属性既给对应字段取的变量名。在value属性中可以使用字符串拼接等特殊处理。

用法:
    <select id="listProduct" resultType="Product">
        <bind name="likename" value="'%' + name + '%'" />
        select * from   product_  where name like #{likename}
    </select>

sql片段标签
通过该标签可定义能复用的sql语句片段,在执行sql语句标签中直接引用即可。
这样既可以提高编码效率,还能有效简化代码,提高可读性。 sql标签 用来封装SQL语句, include 标签来调用。

用法:
<!--定义sql片段-->
<sql id="orderAndItem">    o.order_id,o.cid,o.address,o.create_date,o.orderitem_id,i.orderitem_id,i.product_id,i.count
</sql>
<select id="findOrderAndItemsByOid" parameterType="java.lang.String" resultMap="BaseResultMap">
    select
<!--引用sql片段-->
    <include refid="orderAndItem" />
    from ordertable o
    join orderitem i on o.orderitem_id = i.orderitem_id
    where o.order_id = #{orderId}
</select>

你可能感兴趣的:(mybatis框架,java)