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语句。
posted @ 2022-09-18 23:44  周XX  阅读(1413)  评论(0编辑  收藏  举报