当前位置:Gxlcms > 数据库问题 > 判断字段名是否与数据库中已有字段名相同

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

时间:2021-07-01 10:21:17 帮助过:17人阅读

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

后来想到,可以将前台的数据传入到一个新的对象中,然后将这个对象在数据库中做对比,用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;
}

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

标签:new   int   lse   大于   tac   exception   对比   flag   catch   

人气教程排行