mybatis如何實(shí)現(xiàn)saveOrUpdate
1. selectKey標(biāo)簽查詢
DDL
CREATE TABLE `luck_reward_info` ( ? `id` int NOT NULL AUTO_INCREMENT COMMENT 'id', ? `activity_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '活動id', ? `reward_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '獎品名', ? `reward_level` int DEFAULT NULL COMMENT '獎品等級', ? `num` int DEFAULT NULL COMMENT '獎品數(shù)量', ? `dis_number` int DEFAULT '0' COMMENT '已發(fā)獎品數(shù)量', ? `create_time` datetime DEFAULT NULL COMMENT '創(chuàng)建時間', ? `update_time` datetime DEFAULT NULL COMMENT '修改時間', ? `field` varchar(255) DEFAULT NULL COMMENT '備注', ? PRIMARY KEY (`id`,`activity_id`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC COMMENT='獎品信息表';
RewardInfoMapper接口
@Mapper
public interface RewardInfoMapper{
? ?int saveOrUpdate(RewardInfo rewardInfo);
}使用selectKey
<insert id="saveOrUpdate" >
? <selectKey keyProperty="count" resultType="int" order="BEFORE">
? ? select count(*) from luck_reward_info where id = #{id}
? </selectKey>
? <if test="count > 0">
? ? ?? ?UPDATE luck_reward_info
? ? ? ? <set>
? ? ? ? ? ? <if test='activityId != null and activityId != "" '>activity_id=#{activityId},</if>
? ? ? ? ? ? <if test='rewardName != null and rewardName != "" '>reward_name=#{rewardName},</if>
? ? ? ? ? ? <if test='rewardLevel != null '>reward_level=#{rewardLevel},</if>
? ? ? ? ? ? <if test='num != null '>num=#{num},</if>
? ? ? ? ? ? <if test='disNumber != null '>dis_number=#{disNumber},</if>
? ? ? ? ? ? update_time=now(),
? ? ? ? ? ? <if test='field != null and field != "" '>field=#{field},</if>
? ? ? ? </set>
? ? ? ? WHERE id=#{id}
? </if>
? <if test="count==0">
? ? INSERT INTO luck_reward_info(
? ? ? ? ? ? id,activity_id,reward_name,reward_level,number,create_time,field
? ? ? ? ? ? )
? ? ? ? ? ? VALUES (
? ? ? ? ? ? (select id from (select (ifnull(max(id), 0)) + 1 as id from luck_reward_info)t), #{activityId},#{rewardName},#{rewardLevel},#{number},now(),#{field}
? ? ? ? ? ? )
? </if>
</insert>報錯:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorException: No setter found for the keyProperty ‘count’ in com.cmbchina.ssm.entity.RewardInfo.
原因:RewardInfo內(nèi)必須有count屬性,來接收count值,否則報錯
優(yōu)化:接口使用Map,實(shí)體類不需要再新增count字段了
//RewardInfoMapper.java int saveOrUpdate(Map<String, Object> map);
2. 主鍵自增或者累加的,不使用selectKey
根據(jù)id是否為空,空新增,不為空修改
<insert id="saveOrUpdate" keyProperty="id" useGeneratedKeys="true">
? ? ? ? <if test="id == null">
? ? ? ? ? ? INSERT INTO luck_reward_info(
? ? ? ? ? ? ? ? id,activity_id,reward_name,reward_level,num,create_time,field
? ? ? ? ? ? )
? ? ? ? ? ? VALUES (
? ? ? ? ? ? ? ? (select id from (select (ifnull(max(id), 0)) + 1 as id from luck_reward_info)t), #{activityId},#{rewardName},#{rewardLevel},#{num},now(),#{field}
? ? ? ? ? ? )
? ? ? ? </if>
? ? ? ? <if test="id != null">
? ? ? ? ? ? UPDATE luck_reward_info
? ? ? ? ? ? <set>
? ? ? ? ? ? ? ? <if test='activityId != null and activityId != "" '>activity_id=#{activityId},</if>
? ? ? ? ? ? ? ? <if test='rewardName != null and rewardName != "" '>reward_name=#{rewardName},</if>
? ? ? ? ? ? ? ? <if test='rewardLevel != null '>reward_level=#{rewardLevel},</if>
? ? ? ? ? ? ? ? <if test='num!= null '>num=#{num},</if>
? ? ? ? ? ? ? ? <if test='disNumber != null '>dis_number=#{disNumber},</if>
? ? ? ? ? ? ? ? update_time=now(),
? ? ? ? ? ? ? ? <if test='field != null and field != "" '>field=#{field},</if>
? ? ? ? ? ? </set>
? ? ? ? ? ? WHERE id=#{id}
? ? ? ? </if>
? ? </insert>3. 主鍵為varchar的使用ON DUPLICATE KEY UPDATE
DDL
CREATE TABLE `luck_activity_info` ( ? `id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT 'id', ? `title` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '標(biāo)題', ? `start_time` datetime DEFAULT NULL COMMENT '開始時間', ? `end_time` datetime DEFAULT NULL COMMENT '結(jié)束時間', ? `sap_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '操作員工編號', ? `create_time` datetime DEFAULT NULL COMMENT '創(chuàng)建時間', ? `update_time` datetime DEFAULT NULL COMMENT '修改時間', ? PRIMARY KEY (`id`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC COMMENT='抽獎活動表';
條數(shù) 新增返回1,修改返回2
<insert id="saveOrUpdate">
? ? ? ? INSERT INTO luck_activity_info(
? ? ? ? ? ? id,title,start_time,end_time,sap_id,create_time
? ? ? ? )
? ? ? ? VALUES (
? ? ? ? ? ? #{id},#{title},#{startTime},#{endTime},#{sapId},now()
? ? ? ? )
? ? ? ? ON DUPLICATE KEY UPDATE
? ? ? ? ? ? <if test='title != null and title != "" '>title=#{title},</if>
? ? ? ? ? ? <if test='startTime != null '>start_time=#{startTime},</if>
? ? ? ? ? ? <if test='endTime != null '>end_time=#{endTime},</if>
? ? ? ? ? ? <if test='sapId != null and sapId != "" '>sap_id=#{sapId},</if>
? ? ? ? ? ? update_time=now()
? ? </insert>總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
hibernate-validator改進(jìn)校驗(yàn)框架validator?v0.4使用
這篇文章主要為大家介紹了改進(jìn)?hibernate-validator,新一代校驗(yàn)框架?validator?使用介紹?v0.4,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2023-03-03
java客戶端線上Apollo服務(wù)端的實(shí)現(xiàn)
這篇文章主要介紹了java客戶端線上Apollo服務(wù)端的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
關(guān)于Mybatis使用collection分頁問題
項(xiàng)目中mybatis分頁的場景是非常高頻的,當(dāng)使用ResultMap并配置collection做分頁的時候,我們可能會遇到獲取當(dāng)前頁的數(shù)據(jù)少于每頁大小的數(shù)據(jù)問題。接下來通過本文給大家介紹Mybatis使用collection分頁問題,感興趣的朋友一起看看吧2021-11-11
spring如何集成cxf實(shí)現(xiàn)webservice接口功能詳解
這篇文章主要給大家介紹了關(guān)于spring如何集成cxf實(shí)現(xiàn)webservice接口功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家 的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧2018-07-07
Java中使用Preferences 的 API設(shè)置用戶偏好
這篇文章主要介紹了Java中使用Preferences 的 API設(shè)置用戶偏好的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09
關(guān)于ObjectUtils.isEmpty()?和?null?的區(qū)別
這篇文章主要介紹了關(guān)于ObjectUtils.isEmpty()?和?null?的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
使用LambdaQueryWrapper動態(tài)加過濾條件?動態(tài)Lambda
這篇文章主要介紹了使用LambdaQueryWrapper動態(tài)加過濾條件?動態(tài)Lambda,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教。2022-01-01

