1. 程式人生 > >常用mybatis中sql寫法

常用mybatis中sql寫法

目錄

 

1.時間處理

2.in操作包含大於1000

3.oracle批量處理

4.orcle模糊查詢 (左模糊,全模糊)


1.時間處理


<if test="timeLimitStart != null and timeLimitEnd != null">
     AND to_date(TIME_LIMIT,'yyyy-mm-dd') BETWEEN #{timeLimitStart,jdbcType=TIMESTAMP} AND
#{timeLimitEnd,jdbcType=TIMESTAMP}
</if>


<if test="creationTimeStart != null and creationTimeEnd != null">
    AND CREATION_TIME BETWEEN #{creationTimeStart,jdbcType=TIMESTAMP} AND
#{creationTimeEnd,jdbcType=TIMESTAMP}
</if>

2.in操作包含大於1000


<if test="idCardBranchList !=null and idCardBranchList.size>0">
    AND ( ID_CARD IN
    <foreach collection="idCardBranchList" index="index" item="item" open="(" separator="," close=")">
        <if test="(index % 999) == 998">NULL ) OR ID_CARD IN (</if>#{item,jdbcType=VARCHAR}
    </foreach>
    )
</if>

3.oracle批量處理


<!-- 批量插入 -->
<insert id ="insertBatch" parameterType="java.util.List" useGeneratedKeys="false">
  insert into SUPPLIER_UNIT_PRICE
  (ID,SUPPLIER_ID)
  SELECT SEQ_S_UINT_PRICE.NEXTVAL,t.* from(
  <foreach collection="list" item="item" index="index" separator="union all">
    SELECT
    #{item.supplierId}
    FROM DUAL
  </foreach>
  )t
</insert>

<!-- 批量更新 -->
<update id="updateBatch" parameterType="java.util.List">
  update SUPPLIER_UNIT_PRICE
  <trim prefix="set" suffixOverrides=",">
    <trim prefix="UNIT = case" suffix="end,">
      <foreach collection="list" item="item" index="index">
        <if test="item.unit!=null">
          when id=#{item.id} then #{item.unit}
        </if>
      </foreach>
    </trim>
    <trim prefix="PRICE = case" suffix="end,">
      <foreach collection="list" item="item" index="index">
        <if test="item.price!=null">
          when id=#{item.id} then #{item.price}
        </if>
      </foreach>
    </trim>
  </trim>
  where ID in
  <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
    #{item.id}
  </foreach>
</update>

4.orcle模糊查詢 (左模糊,全模糊)

<if test="code != null"> 
    and CODE LIKE CONCAT(#{code, jdbcType=VARCHAR}, '%')
</if>

<if test="code != null">
    and settle.code like concat(concat('%',#{code}),'%')
</if>