MysqlとOracleはMybatisで一括追加、削除、更新

3363 ワード

oracleバッチで追加されたsqlは次のとおりです.
<insert id="insertAttractionsBatch" parameterType="Java.util.List">
insert into ATTRACTIONS (
ID, NAME, LONGITUDE, LATITUDE,  UPDATE_TIME
)
  <foreach collection="list" item="item" index="index" separator="union all" > 
      (select  
#{item.id,jdbcType=VARCHAR}, #{item.name,jdbcType=VARCHAR}, #{item.longitude,jdbcType=DECIMAL}, #{item.updateTime,jdbcType=TIMESTAMP}
       from dual)
    </foreach>
</insert>

注意が必要なのは、sqlにvaluesがないことと、ラベルに(selecte.....from dual)がないことです.
MySqlのsqlはこうです.
新規:
<insert id="insertAttractionsBatch" parameterType="java.util.List">
insert into ATTRACTIONS (
ID, NAME, LONGITUDE, LATITUDE,  UPDATE_TIME
)  
    <foreach collection="list" item="item" index="index" separator="union all" >
#{item.id,jdbcType=VARCHAR}, #{item.name,jdbcType=VARCHAR}, #{item.longitude,jdbcType=DECIMAL}, #{item.updateTime,jdbcType=TIMESTAMP}
    </foreach>
</insert>

Mysqlの更新は次のとおりです.
1つ目の方法:
<update id="updateBatch" parameterType="Map">    
        update aa   set     
            a=#{fptm},    
            b=#{csoftrain}    
        where c in     
        <foreach collection="cs" index="index" item="item" pen="("separator=","close=")">    
            #{item}    
        </foreach>  
</update>  

しかし、このように変更されたフィールドの値は同じです.
2つ目の方法:
<update id="batchUpdate"  parameterType="java.util.List">  
        
          <foreach collection="list" item="item" index="index" open="" close="" separator=";">  
                update test   
                <set>  
                  test=${item.test}+1  
                </set>  
                where id = ${item.id}  
         </foreach>  
            
    </update>  

これにより、複数のSQL文を一度に実行できます.
oracleの更新は通常の方法ではできません.このようにする必要があります.
<update id="updateAttractionsBatch" parameterType="java.util.List">
    begin  
        <foreach collection="list" item="item" index="index" separator=";" > 
            update ATTRACTIONS 
            <set>
            <if test="item.id!=null and item.id!=''">
                id = #{item.id},
            </if>
            <if test="item.head!=null and item.head!=''">
                HEAD = #{item.head},
            </if>
            </set>
            where id = #{item.id}
            </foreach>
        ;end;
    </update>

削除はMySqlと同じです.
<delete id="deleteAttractions" parameterType="java.util.List">
  delete from ATTRACTIONS
  <where>
  <foreach collection="list" index="index" item="item" open="(" separator="or" close=")">     
  id=#{item.id}
    </foreach>
  </where>
  </delete>