MyBatis XML一个方法执行插入或更新操做(PostgreSQL)

MyBatis XML一个方法执行插入或更新操做(PostgreSQL)

在MyBatis中,你可以使用PostgreSQL的INSERT ... ON CONFLICT子句来实现插入或更新(即"upsert")操作。以下是一个示例,展示如何在MyBatis中配置和执行这样的操作。

PostgreSQL SQL 示例

假设你有一个表users,包含以下字段:

id (主键)

name

email

你希望在插入数据时,如果id已经存在,则更新相应的name和email字段。

SQL语句可能如下:

sql 复制代码
INSERT INTO users (id, name, email)
VALUES (1, '黎明', '123@163.com')
ON CONFLICT (id) DO UPDATE
SET name = '黎明',
    email = '123@163.com';

MyBatis Mapper XML 配置

首先,定义你的MyBatis Mapper XML文件,比如UserMapper.xml:

xml 复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.example.mapper.UserMapper">

  <insert id="insertAndUpdate" parameterType="com.example.model.User">
    INSERT INTO users
    <trim prefix="(" suffix=")" suffixOverrides=",">
    	<if test="id!=null">
        	id,
        </if>
        <if test="name!=null and name!=''">
        	name,
        </if>
        <if test="email!=null and email!=''">
        	email,
        </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
        <if test="id!= null">
            #{id},
        </if>
        <if test="name!=null and name!=''">
            #{name},
        </if>
        <if test="email!=null and email!=''">
            #{email},
        </if>
    </trim>
    ON conflict(id)
    DO UPDATE SET
    <trim prefix="" suffix="" suffixOverrides=",">
        <if test="id!=null">
            id=#{id},
        </if>
        <if test="name!=null and name!=''">
        	name=#{name},
        </if>
        <if test="email!=null and email!=''">
        	email=#{email},
        </if>
    </trim>
  </insert>

</mapper>

MyBatis Mapper 接口

接下来,定义对应的Mapper接口,比如UserMapper.java:

java 复制代码
package com.example.mapper;

import com.example.model.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface UserMapper {
    // 或者,如果你使用XML配置,可以省略注解,方法签名要匹配XML中的id
    void insertAndUpdate(User user);

	// 或者,你可以直接在接口上写sql
    @Insert("INSERT INTO users (id, name, email) VALUES (#{id}, #{name}, #{email}) ON CONFLICT (id) DO UPDATE SET name = #{name}, email = #{email}")
    void upsertUser(User user);
}

注意:ON CONFLICT子句中的冲突字段(id)必须是表的主键或有唯一约束的字段

相关推荐
双力臂4044 小时前
MyBatis动态SQL进阶:复杂查询与性能优化实战
java·sql·性能优化·mybatis
慕y2746 小时前
Java学习第十五部分——MyBatis
java·学习·mybatis
ldj20208 小时前
2025 Centos 安装PostgreSQL
linux·postgresql·centos
一勺菠萝丶13 小时前
Spring Boot + MyBatis/MyBatis Plus:XML中循环处理List参数的终极指南
xml·spring boot·mybatis
coding and coffee17 小时前
狂神说 - Mybatis 学习笔记 --下
java·后端·mybatis
耀耀_很无聊1 天前
07_通过 Mybatis 自动填充记录的创建时间和更新时间
mybatis
程序员张31 天前
SQL分析与打印-p6spy组件
spring boot·sql·mybatis·mybatisplus·p6spy
坤坤不爱吃鱼2 天前
【MySQL\Oracle\PostgreSQL】迁移到openGauss数据出现的问题解决方案
mysql·postgresql·oracle
行星0082 天前
PostgreSQL大表创建分区实战
数据库·postgresql
喜欢敲代码的程序员2 天前
SpringBoot+Mybatis+MySQL+Vue+ElementUI前后端分离版:项目搭建(一)
spring boot·mysql·elementui·vue·mybatis