public void insertOrUpdate(WIFIInfo wifiInfo) { log.info("创建wifi账号密码,账号密码信息:"+ JSONObject.toJSONString(wifiInfo)); //查询是否已存在对应的wifi信息 boolean isHave = selectCountById(wifiInfo.getId()); wifiInfo.setUpdateTime(new Date()); //如果已存在wifi数据则修改,不存在则添加 int ins = 0; try { if(isHave){ ins = wifiInfoMapper.updateByPrimaryKeySelective(wifiInfo); if (ins > 0) { log.info("修改wifi账号成功"); } } else { wifiInfo.setCreateTime(new Date()); ins = wifiInfoMapper.insert(wifiInfo); if (ins > 0) { log.info("创建wifi账号成功"); } } } catch (Exception e) { log.error(isHave ? "wifi账号修改失败" : "wifi账号创建失败" + ",报错信息:"+ e.getMessage()); } }简化代码
时间: 2024-03-20 11:45:26 浏览: 56
可以简化为:
```
public void insertOrUpdate(WIFIInfo wifiInfo) {
log.info("创建wifi账号密码,账号密码信息:"+ JSONObject.toJSONString(wifiInfo));
wifiInfo.setUpdateTime(new Date());
boolean isHave = selectCountById(wifiInfo.getId());
int result = 0;
try {
if (isHave) {
result = wifiInfoMapper.updateByPrimaryKeySelective(wifiInfo);
log.info(result > 0 ? "修改wifi账号成功" : "wifi账号修改失败");
} else {
wifiInfo.setCreateTime(new Date());
result = wifiInfoMapper.insert(wifiInfo);
log.info(result > 0 ? "创建wifi账号成功" : "wifi账号创建失败");
}
} catch (Exception e) {
log.error((isHave ? "wifi账号修改失败" : "wifi账号创建失败") + ",报错信息:" + e.getMessage());
}
}
```
去掉了无用的局部变量,将修改和创建的判断改为使用返回值来判断。
阅读全文