前端校验(SpringBoot 通用方案:创建 - 更新时校验名称重复)

前端校验(SpringBoot 通用方案:创建 - 更新时校验名称重复)
SpringBoot 通用方案:创建 / 更新时校验名称重复

项目使用的标准代码,包含:

  • 通用重复校验逻辑
  • create 时:直接校验是否存在
  • update 时:排除自身 ID 再校验(避免自己跟自己重复)
  • 事务、异常、Service 规范写法
  • 真正企业级可用

一、核心思路

  1. create:检查 name 是否存在 → 存在则抛异常
  2. update:检查 name 是否存在 且 id != 当前 id → 存在则抛异常
  3. 抽取公共校验方法,避免代码重复

二、直接可用代码(以字典类型为例:DictType)

1. Repository(增加查询方法)

java

运行

public interface DictTypeRepository extends JpaRepository {    // 校验名称是否存在(create用)    boolean existsByDictName(String dictName);    // 校验名称是否存在,排除当前ID(update用)    boolean existsByDictNameAndIdNot(String dictName, Long id);}

三、Service 实现(核心代码)

java

运行

@Servicepublic class DictTypeServiceImpl implements DictTypeService {    private final DictTypeRepository dictTypeRepository;    // 构造注入    public DictTypeServiceImpl(DictTypeRepository dictTypeRepository) {        this.dictTypeRepository = dictTypeRepository;    }    // ======================    // 1. 创建 - 校验重复    // ======================    @Override    @Transactional(rollbackFor = Exception.class)    public Long createDictType(DictType dictType) {        // 校验名称是否重复        checkNameExists(dictType.getDictName());                // 保存        return dictTypeRepository.save(dictType).getId();    }    // ======================    // 2. 更新 - 校验重复(排除自身)    // ======================    @Override    @Transactional(rollbackFor = Exception.class)    public void updateDictType(DictType dictType) {        Long id = dictType.getId();        if (id == null) {            throw new RuntimeException("更新时ID不能为空");        }        // 校验名称重复(排除自己)        checkNameExists(dictType.getDictName(), id);        dictTypeRepository.save(dictType);    }    // ======================    // 公共方法:创建时校验    // ======================    private void checkNameExists(String dictName) {        if (dictTypeRepository.existsByDictName(dictName)) {            throw new RuntimeException("名称【" + dictName + "】已存在");        }    }    // ======================    // 公共方法:更新时校验    // ======================    private void checkNameExists(String dictName, Long excludeId) {        if (dictTypeRepository.existsByDictNameAndIdNot(dictName, excludeId)) {            throw new RuntimeException("名称【" + dictName + "】已存在");        }    }}

四、超简洁写法(合并成一个通用方法)

如果你想更优雅,只保留一个方法:

java

前端校验(SpringBoot 通用方案:创建 - 更新时校验名称重复)

运行

/** * 校验名称是否重复(通用 create/update) * @param dictName  名称 * @param excludeId 排除ID(null=创建,有值=更新) */private void checkNameUnique(String dictName, Long excludeId) {    boolean exists;    if (excludeId == null) {        exists = dictTypeRepository.existsByDictName(dictName);    } else {        exists = dictTypeRepository.existsByDictNameAndIdNot(dictName, excludeId);    }    if (exists) {        throw new RuntimeException("名称【" + dictName + "】已存在");    }}

使用:

java

运行

// 创建checkNameUnique(dictName, null);// 更新checkNameUnique(dictName, id);

五、Controller 示例

java

运行

@RestController@RequestMapping("/dict/type")public class DictTypeController {    private final DictTypeService dictTypeService;    public DictTypeController(DictTypeService dictTypeService) {        this.dictTypeService = dictTypeService;    }    @PostMapping("/create")    public R create(@RequestBody DictType dictType) {        Long id = dictTypeService.createDictType(dictType);        return R.success(id);    }    @PostMapping("/update")    public R update(@RequestBody DictType dictType) {        dictTypeService.updateDictType(dictType);        return R.success();    }}

六、规则总结

  • create:existsByXxxName(name)
  • update:existsByXxxNameAndIdNot(name, id)
  • 校验失败抛异常 → 全局异常捕获返回前端
  • 公共方法抽取,不写重复代码

文章版权声明:除非注明,否则均为边学边练网络文章,版权归原作者所有

相关阅读

最新文章

热门文章

本栏目文章