*mapper.xml中常用的SQL相关标签简介

if标签

用法:

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

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!=0">
            and price > #{price}
        </if>
    </where>     
</select>

sql片段标签

作用:

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

用法:

<!--定义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>

trim标签

作用:

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

用法:

<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>

<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>

这个标签还经常按照这样的形式出现:

<trim prefix="" suffix="" suffixOverrides=""prefixOverrides=""></trim>

prefix:在trim标签内sql语句加上前缀。如:prefix="(",添加前缀"("。
suffix:在trim标签内sql语句加上后缀。如:suffix=")",添加后缀"("。
suffixOverrides:指定去除多余的后缀内容,如:suffixOverrides=",",去除trim标签内sql语句多余的后缀","。
prefixOverrides:指定去除多余的前缀内容。

例子:

<insert id="insertEstimateSurface" parameterType="User">
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides=",">
        <if test="id != null">id,</if>
        <if test="name != null">name,</if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
        <if test="id != null">#{id},</if>
        <if test="name != null">#{name},</if>
     </trim>
</insert>

这边如果没有加入prefix="",suffix="",suffixOverrides=""这几个参数:

sql语句为: insert into user id,name, #{id},#{name}, —> 这语句错误

加入prefix="", suffix=""这几个参数后:

sql语句为: insert into user (id,name,) values ( #{id},#{name},) —>这语句错误,后面还多了个","

加入prefix="",suffix="",suffixOverrides=""这几个参数后:
sql语句为: insert into user (id, name) values ( #{id}, #{name}) —>语句正确

set标签

作用:

与where标签类似的,在update语句里也会碰到多个字段相关的问题。 在这种情况下,就可以使用set标签。
其效果与where标签类似,有数据的时候才进行设置。

用法:

<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>

choose when otherwise标签

作用:

有任何任何条件符合,就进行条件查询, 否则就只使用id>1这个条件(即前面的标签都不符合条件)。
也就相当于if···········else

Mybatis里面没有else标签,但是可以使用when otherwise标签来达到这样的效果。

用法:

<mapper namespace="com.how2java.pojo">
	<select id="listProduct" resultType="Product">
              SELECT * FROM product_
              <where>
                <choose>
                  <when test="name != null">
                    and name like concat('%',#{name},'%')
                  </when>          
                  <when test="price !=null and price != 0">
                    and price > #{price}
                  </when>                
                  <otherwise>
                    and id >1
                  </otherwise>
                </choose>
              </where>
	</select>
</mapper>

foreach标签

作用:

foreach标签通常用于in 这样的语法里。

用法(接收一个List集合参数):

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

bin标签

bind标签就像是再做一次字符串拼接,网上也有说叫绑定,差不多意思,只是方便后续的使用。

用法:

<mapper namespace="com.how2java.pojo">

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

</mapper>

猜你喜欢

转载自blog.csdn.net/u013611126/article/details/115399421