*mapper.xml でよく使用される SQL 関連タグの紹介

タグの場合

使用法:

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

ここのタグ

効果:

タグは続行されます自動判定: いずれかの条件が true でない場合、where キーワードは SQL ステートメントに表示されません (キー ポイント)。
いずれかの条件が true の場合、余分な 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>

トリムタグ

効果:

トリムは、タグを使用できる場所など、必要な機能をカスタマイズするために使用されます。

使用法:

<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: トリムタグ内の SQL ステートメントにプレフィックスを追加します。例: prefix="("、接頭辞 "(" を追加します。
suffix: トリム タグの SQL ステートメントに接尾辞を追加します。例: 、suffix=")"接尾辞 "(" を追加します。
suffixOverrides: 冗長な接尾辞の内容を削除するように指定します。次のようなものです。 、suffixOverrides=","冗長な要素を削除します。トリム タグ内の 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})—>ステートメントは正しいです

タグを設定する

効果:

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>

それ以外の場合はタグを選択してください

効果:

いずれかの条件が満たされる場合は、条件付きクエリを実行します。それ以外の場合は、条件 id>1 のみが使用されます (つまり、前のタグがいずれも条件を満たしません)。
if・・・・・・elseと同等

Mybatis には else タグはありませんが、when else タグを使用してこの効果を実現できます。

使用法:

<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 のような構文で使用されます。

使用法(リスト コレクション パラメーターを受け取る):

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

ビンラベル

バインド タグは、文字列の結合を再度行うようなもので、インターネット上ではバインドとも呼ばれます。意味は同じですが、後続の利用を容易にするためのものです。

使用法:

<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