这个问题,前面写过一篇文章:MySql -- 不存在则插入,存在则更新或忽略_知难行难1985的博客-CSDN博客_mysql 如果不存在则插入
不过那篇文章依赖的是比如Primary Key或Unique Key来确定是否重复,如果没有Unique Key来确定是否重复呢?本文就来探讨一下。
1.插入单条记录
需要满足下面几个需求:
- 插入单条记录,依据某个或者某几个字段判断(非Unique Key),如果数据库中不存在,则插入;如果数据库中已经存在,则什么也不做(目前还没有研究更新,难度应该不大,本项目中暂时用不到)
- 如果发生了插入操作。返回Id,赋值给被存储对象,因为业务中要用到。
- 返回 affects rows。如果插入返回1,不插入返回0.
mybatis代码:
mapper.xml:
<!-- 单条插入:以多个字段为标准,如果存在,不做任何操作,如果不存在,则插入,亲测好用 -->
<insert id="insertCheckDuplicateKeySingle" keyProperty="id" useGeneratedKeys="true">
INSERT INTO e**_****_customer (customer_id, issuer_id, issuer_*****_no, province_*******_no, mobile, email,
customer_type, customer_name, customer_mobile, customer_email, mail_address, credential_type,
credential_no, agent_name, agent_mobile, product_code, business_code, bill_type, sign_bank, `status`,
status_update_time, del_flag, create_by, create_time, update_by, update_time, remark)
SELECT #{customerId}, #{issuerId}, #{issuerCustomerNo},#{provinceCustomerNo},
#{mobile}, #{email},#{customerType}, #{customerName},#{customerMobile}, #{customerEmail},
#{mailAddress}, #{credentialType}, #{credentialNo}, #{agentName}, #{agentMobile}, #{productCode}, #{businessCode},
#{billType}, #{signBank}, #{status}, #{statusUpdateTime}, #{delFlag}, #{createBy},
#{createTime},#{updateBy}, #{updateTime}, #{remark} FROM DUAL
where not exists (select issuer_customer_no, del_flag, issuer_id from etc_issuer_customer where issuer_customer_no = #{issuerCustomerNo}
and del_flag = 0 and issuer_id = #{issuerId})
</insert>
Mapper.java
@Repository
public interface E*********Mapper extends BaseMapper<E*******Customer> {
public int insertCheckDuplicateKeySingle(E********Customer e******Customer);
}
这样在插入单条数据的时候,如果数据库中不存在,则插入数据,返回Id存储于被保存对象中;同时返回affected rows作为返回值,如果返回值是1,则表示插入成功,如果返回值是0,则表示数据库中已经有该条记录,不做任何操作,然后我们可以从数据库中将这条记录查询出来,根据业务需要做更新之类的操作。
e****Customer.setStatusUpdateTime(date);
e****Customer.setDelFlag(0);
//这个怎么设置,问问张军,这个不是前端调用修改
e****Customer.setCreateBy("system");
e****Customer.setCreateTime(new Date());
e****Customer.setUpdateBy("system");
e****Customer.setUpdateTime(new Date());
e****Customer.setRemark("");
int resultIssuer = e****Mapper.insertCheckDuplicateKeySingle(e****Customer);
2.批量插入
需要满足下面几个需求:
- 批量插入数据,依据某个或者某几个字段判断(非Unique Key),如果数据库中不存在,则插入;如果数据库中已经存在,则什么也不做(目前还没有研究更新,难度应该不大,本项目中暂时用不到)
- 部分插入,部分因为重复问题不做任何操作。每个插入操作能够返回Id,赋值给被存储对象,业务中要用到。
- 返回 affects rows。插入多少条数据,就返回多少。
mybatis代码:
mapper.xml:
<insert id="insertBatchDuplicate" keyProperty="id" useGeneratedKeys="true" parameterType="java.util.List">
INSERT INTO e******_customer (id,c*****_id, issuer_id, issuer_******_no, province_******_no, mobile, email,
c****_type, c****_name, c****_mobile, customer_email, mail_address, credential_type,
credential_no, agent_name, agent_mobile, product_code, business_code, bill_type, sign_bank, `status`,
status_update_time, del_flag, create_by, create_time, update_by, update_time, remark)
select * from (
<foreach collection="list" item="obj" separator=" UNION ALL " >
SELECT #{obj.id} id,#{obj.customerId} customer_id, #{obj.issuerId} issuer_id, #{obj.issuerCustomerNo} issuer_customer_no,#{obj.provinceCustomerNo} province_customer_no,
#{obj.mobile} mobile, #{obj.email} email,#{obj.customerType} customer_type, #{obj.customerName} customer_name,#{obj.customerMobile} customer_mobile, #{obj.customerEmail} customer_email,
#{obj.mailAddress} mail_address, #{obj.credentialType} credential_type, #{obj.credentialNo} credential_no, #{obj.agentName} agent_name, #{obj.agentMobile} agent_mobile, #{obj.productCode} product_code, #{obj.businessCode} business_code,
#{obj.billType} bill_type, #{obj.signBank} sign_bank, #{obj.status} status, #{obj.statusUpdateTime} status_update_time, #{obj.delFlag} del_flag, #{obj.createBy} create_by,
#{obj.createTime} create_time,#{obj.updateBy} update_by, #{obj.updateTime} update_time, #{obj.remark} remark FROM DUAL
where not exists (select issuer_customer_no,del_flag,issuer_id from etc_issuer_customer where issuer_customer_no = #{obj.issuerCustomerNo}
and del_flag = 0 and issuer_id = #{obj.issuerId})
</foreach>
) b
</insert>
Mapper.java
@Repository
public interface E*******Mapper extends BaseMapper<E******Customer> {
public int insertBatchDuplicate(@Param("list") List<E****Customer> e*****CustomerList);
}
批量插入数据,部分插入,部分因为重复不做任何操作,插入成功的会返回Id,赋值给被插入对象。返回值是插入对象的数量(affected rows)。然后可以遍历对象 list,查看谁的id是null,这说明这个对象已经在数据库中有了,可以根据业务需要,对其进行更新操作。
int affectedNum1 = e****Mapper.insertBatchDuplicate(i*******List);
if (affectedNum1 >= 10){
..................................................................
}