mybatis+mysql批量插入和批量更新
1、批量插入
批量插入數據使用的sql語句是:java
insert into table (字段一,字段二,字段三) values(xx,xx,xx),(oo,oo,oo)
mybatis中mapper.xml的代碼以下:mysql
<!-- 批量插入數據 --> <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>
2、批量更新
批量更新數據使用的sql語句是:程序員
UPDATE table SET aa = CASE id WHEN 1 THEN 'oo' WHEN 2 THEN 'pp' WHEN 3 THEN 'qq' END ,SET bb = CASE id WHEN 1 THEN 'xx' WHEN 2 THEN 'yy' WHEN 3 THEN 'zz' END WHERE id IN (1,2,3)
上面這一條mysql語句能夠更新多條記錄,mybatis中mapper.xml的代碼以下:sql
<!-- 批量更新數據 --> <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>
3、SELECT LAST_INSERT_ID() 的使用和注意事項
轉載自:https://blog.csdn.net/czd3355/article/details/71302441數據庫
整體解釋:將插入數據的主鍵返回到 user 對象中。
具體解釋:
SELECT LAST_INSERT_ID():獲得剛 insert 進去記錄的主鍵值,只適用與自增主鍵
keyProperty:將查詢到主鍵值設置到 parameterType 指定的對象的那個屬性
order:SELECT LAST_INSERT_ID() 執行順序,相對於 insert 語句來講它的執行順序
resultType:指定 SELECTLAST_INSERT_ID() 的結果類型mybatis
---------------------------------------------------------------------------------------app
-- 存在即更新,不存在就插入(根據ID)
insert into `vclb_mm_inventory` (`ID_`, `STOCK_ID_`, `ITEM_ID_`, `AMOUNT_`)
values ('489734716803514367', '倉庫一', '水杯', 44)
ON DUPLICATE KEY UPDATE `AMOUNT_` = `AMOUNT_` + 44;
---------------------
原文:https://blog.csdn.net/zhang135687/article/details/82686991
<!-- 批量更新數據 -->
<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>ui
-----------------------------------------------------------------------spa
SQL如何實現存在即更新,沒有就插入
SQL Server,今天遇到個問題就是SQL如何實現存在即更新,沒有就插入。
我用mybatis寫的:意思是就是判斷類和位置同時存在才更新,只有一個或都不存在就插入。可是我寫的結果倒是隻要(類別和positionID)其中一個存在,就把數據庫中的全部有關的給跟新了,請問各位大俠有什麼好的解決辦法嗎?
若是存在(SELECT * FROM T_Mobilie_BackstageCommon其中類別=#{類別,jdbcType爲VARCHAR}和positionId =#{positionId,jdbcType爲整數})
開始
更新T_Mobilie_BackstageCommon設定類別=#{類別,jdbcType爲VARCHAR},pathStr =#{pathStr ,jdbcType爲VARCHAR},jumpToUrl =#{jumpToUrl,jdbcType爲VARCHAR},標題=#{標題,jdbcType爲VARCHAR},備忘錄=#{備忘錄,jdbcType爲VARCHAR},expansion_one =#{expansion_one,jdbcType爲VARCHAR}, expansion_two =#{expansion_two,jdbcType = VARCHAR}
end
else
begin
插入到T_Mobilie_BackstageCommon(category,pathStr,jumpToUrl,title,needToLogin,positionId,memo,expansion_one,expansion_two)
值(#{category,jdbcType = VARCHAR},#{pathStr,jdbcType = VARCHAR},#{jumpToUrl,jdbcType = VARCHAR} ,#{title,jdbcType = VARCHAR},#{needToLogin,jdbcType = INTEGER},#{positionId,jdbcType = INTEGER},#{memo,jdbcType = VARCHAR},#{expansion_one,jdbcType = VARCHAR},#{expansion_two, jdbcType = VARCHAR})
結束.net
------------------------------------------------------------------------------
話很少多說,直接上代碼,這是本身親手查得,找了很久也是,而後才寫好,寫一下記錄着吧
-- 存在即更新,不存在就插入(根據ID)
insert into `vclb_mm_inventory` (`ID_`, `STOCK_ID_`, `ITEM_ID_`, `AMOUNT_`)
values ('489734716803514367', '倉庫一', '水杯', 44)
ON DUPLICATE KEY UPDATE `AMOUNT_` = `AMOUNT_` + 44;
-- 將物品名稱與倉庫名稱修改成庫存表中惟一索引
ALTER TABLE vclb_mm_inventory ADD unique(`STOCK_ID_` , `ITEM_ID_`);
--------------------- 做者:小張程序員 來源:CSDN 原文:https://blog.csdn.net/zhang135687/article/details/82686991