新增和更新时候库中是否有重复字段的值,有的话,就不插入,没有的话,就执行插入;
Controller中:
//查询--按照name和nameShort--全称和简称
int repayCount = crmCustomerService.queryRepayObject(crmCustomerVO);
if(repayCount >0){
return R.error("系统已存在相同全称与简称的记录,请核查修改!");
}
Service和Dao中:
//查询是否有重复的
public int queryRepayObject(CrmCustomerVO crmCustomerVO);
ServiceImpl中:
//查询是否有重复的
@Override
public int queryRepayObject(CrmCustomerVO crmCustomerVO) {
int count = crmCustomerDao.queryRepayObject(crmCustomerVO);
return count;
}
最后是Mybatis的dao.xml中:
<!-- name和nameShort查询 -->
<select id="queryRepayObject" resultType="int">
select
count(1)
from crm_customer
where (name = #{name} or name_short=#{nameShort})
and id != #{id}
</select>
需要区分新增和更新的区别:
条件id !=#{id} 很重要~
数据库唯一性校验

本文介绍了一种在数据库操作中确保记录唯一性的方法。通过在插入前查询全称和简称是否重复来避免数据冗余。具体实现涉及Controller、Service、DAO层及Mybatis配置。
1723

被折叠的 条评论
为什么被折叠?



