最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

mybatis如何實(shí)現(xiàn)saveOrUpdate

 更新時(shí)間:2023年02月08日 08:39:40   作者:分享心得  
這篇文章主要介紹了mybatis如何實(shí)現(xiàn)saveOrUpdate問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

1. selectKey標(biāo)簽查詢(xún)

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)品等級(jí)',
? `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屬性,來(lái)接收count值,否則報(bào)錯(cuò)

優(yōu)化:接口使用Map,實(shí)體類(lèi)不需要再新增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 '開(kāi)始時(shí)間',
? `end_time` datetime DEFAULT NULL COMMENT '結(jié)束時(shí)間',
? `sap_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '操作員工編號(hào)',
? `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)象解讀

    這篇文章主要介紹了Spring之ShutDown?Hook死鎖現(xiàn)象解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Java微服務(wù)Filter過(guò)濾器集成Sentinel實(shí)現(xiàn)網(wǎng)關(guān)限流過(guò)程詳解

    Java微服務(wù)Filter過(guò)濾器集成Sentinel實(shí)現(xiàn)網(wǎng)關(guān)限流過(guò)程詳解

    這篇文章主要介紹了Java微服務(wù)Filter過(guò)濾器集成Sentinel實(shí)現(xiàn)網(wǎng)關(guān)限流過(guò)程,首先Sentinel規(guī)則的存儲(chǔ)默認(rèn)是存儲(chǔ)在內(nèi)存的,應(yīng)用重啟之后規(guī)則會(huì)丟失。因此我們通過(guò)配置中心Nacos保存規(guī)則,然后通過(guò)定時(shí)拉取Nacos數(shù)據(jù)來(lái)獲取規(guī)則配置,可以做到動(dòng)態(tài)實(shí)時(shí)的刷新規(guī)則
    2023-02-02
  • Spring源碼解密之自定義標(biāo)簽與解析

    Spring源碼解密之自定義標(biāo)簽與解析

    這篇文章主要給大家介紹了關(guān)于Spring源碼解密之自定義標(biāo)簽與解析的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01
  • 解決執(zhí)行Junit單元測(cè)試報(bào)錯(cuò)java.lang.ClassNotFoundException問(wèn)題

    解決執(zhí)行Junit單元測(cè)試報(bào)錯(cuò)java.lang.ClassNotFoundException問(wèn)題

    這篇文章主要介紹了解決執(zhí)行Junit單元測(cè)試報(bào)錯(cuò)java.lang.ClassNotFoundException問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • IDEA整合jeesite4.x及安裝教程

    IDEA整合jeesite4.x及安裝教程

    本文給大家介紹IDEA整合jeesite4.x及安裝教程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-07-07
  • Spring Boot @Conditional注解使用示例詳解

    Spring Boot @Conditional注解使用示例詳解

    在SpringBoot中,@Conditional注解用于條件性地注冊(cè)bean,根據(jù)某些條件決定是否創(chuàng)建特定bean,可以實(shí)現(xiàn)Condition接口并重寫(xiě)matches方法來(lái)定義條件,本文介紹Spring Boot @Conditional注解的相關(guān)知識(shí),感興趣的朋友一起看看吧
    2024-12-12
  • Java實(shí)現(xiàn)微信支付的簽名算法示例

    Java實(shí)現(xiàn)微信支付的簽名算法示例

    這篇文章主要為大家介紹了Java實(shí)現(xiàn)微信支付的簽名算法實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • Java中使用異或運(yùn)算符實(shí)現(xiàn)加密字符串

    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的四種方式詳解

    這篇文章主要介紹了Java中遍歷ConcurrentHashMap的四種方式詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • java實(shí)現(xiàn)紙牌游戲之小貓釣魚(yú)算法

    java實(shí)現(xiàn)紙牌游戲之小貓釣魚(yú)算法

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)紙牌游戲之小貓釣魚(yú)算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01

最新評(píng)論

丹寨县| 南昌县| 隆尧县| 兰州市| 迁西县| 屏东县| 鄂托克旗| 茌平县| 双柏县| 文昌市| 厦门市| 泰顺县| 乌苏市| 衡阳市| 兰溪市| 安平县| 双城市| 阜宁县| 和田县| 石城县| 司法| 增城市| 固阳县| 扎囊县| 佳木斯市| 八宿县| 慈溪市| 阆中市| 开封市| 莱芜市| 壤塘县| 济南市| 平凉市| 舞钢市| 都匀市| 大同市| 商河县| 余庆县| 九江市| 文登市| 江津市|