mybatis批量sql語句
阿新 • • 發佈:2018-12-20
批量插入
對應sql
insert into tabelname(colum1,colum2...) values(aa,aa...),(bb,bb...)
<!-- 批量插入資料 --> <insert id="insertBatch" parameterType="java.util.List" useGeneratedKeys="true"> <selectKey resultType="long" keyProperty="id" order="AFTER"> SELECT LAST_INSERT_ID() </selectKey> insert into wd_solr(fayu_id,tablename,name,logo,description,section_no,look_count,favorite_count,create_uid,create_time,update_time,timestamp) values <foreach collection="list" item="wdSolr" index="index" separator=","> ( #{wdSolr.fayuId},#{wdSolr.tablename},#{wdSolr.name},#{wdSolr.logo}, #{wdSolr.description},#{wdSolr.sectionNo},#{wdSolr.lookCount},#{wdSolr.favoriteCount}, #{wdSolr.createUid},#{wdSolr.createTime},#{wdSolr.updateTime},#{wdSolr.timestamp} ) </foreach> </insert>
批量更新
對應Sql
update tablename set
colum1= case id when 1 then aa
when 2 then bb
when 3 then cc
....
end,
colum2=case id when 1 then aa
when 2 then bb
when 3 then cc
....
end,
where id in (1,2,3....)
<!-- 批量更新資料 --> <update id="updateBatch"> update wd_solr set name = <foreach collection="list" item="wdSolr" index="index" separator=" " open="case id" close="end"> when #{wdSolr.id} then #{wdSolr.name} </foreach> ,logo = <foreach collection="list" item="wdSolr" index="index" separator=" " open="case id" close="end"> when #{wdSolr.id} then #{wdSolr.logo} </foreach> ,timestamp = <foreach collection="list" item="wdSolr" index="index" separator=" " open="case id" close="end"> when #{wdSolr.id} then #{wdSolr.timestamp} </foreach> where id in <foreach collection="list" item="wdSolr" index="index" separator="," open="(" close=")"> #{wdSolr.id} </foreach> </update>
批量刪除
對應sql
delete from tablename where id in (1,2,3....)
<!-- 批量刪除資料 --> <delete id="deleteBatch"> delete from wd_solr where id in ( <foreach collection="list" item="wdSolr" index="index" separator=","> #{wdSolr.id} </foreach> ) </delete>