Some typical SQL in our daily coding.

一. 數據庫的表設計中經常會有使用json存儲的大字段,經常會有模糊匹配大字段的需求,在 mapper.xml 文件中如何進行模糊匹配呢。

<if test="feeType != null and feeType !=''">
      and fee_extension like (CONCAT(CONCAT('%"feeType ":', #{feeType, jdbcType=VARCHAR}),'%')) 
    </if>

二. 若 where 條件中的字段爲空,需要備用字段值進行替換。使用 otherwise

<choose>
            <when test="order.isPreOrder!= null">
                #{order.isPreOrder,jdbcType=INTEGER},
            </when>
            <otherwise>
                0,
            </otherwise>
        </choose>

三. 如何複用一大串字段名稱和對應的值?使用一個引用id——refid。

<sql id="batchInsert">
        insert into tableA
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <include refid="cn.com.mollychin.ob.TableA.Insert_Column_List"></include>
        </trim>
        values
        <foreach collection="domainList" item="domain" separator=",">
            (
            <include refid="cn.com.mollychin.ob.TableA.Insert_Value_List"></include>
            )
        </foreach>
    </sql>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章