我尝试了几个选项,但每次都会遇到相同的错误 -PreparedStatementCallback; SQL 的未分类 SQLException [SELECT * FROM GardeningPost limit ?抵消 ?]; SQL状态[S0022];错误代码[0];未找到列“user_image_url”。
@Override
public List<GardeningPost> getAllGardeningPosts(int pageSize, int offset) {
return jdbcTemplate.query("SELECT GardeningPost.*, Users.image_url AS user_image_url " +
"FROM GardeningPost " +
"INNER JOIN Users ON GardeningPost.users_gardening_post_id = Users.id " +
"LIMIT ? OFFSET ?", gardeningPostRowMapper, pageSize, offset);
}
因为我只使用jdbc,所以我不修改我的schema.sql,它是:
CREATE TABLE Users
(
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL,
password VARCHAR(255) DEFAULT NULL,
enabled BOOLEAN DEFAULT FALSE,
non_locked BOOLEAN DEFAULT TRUE,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
image_url VARCHAR(255) DEFAULT 'https://cdn-icons-png.flaticon.com/512/149/149071.png',
CONSTRAINT UQ_Users_Email UNIQUE (email)
);
CREATE TABLE GardeningPost (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
date DATETIME DEFAULT CURRENT_TIMESTAMP,
title VARCHAR(60) NOT NULL,
description TEXT,
tag VARCHAR(50),
likes INT,
view_count INT,
img_url VARCHAR(255),
users_gardening_post_id BIGINT UNSIGNED NOT NULL,
FOREIGN KEY (users_gardening_post_id) REFERENCES Users(id)
);
但我直接在类中添加新列 user_image_url :
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(NON_DEFAULT)
public class GardeningPost {
private Long id;
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
private LocalDateTime date;
private String title;
private String description;
private String tag;
private Long likes;
private Long view_count;
private String img_url;
private String user_image_url;
}
我创建该方法的原因是因为我想使用该关系并在控制器响应中提供,该响应将包含在 GardeningPost 中以及 user_image_url 作为用户个人资料图片。
mysql 表格
GardeningPost
不包含名为 user_image_url
作为您的GardeningPost
java 实体类的文件。我认为你需要在 mysql 表中添加一列 user_image_url
。