New name and save the check whether to repeat

For this, most of the projects can be applied the following routines:

  • Ado, directly on the code
/**
	 * 校验参数名称是否重复
	 *
	 * @param dtoParameter 前端传来的实体类
	 */
	public void checkSaveParameters(DtoParameter dtoParameter) {

		// 校验参数名称
		EntityWrapper<tb_business_parameterinfo> wrapper = new EntityWrapper<>();
		wrapper.eq("deleted", false);
		wrapper.eq("name", dtoParameter.getName()); // 组装的条件
		if (!ObjectUtils.isEmpty(dtoParameter.getId())) {
			wrapper.ne("id",dtoParameter.getId());
		}
		tb_business_parameterinfo parameterinfo = super.selectOne(wrapper);
		if (!ObjectUtils.isEmpty(parameterinfo)) {
			throw new RuntimeException("该名称已存在,请重新填写!!");
		}
	}
This code means:
  • If you are editing, it is judged that name equal to the tip pass over the name, and id id not equal to the data in this section are not in the database, if it proves that already exists, if not, you can edit.
  • If you are new, not only assembled id judge other conditions, if it is proved that already exists, if not, you can edit.

Note: Why do not need to assemble a new id, id so as not to give him a new exclusion is that you do not give her to write on if (!ObjectUtils.isEmpty(dtoParameter.getId())) {}this judgment, he will assign id to null, so check it out standard, so remember, be sure to this id to separate, to judge new or edited.

Published 46 original articles · won praise 41 · views 2380

Guess you like

Origin blog.csdn.net/qq_17589751/article/details/104768158