判断字段名是否与数据库中已有字段名相同

  刚开始的时候,考虑的是获取到前台传的字段名,然后遍历数据库中的内容来判断

后来想到,可以将前台的数据传入到一个新的对象中,然后将这个对象在数据库中做对比,用size()来判断

如果size()大于0,那么就存在相同的名字

代码如下:

@RequestMapping("/add") 
@ResponseBody
public JsonData add(@ModelAttribute CustomizeRptModule po) {
JsonData json = new JsonData();
try {
CustomizeRptModule aa=new CustomizeRptModule();
aa.setModulename(po.getModulename());
aa.setDelFlag("0");
List<CustomizeRptModule> list= customizeRptModuleService.selectByModulename(aa);
if(list.size()>0){//重名
json.setSuccess(false);
json.setMsg("添加失败,模块名称相同");
}else{
this.customizeRptModuleService.insert(po);
json.setSuccess(true);
json.setMsg("添加成功");
}
} catch (Exception e) {
e.printStackTrace();
json.setSuccess(false);
json.setMsg("添加失败");
}
return json;
}
posted @ 2019-06-10 18:16  蜗牛的代码之路  阅读(561)  评论(0编辑  收藏  举报