判斷不重複

Dao層:

public Long judgeExist(String year,String name,Long id){

DetachedCriteriaBuilder dcb = DetachedCriteriaBuilder.instance(Holidy.class);
dcb.addEq("year", year).addEq("name", name).addEq("is_delete", false);
return this.count(dcb);

}


Service層:

Boolean judgeAvailable(String year , String name ,Long id);


@Override
@Transactional
public Boolean judgeAvailable(String year, String name, Long id) {
Long num=this.holidyDAO.judgeExist(year, name, id);
if(num > 0){
return false;
}
return true;
}


Controller層:

@RequestMapping("/create")
@ResponseBody
@RequireLogin
public ResultResponse saveHolidy(HttpServletRequest request, Holidy holidy ,Model model){
ResultResponse rr = new ResultResponse(false);


try{
Boolean ava=this.holidyService.judgeAvailable(holidy.getYear(),holidy.getName() , holidy.getId());

if(!ava){
rr.setMessage("Holiday is exist!");
return rr;
}

Long userId=getLoginUser().to(UserInfo.class).getId();
holidy.setCreateBy(userId);
holidy.setCreateDate(getDate());
this.holidyService.save(holidy);
rr.setResult(true);

  }catch(Exception e){
   
rr.setMessage("Backend error");
ERROR.error(this.getClass().getSimpleName(),e);

  }

return rr;
}






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章