补作业:伙伴匹配系统,解决某个sql语句错误的方法
后端报错是这样的:

就是User里除了id,其他全是null,会出现上述错误。
并且前端控制台也会出现:
{code: 50000, date: null, message: "\r\n### Error updating database. Cause: java.sql.SQ…8 AND id_detele=0' at line 1\n; bad SQL grammar []", description: ''} ,按鱼总的话说,不安全!
我的解决方法是这样的:
在User类里添加一个
public boolean allFieldsAreNull() throws IllegalAccessException {
for (Field field : this.getClass().getDeclaredFields()) {
field.setAccessible(true);
if (!field.getName().equals("id") && !field.getName().equals("serialVersionUID")) {
if (field.get(this) != null) {
return false;
}
}
}
return true;
}
这里还得优化,要是不为null不只是id和serialVersionUID,那这句if 语句可写得老长!
然后再controller层对应的方法里写:
if (user.allFieldsAreNull()) {
throw new BusinessException(PARAMS_ERROR);
}
抛出封装好的异常就好了!
评论
问答助学
相关内容
0个评论
全部评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
作者分享
有没有做完 rpc项目 第二章 全局加载配置 扩展 的鱼友,教教我怎么实现监听配置文件的变更和自动更新呗😀
这是我的代码:
public class ConfigFileWatcher {
private Props props;
public ConfigFileWatcher(String configFileName) throws IOException, URISyntaxException {
//加载配置文件
this.props = new Props(configFileName);
//启动配置文件监听
startConfigFileWatcher(props, configFileName);
}
private static void startConfigFileWatcher(Props props, String filePath) throws URISyntaxException {
//todo 获取资源文件的URL 要适配linux
URL resourceUrl = ConfigFileWatcher.class.getResource("/"+filePath);
if (resourceUrl == null) {
return;
}
// 将URL转换为Path
Path path = null;
try {
path = Paths.get(resourceUrl.toURI()).getParent();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
if (path == null) {
System.err.println("无法获取资源文件的父目录: " + resourceUrl);
return;
}
// 输出父目录
System.out.println("资源文件的父目录: " + path);
// 创建一个简单的监听器
Watcher watcher = new SimpleWatcher() {
@Override
public void onModify(WatchEvent<?> event, Path currentPath) {
if (currentPath.endsWith(filePath)) {
Console.log("配置文件已修改,重新加载...");
// 重新加载配置文件
props.autoLoad(true);
}
}
};
try{
// 创建并启动监控器
WatchMonitor watchMonitor = WatchMonitor.create(path);
watchMonitor.setWatcher(watcher);
watchMonitor.start();
}catch (Exception e) {
Console.error(e, "无法启动配置文件监听器: {}", path);
}
}
}
在ConfigUtils里
//加载文件
ConfigFileWatcher watcher = new ConfigFileWatcher(application.toString());
return watcher.getProps().toBean(clazz,Prefix);
应该是我理解不够,还是没做出来😜,求各路大佬指点和分享
3
我已通关码神挑战,一起来玩呀~https://1024.codefather.cn/
6
1
oj判题系统-vue中async和await是否失效问题
4
求,生产者能动态的获取序列化器选择配置
3
有鱼友了解对日开发吗,找的java实习岗!在沈阳东软,实习工资2000包住宿,今天得到邮件实习通知,目前就这一个实习,还在犹豫!
12
