Repository中@Query的错误记录:
错误:
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [SELECT * from t_code t where username=?]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:273)
at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:280)
at java.util.concurrent.CompletableFuture
A
s
y
n
c
S
u
p
p
l
y
.
r
u
n
(
C
o
m
p
l
e
t
a
b
l
e
F
u
t
u
r
e
.
j
a
v
a
:
1592
)
a
t
j
a
v
a
.
u
t
i
l
.
c
o
n
c
u
r
r
e
n
t
.
C
o
m
p
l
e
t
a
b
l
e
F
u
t
u
r
e
AsyncSupply.run(CompletableFuture.java:1592) at java.util.concurrent.CompletableFuture
AsyncSupply.run(CompletableFuture.java:1592)atjava.util.concurrent.CompletableFutureAsyncSupply.exec(CompletableFuture.java:1582)
at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
could not execute query
Column ‘code_id’ not found.
可以直观的看出是 是列 code_id 未找到,因此直接查看Code实体类
@Table(name = "t_code")
public class Code {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@ApiModelProperty(value = "绑定验证表的id")
private Integer codeId;
@ApiModelProperty(value = "可以登陆的游戏的手机号")
private String username;
}
查看数据库字段名:
CREATE TABLE `t_code` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT 'id',
`codeid` int NOT NULL COMMENT '绑定验证表的id',
`username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '可以登陆的游戏的手机号',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=21979 DEFAULT CHARSET=utf8;
可以看出 实体类为大写Id,数据库中为小写的id,因此实体对应查询不到,解决方案:
将数据库的字段名更改为:code_id