合肥网站网页设计,长春盛网网站建设,wordpress th7好卡,wordpress退货插件问题
这个报错是出现在Java Spring boot项目中#xff0c;使用MyBatis-Plus通过创建的实体类对数据库的操作过程中#xff0c;通过实体创建数据库表是没有问题的#xff0c;而在接口调用服务类操作数据库的时候#xff0c;会出现报错。报错详情如下#xff1a; 服务请求异…问题
这个报错是出现在Java Spring boot项目中使用MyBatis-Plus通过创建的实体类对数据库的操作过程中通过实体创建数据库表是没有问题的而在接口调用服务类操作数据库的时候会出现报错。报错详情如下 服务请求异常:org.springframework.jdbc.BadSqlGrammarException: com..server.mapper.UsageMapper.insert (batch index #1) failed. Cause: java.sql.BatchUpdateException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near usage,…… create_time, update_time) VALUES (1854720160046714882, 18547201’ at line 1; bad SQL grammar []; nested exception is java.sql.BatchUpdateException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near usage, …… , create_time, update_time) VALUES (1854720160046714882, 18547201’ at line 1. 其中的实体类如下
Data
TableName(autoResultMap true)
TableComment(用量信息)
ApiModel(value 用量信息)
public class GoodsUsage {ApiModelProperty(value id)Column(comment id)private Long id;ApiModelProperty(value 用量)Column(comment 用量)private BigDecimal usage;ApiModelProperty(创建时间)Column(comment 创建时间)private LocalDateTime createTime;ApiModelProperty(更新时间)Column(comment 更新时间)private LocalDateTime updateTime;
}报错的调用处理是批量保存数据记录
this.saveBatch(usages);解决方法
看到报错问题以为是关键字引起数据库操作问题后来发现MyBatis-Plus已经根据实体创建出了数据库表
后续没有照着这个方向调试而是以为MyBatis-Plus 的LambdaQueryWrapper 表达式所生成的SQL语句有问题排查了很久也没有发现问题不过将控制台选择复制打印的params的sql语句复制过去存在同样的报错最后发现是usage字段名和MySQL的Usage权限重名了也就是关键字冲突将usage字段重命名为其他可用的名称即可修复。
以下为其他解决参考 注意尽量避免使用关键字作为表名或者字段名如果一定要用关键字作为字段名在SQL处理时用单引号将名称括起来(‘usage’); MyBatis-Plus 的关键字处理,可以使用 TableName 和 TableField 注解来处理关键字这样 MyBatis-Plus 在构建 SQL 时会使用单引号‘’来包围列名从而避免了关键字冲突。 可以使用如下代码获取所有保留关键字
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.SqlReservedWords;String keywords String.join(StringPool.COMMA, SqlReservedWords.KEYWORDS);
System.out.println(保留关键字: keywords);