mybatis+mysql批量插入和批量更新
一、批量插入
批量插入数据使用的sql语句是:
insert into table (字段一,字段二,字段三) values(xx,xx,xx),(oo,oo,oo)
mybatis中mapper.xml的代码如下:
<!-- 批量插入数据 -->
<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 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的代码如下:
<!-- 批量更新数据 -->
<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>
三、SELECT LAST_INSERT_ID() 的使用和注意事项
转载自:https://blog.****.net/czd3355/article/details/71302441
总体解释:将插入数据的主键返回到 user 对象中。
具体解释:
SELECT LAST_INSERT_ID():得到刚 insert 进去记录的主键值,只适用与自增主键
keyProperty:将查询到主键值设置到 parameterType 指定的对象的那个属性
order:SELECT LAST_INSERT_ID() 执行顺序,相对于 insert 语句来说它的执行顺序
resultType:指定 SELECTLAST_INSERT_ID() 的结果类型
---------------------------------------------------------------------------------------
-- 存在即更新,不存在就插入(根据ID)
insert into `vclb_mm_inventory` (`ID_`, `STOCK_ID_`, `ITEM_ID_`, `AMOUNT_`)
values ('489734716803514367', '仓库一', '水杯', 44)
ON DUPLICATE KEY UPDATE `AMOUNT_` = `AMOUNT_` + 44;
---------------------
原文:https://blog.****.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>
-----------------------------------------------------------------------
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})
结束
------------------------------------------------------------------------------
话不多多说,直接上代码,这是自己亲手查得,找了好久也是,然后才写好,写一下记录着吧
-- 存在即更新,不存在就插入(根据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_`);
---------------------
作者:小张程序员
来源:****
原文:https://blog.****.net/zhang135687/article/details/82686991