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 '活動(dòng)id', ? `reward_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '獎(jiǎng)品名', ? `reward_level` int DEFAULT NULL COMMENT '獎(jiǎng)品等級', ? `num` int DEFAULT NULL COMMENT '獎(jiǎng)品數(shù)量', ? `dis_number` int DEFAULT '0' COMMENT '已發(fā)獎(jiǎng)品數(shù)量', ? `create_time` datetime DEFAULT NULL COMMENT '創(chuàng)建時(shí)間', ? `update_time` datetime DEFAULT NULL COMMENT '修改時(shí)間', ? `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='獎(jiǎng)品信息表';
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>報(bào)錯(cuò):
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值,否則報(bào)錯(cuò)
優(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 '開始時(shí)間', ? `end_time` datetime DEFAULT NULL COMMENT '結(jié)束時(shí)間', ? `sap_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '操作員工編號', ? `create_time` datetime DEFAULT NULL COMMENT '創(chuàng)建時(shí)間', ? `update_time` datetime DEFAULT NULL COMMENT '修改時(shí)間', ? PRIMARY KEY (`id`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC COMMENT='抽獎(jiǎng)活動(dòng)表';
條數(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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring之ShutDown?Hook死鎖現(xiàn)象解讀
這篇文章主要介紹了Spring之ShutDown?Hook死鎖現(xiàn)象解讀,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
Java微服務(wù)Filter過濾器集成Sentinel實(shí)現(xiàn)網(wǎng)關(guān)限流過程詳解
這篇文章主要介紹了Java微服務(wù)Filter過濾器集成Sentinel實(shí)現(xiàn)網(wǎng)關(guān)限流過程,首先Sentinel規(guī)則的存儲默認(rèn)是存儲在內(nèi)存的,應(yīng)用重啟之后規(guī)則會丟失。因此我們通過配置中心Nacos保存規(guī)則,然后通過定時(shí)拉取Nacos數(shù)據(jù)來獲取規(guī)則配置,可以做到動(dòng)態(tài)實(shí)時(shí)的刷新規(guī)則2023-02-02
解決執(zhí)行Junit單元測試報(bào)錯(cuò)java.lang.ClassNotFoundException問題
這篇文章主要介紹了解決執(zhí)行Junit單元測試報(bào)錯(cuò)java.lang.ClassNotFoundException問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Spring Boot @Conditional注解使用示例詳解
在SpringBoot中,@Conditional注解用于條件性地注冊bean,根據(jù)某些條件決定是否創(chuàng)建特定bean,可以實(shí)現(xiàn)Condition接口并重寫matches方法來定義條件,本文介紹Spring Boot @Conditional注解的相關(guān)知識,感興趣的朋友一起看看吧2024-12-12
Java中使用異或運(yùn)算符實(shí)現(xiàn)加密字符串
這篇文章主要介紹了Java中使用異或運(yùn)算符實(shí)現(xiàn)加密字符串,本文直接給出實(shí)現(xiàn)代碼,以及運(yùn)算結(jié)果加密實(shí)例,需要的朋友可以參考下2015-06-06
Java中遍歷ConcurrentHashMap的四種方式詳解
這篇文章主要介紹了Java中遍歷ConcurrentHashMap的四種方式詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10

