在 JDBC 连接方法中找不到列

问题描述 投票:0回答:1

我尝试了几种选择,但每次都会遇到相同的错误:

PreparedStatementCallback; SQL 的未分类 SQLException [SELECT

  • 来自园艺邮政限制?抵消 ?]; 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 作为用户个人资料图片。

java sql spring jdbc
1个回答
1
投票

我尝试了你的代码,但没有重现相同的错误。这是我的代码:

@Repository
public class GardeningPostRepository {

    private JdbcTemplate jdbcTemplate;

    @Autowired
    public GardeningPostRepository(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    public void test() {
        RowMapper<GardeningPost> gardeningPostRowMapper = (rs, i) -> {
            GardeningPost post = new GardeningPost();
            post.setUser_image_url(rs.getString("user_image_url"));
            return post;
        };
        List<GardeningPost> postList = jdbcTemplate.query("SELECT GardeningPost.*, Users.image_url AS user_image_url " +
                "FROM GardeningPost " +
                "INNER JOIN Users ON GardeningPost.users_gardening_post_id = Users.id ", gardeningPostRowMapper);
        System.out.println(postList);
    }
}

由于您的错误sql脚本是

SELECT * FROM GardeningPost limit ? offset ?
并且与
getAllGardeningPosts
方法中的不一样,我猜您调用了另一种方法,并使用了错误的rowMapper来尝试从GardeningPost表中不存在的列映射
user_image_url
.

© www.soinside.com 2019 - 2024. All rights reserved.