如何解决我是 spring 的新手,在通过 spring-data-jpa 连接 springboot 和 mysql-db 时遇到这个问题
我在邮递员中到达端点时收到此错误
2021-07-08 16:21:36.244 WARN 11140 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.sqlExceptionHelper:sql 错误:1054,sqlState:42S22 2021-07-08 16:21:36.244 错误 11140 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.sqlExceptionHelper:“字段列表”中的未知列“products0_.product_id” 2021-07-08 16:21:36.258 INFO 11140 --- [nio-8080-exec-1] o.h.e.internal.DefaultLoadEventListener :HHH000327:执行加载命令时出错
org.hibernate.exception.sqlGrammarException: 无法提取结果集
Caused by: java.sql.sqlSyntaxErrorException: UnkNown column 'products0_.product_id' in 'field list'
java.sql.sqlSyntaxErrorException:“字段列表”中的未知列“products0_.product_id”
实体
@Entity
public class Products {
@Id
private String productId;
private String productName;
private double price;
private String ownerId;
private String imageURL;
private String inStock;
}
//with getter setter and constructors
ProductsServiceImpl.java
package com.akilan.juzTradeapi.service;
import com.akilan.juzTradeapi.entity.Products;
import com.akilan.juzTradeapi.repo.ProductsRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ProductsServiceImpl implements ProductsService {
@Autowired
private ProductsRepo productsRepo;
@Override
public List<Products> findAllProducts() {
return productsRepo.findAll();
}
@Override
public void createDepartment(Products products) {
productsRepo.save(products);
}
}
产品仓库
package com.akilan.juzTradeapi.repo;
import com.akilan.juzTradeapi.entity.Products;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ProductsRepo extends JpaRepository<Products,String> {
}
application.properties
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driver-class-name=com.MysqL.cj.jdbc.Driver
spring.jpa.properties.hibernate.format_sql = true
解决方法
感谢您的帮助, 问题是我错过了自动创建表所需的这个属性。
spring.jpa.hibernate.ddl-auto=update
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/tab
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。