1. 需求描述
在我们日常开发中,经常会有些数据是唯一的,比如人员的信息、车辆基本信息等等,我们在数据库中只存一条数据,我们在批量导入数据的时候,有重复数据是很难受的事情,我们用MERGE INTO 就可以解决这件事情
2. MERGE INTO 语句简介
使用 MERGE INTO 语法可合并 UPDATE 和 INSERT 语句。通过 MERGE 语句,根据一张表(或视图)的连接条件对另外一张表(或视图)进行查询,连接条件匹配上的进行 UPDATE(可能含有 DELETE),无法匹配的执行 INSERT。其中,数据表包括:普通表、分区表、加密表、压缩表和堆表。
语法格式
MERGE INTO <merge_into_obj> [<表别名>] USING <表引用> ON (<条件判断表达式>)
<[<merge_update_clause>] [<merge_insert_clause>]>
<merge_into_obj> ::= <单表引用> | <子查询>
<单表引用> ::= [<模式名>.]<基表或视图名>
<子查询> ::= (<查询表达式>)
<merge_update_clause>::=WHEN MATCHED THEN UPDATE SET <set_value_list> <where_clause_null> [DELETE <where_clause_null>]
<merge_insert_clause>::=WHEN NOT MATCHED THEN INSERT [<full_column_list>] VALUES <ins_value_list> <where_clause_null>;
<表引用>::=<普通表> | <连接表> 详见《DM8_SQL语言使用手册》第四章 数据查询语句
<set_value_list> ::= <列名>=<值表达式| DEFAULT> {,<列名>=<值表达式| DEFAULT>}
<where_clause_null> ::= [WHERE <条件表达式>]
<full_column_list>::= (<列名>{,<列名>})
<ins_value_list>::= (<插入值>{,<插入值>})
3. 具体SQL语句
*Mapper.java
void mergeSaveCarInfoList(@Param("list") List<TbCarInfo> list);
*Mapper.xml
<insert id="mergeSaveCarInfoList" parameterType="com.shxp.project.target.domain.po.TbCarInfo">
MERGE INTO tb_car_info t
USING
(
SELECT
car_no,car_type,car_color,car_normal,car_company,created_by,created_date,owner,id_card,home,tel,remark,update_by,update_date,created_by_id,update_by_id,flag,car_type_name,car_color_name
FROM (
<foreach collection="list" item="item" separator="union all">
SELECT #{item.carNo,jdbcType=VARCHAR} as car_no,
#{item.carType,jdbcType=VARCHAR} as car_type,
#{item.carColor,jdbcType=VARCHAR} as car_color,
#{item.carNormal,jdbcType=VARCHAR} as car_normal,
#{item.carCompany,jdbcType=VARCHAR} as car_company,
#{item.createdBy,jdbcType=VARCHAR} as created_by,
#{item.createdDate,jdbcType=VARCHAR} as created_date,
#{item.owner,jdbcType=VARCHAR} as owner,
#{item.idCard,jdbcType=VARCHAR} as id_card,
#{item.home,jdbcType=VARCHAR} as home,
#{item.tel,jdbcType=VARCHAR} as tel,
#{item.remark,jdbcType=VARCHAR} as remark,
#{item.updateBy,jdbcType=VARCHAR} as update_by,
#{item.updateDate,jdbcType=VARCHAR} as update_date,
#{item.createdById,jdbcType=VARCHAR} as created_by_id,
#{item.updateById,jdbcType=VARCHAR} as update_by_id,
#{item.flag,jdbcType=VARCHAR} as flag,
#{item.carTypeName,jdbcType=VARCHAR} as car_type_name,
#{item.carColorName,jdbcType=VARCHAR} as car_color_name
FROM dual
</foreach>
) tmp
) a
ON (a.car_no = t.car_no)
WHEN matched THEN
UPDATE SET
t.car_type = a.car_type,
t.car_color =a.car_color,
t.car_normal= a.car_normal,
t.car_company = a.car_company,
t.created_by = a.created_by,
t.created_date = a.created_date,
t.owner = a.owner,
t.id_card =a.id_card,
t.home =a.home,
t.tel = a.tel,
t.remark = a.remark,
t.update_by = a.update_by,
t.update_date = a.update_date,
t.created_by_id = a.created_by_id,
t.update_by_id = a.update_by_id,
t.flag =a.flag,
t.car_type_name =a.car_type_name,
t.car_color_name =a.car_color_name,
t.is_delete ='0'
WHEN NOT matched THEN
INSERT
(car_no,car_type,car_color,car_normal,car_company,created_by,created_date,owner,id_card,home,tel,remark,update_by,update_date,created_by_id,update_by_id,flag,car_type_name,car_color_name)
VALUES
(a.car_no,a.car_type,a.car_color,a.car_normal,a.car_company,a.created_by,a.created_date,a.owner,a.id_card,a.home,a.tel,a.remark,a.update_by,a.update_date,a.created_by_id,a.update_by_id,a.flag,a.car_type_name,a.car_color_name)
</insert>
4. 应用场景
xlsx数据导入
java 代码可以参考:easyexcel 导入_菜鸟进军大神陆的博客-CSDN博客_引入easyexcel