• SpringBoot集成Mybatis 实现InsertOrUpdate功能


    需求场景

    在项目开发过程中,难免会遇到这样的场景:对一张表,当数据不存在的时候,进行insert插入操作;数据存在的时候,进行update更新操作;

    下面就来使用Mybatis的InsertOrUpdate功能来实现一下:

    具体实现

    关于SpringBoot集成Mybatis可以参考:https://blog.csdn.net/weixin_43759352/article/details/104494336
    在这里不再详细介绍

    新建实体类City.java

    @Data
    @ToString
    public class City implements BaseDO {
        private String id;
        private String province;
        private String city;
        private String district;
        private String detail;
        
        private String insertBy;
        private String updateBy;
        private Date insertTime;
        private Date updateTime;
    
        private int count;
    }
    

    新增Mapper接口及对应的Mapper.xml

    void insertOrUpdateCity(City city);
    
    <update id="insertOrUpdateCity" parameterType="com.example.simplememory.entity.City">
            <selectKey keyProperty="count" resultType="int" order="BEFORE">
                select count(1) from sys_city where id= #{id}
            </selectKey>
            <if test="count > 0">
                update sys_city
                <set>
                    <if test="province != null and province != ''">
                        province= #{province},
                    </if>
                    <if test="city != null and city != ''">
                        city= #{city},
                    </if>
                    <if test="district != null and district != ''">
                        district= #{district},
                    </if>
                    <if test="detail != null and detail != ''">
                        detail= #{detail},
                    </if>
                </set>
                where ID = #{id}
            </if>
            <if test="count == 0">
                insert into sys_city
                <trim prefix="(" suffix=")" suffixOverrides=",">
                    ID,
                    <if test="province != null and province != ''">
                        province,
                    </if>
                    <if test="city != null and city != ''">
                        city,
                    </if>
                    <if test="district != null and district != ''">
                        district,
                    </if>
                    <if test="detail != null and detail != ''">
                        detail,
                    </if>
                </trim>
                <trim prefix="values (" suffix=")" suffixOverrides=",">
                    #{id},
                    <if test="province != null and province != ''">
                        #{province},
                    </if>
                    <if test="city != null and city != ''">
                        #{city},
                    </if>
                    <if test="district != null and district != ''">
                        #{district},
                    </if>
                    <if test="detail != null and detail != ''">
                        #{detail},
                    </if>
                </trim>
            </if>
        </update>
    

    说明:

    上面的selectKey标签,可以给update标签中的parameterType属性(model类)对应的对象设置属性值。

    selectKey标签的属性描述:

    • keyProperty属性:selectKey 语句结果应该被设置的目标属性。上述对应的是实体City类的count属性。
    • resultType属性:结果的类型,此处为属性count的类型。
    • order属性:可以被设置为 BEFORE 或 AFTER。BEFORE表示先执行selectKey语句,后执行* update语句;AFTER表示先执行update语句,后执行selectKey语句。
  • 相关阅读:
    菜鸟学python之程序初体验
    菜鸟学python之大数据的初认识
    js获取本地ip地址和外网IP地址
    Js中foreach()用法及使用的坑
    模拟实现Promise,探究Promise原理
    搞懂JS的事件循环(Event Loop)和宏任务/微任务
    NodeJS 中的 LRU 缓存(CLOCK-2-hand)实现
    设计模式在前端项目中的应用
    JS 中一些高效的魔法运算符
    Js中如何克隆对象?
  • 原文地址:https://www.cnblogs.com/zhouXX/p/16706301.html
Copyright © 2020-2023  润新知