作者:Eddy 历史版本:1 最后编辑:龚清 更新时间:2024-11-20 15:41
如何使用框架提供的唯一校验功能?
分为三种场景:
- 多字段联合唯一校验;
- 单字段个性化唯一校验;
- 多字段唯一校验;
多字段联合唯一校验
以PartyLevel.java为例
- 创建唯一校验器
DefaultColumn typeColumn = new DefaultColumn("type", "type_", "参与者类型");// 唯一字段
DefaultColumn levelColumn = new DefaultColumn("level", "level_", "等级值");// 唯一字段
UniquePropertyValidation validation // 唯一校验器
= new UniquePropertyValidation(PartyLevel.class, typeColumn, levelColumn);
- 设置唯一校验器
getDao().getUniquePropertyValidator().setValidation(validation);
- 设置仓库对象
getDao().getUniquePropertyValidator().setRepository(partyLevelRepository);
- 完整代码
@Override
protected void init() {
DefaultColumn typeColumn = new DefaultColumn("type", "type_", "参与者类型");
DefaultColumn levelColumn = new DefaultColumn("level", "level_", "等级值");
UniquePropertyValidation validation
= new UniquePropertyValidation(PartyLevel.class, typeColumn, levelColumn);
getDao().getUniquePropertyValidator().setValidation(validation);
getDao().getUniquePropertyValidator().setRepository(partyLevelRepository);
}
单字段个性化唯一校验;
以BoDef.java为例
- 创建唯一校验器
DefaultColumn codeColumn = new DefaultColumn("code", "code_", "编码");
UniquePropertyValidation validation
= new UniquePropertyValidation(BoDef.class, codeColumn);
- 设置唯一校验器
getDao().getUniquePropertyValidator().setValidation(validation);
- 设置仓库对象
getDao().getUniquePropertyValidator().setRepository(boDefRepository);
- 创建新增数据唯一校验函数
// 函数式校验数据库是否存在
Function<Object, Boolean> existInDBExecution = (po) -> {
if(BeanUtils.isEmpty(po)) {
return false;
}
BoDefPo boDefPo = (BoDefPo) po;
boDefRepository.setSkipCache("0");// 禁用缓存,否则出现数据冲突
boolean succ = boDefRepository.isExists(boDefPo);
boDefRepository.setSkipCache(null);
return succ;
};
((UniquePropertyValidator)(getDao().getUniquePropertyValidator())).setExistInDBExecutionForInsert(existInDBExecution);
- 创建更新数据唯一校验函数
// 函数式校验数据库是否存在
Function<Object, Boolean> existInDBExecution = (po) -> {
if(BeanUtils.isEmpty(po)) {
return false;
}
boDefRepository.setSkipCache("0");// 禁用缓存,否则出现数据冲突
boolean succ = boDefRepository.isExists(boDefPo);
boDefRepository.setSkipCache(null);
return succ;
};
((UniquePropertyValidator)(getDao().getUniquePropertyValidator())).setExistInDBExecutionForUpdate(existInDBExecution);
- 完整代码
@Override
protected void init() {
DefaultColumn codeColumn = new DefaultColumn("code", "code_", "编码");
UniquePropertyValidation validation
= new UniquePropertyValidation(BoDef.class, codeColumn);
getDao().getUniquePropertyValidator().setValidation(validation);
getDao().getUniquePropertyValidator().setRepository(boDefRepository);
// 函数式校验数据库是否存在
Function<Object, Boolean> existInDBExecution = (po) -> {
if(BeanUtils.isEmpty(po)) {
return false;
}
BoDefPo boDefPo = (BoDefPo) po;
boDefRepository.setSkipCache("0");// 禁用缓存,否则出现数据冲突
boolean succ = boDefRepository.isExists(boDefPo);
boDefRepository.setSkipCache(null);
return succ;
};
((UniquePropertyValidator)(getDao().getUniquePropertyValidator())).setExistInDBExecutionForInsert(existInDBExecution);
((UniquePropertyValidator)(getDao().getUniquePropertyValidator())).setExistInDBExecutionForUpdate(existInDBExecution);
}
多字段唯一校验;
以PartyEmployee.java为例
- 创建唯一校验器
DefaultColumn accountColumn = new DefaultColumn("account", "ACCOUNT_", "账号");
DefaultColumn wechatColumn = new DefaultColumn("wcAccount", "WC_ACCOUNT_", "微信号");
DefaultColumn emailColumn = new DefaultColumn("email", "EMAIL_", "邮箱");
DefaultColumn mobileColumn = new DefaultColumn("mobile", "MOBILE_", "手机号码");
UniquePropertyValidation validation
= new UniquePropertyValidation(PartyLevel.class, accountColumn, wechatColumn, emailColumn, mobileColumn);
- 设置唯一校验器
getDao().getUniquePropertyValidator().setValidation(validation);
- 设置仓库对象
getDao().getUniquePropertyValidator().setRepository(partyEmployeeRepository);
- 设置多字段数据唯一校验属性
((UniquePropertyValidator)(getDao().getUniquePropertyValidator())).setSingleUniqueValue(true);
- 完整代码
@Override
protected void init() {
DefaultColumn accountColumn = new DefaultColumn("account", "ACCOUNT_", "账号");
DefaultColumn wechatColumn = new DefaultColumn("wcAccount", "WC_ACCOUNT_", "微信号");
DefaultColumn emailColumn = new DefaultColumn("email", "EMAIL_", "邮箱");
DefaultColumn mobileColumn = new DefaultColumn("mobile", "MOBILE_", "手机号码");
UniquePropertyValidation validation
= new UniquePropertyValidation(PartyLevel.class, accountColumn, wechatColumn, emailColumn, mobileColumn);
getDao().getUniquePropertyValidator().setValidation(validation);
getDao().getUniquePropertyValidator().setRepository(partyEmployeeRepository);
((UniquePropertyValidator)(getDao().getUniquePropertyValidator())).setSingleUniqueValue(true);
}
函数入参Object,返回值Boolean;
函数用法请自行查阅JDK1.8新特性Function编程;