进阶之路001 增删改查/数据导入导出之新增功能

 //一次插入多条数据时:

1,      for(User u :list){

           INSERT into user(name,age) values(#name#,#age#)      //此效率极低
            }

2,   INSERT into User(name,age) values 

         <foreach collection="list" item="item" index="index" separator=",">      //此效率高些,批量插入
              (#{item.name},#{item.age})
        </foreach>

Controller层

@SysLog("参数信息做新增")
@RequestMapping("/save")
@RequiresPermissions("xxx:weeklightlimit:save")
public R save(@RequestBody WeekLightLimitEntity weekLightLimit){
try {
int oltpwr=weekLightLimit.getOltpwr();
int ontpwr=weekLightLimit.getOntpwr();
int noLightUp=weekLightLimit.getNoLightUp();
int noLightDown=weekLightLimit.getNoLightDown();
if(oltpwr>ontpwr||noLightUp<noLightDown){
return R.error("提示信息");
}else {
System.out.println("数据输入正确");
};


Integer deptId=weekLightLimit.getDeptId();


System.out.println(deptId);
List<Integer> deptIdList=weekLightLimitService.findAllDeptId();
System.out.println(deptIdList);
for(Integer list:deptIdList) {
if(deptId==list) {
return R.error("提示信息");
}else {
System.out.println(deptId+"并未配置");
};
};


weekLightLimit.setCreateTime(new Date());
weekLightLimit.setUpdateTime(new Date());
weekLightLimitService.save(weekLightLimit);

}catch(Exception e) {
e.printStackTrace();
}

return R.ok();

}

Service接口实现类

@Override
public boolean save(T entity) {
return retBool(baseMapper.insert(entity));
}

 注意:新增与修改是获取当前主键,当主键存在执行修改操作,当主键不存在则执行新增操作;前端的vue里的方法判断执行也与之同理;

posted @ 2020-02-20 20:20  唯恐不及  阅读(324)  评论(0编辑  收藏  举报