需求:gisdata表中插入数据,如果wxid数据存在就更新,不存在就插入
Mysql的mybatis配置
<?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.test.wechat.dao.GisDataMapper">
<sql id="userColumns">
wxid,ip,port,login_username,login_status,wx_num,tel,qq,email,task_name,min_longitude,max_longitude,min_latitude,max_latitude,clent_id,country,province,city,town
</sql>
<insert id="insertGisData" parameterType="GisData" useGeneratedKeys="true">
replace into gisdata (<include refid="userColumns"/>) values (#{wxid},#{ip},#{port},#{loginUsername},#{loginStatus},#{wxNum},#{tel},#{qq},#{email},#{taskName},#{minLongitude},#{maxLongitude},#{minLatitude},#{maxLatitude},#{clentId},#{country},#{province},#{city},#{town})
</insert>
<!-- <delete id="deleteGisData" parameterType="int">
delete from gis_data
</delete> -->
</mapper>
oracle的mybatis配置
<insert id="insertGisData" parameterType="GisData">
merge into gisdata gd
using dual on(gd.wxid=#{wxid})
when matched then
update
set gd.ip = #{ip},
gd.port=#{port},
gd.wx_num=#{wx_num}
gd.town=#{town}
when not matched then
insert
(
wxid,ip,port,wx_num,town
)
values
(
#{wxid},#{ip}, #{port},#{wx_num},#{town}
)
</insert>
备注:MySQL 的 replace into:插入数据前,replace into会首先根据主键或唯一索引判断是否存在相同的记录,如果存在,则先删除原来数据,然后再插入新数据;如果没有相同的记录,则直接插入。
oracle的merge into:与mysql中的replace into类似,在插入数据前,merge into也会根据主键判断是否有相同的记录,不同的是后面的操作,merge into对于存在的相同记录可以不做任何操作,也可以进行修改操作,但是不能有其他操作;如果没有相同记录,可以不做任何操作,也可以做插入操作,同样也 不能有其他操作。
但我实际验证时发现两者之间有一个区别(都是操作同一张表):
MySQL的replace into 1.更新已存在数据 2.删除与新数据匹配不上的数据 3.插入新数据(与原表匹配不上的数据)
oracle的merge into 只能更新与追加