Spring Data JPA findAll() 从 MySQL 数据库返回空列表
我是 Spring 的新手。我正在尝试从 MYSQL 数据库获取一些数据。不幸的是 spring data jpa 返回空列表。但是数据库中存在一条记录:如何访问该记录?
libraryRepository.java
public interface LibraryRepository extends JpaRepository<Library, String>,LibraryRespositoryCustom{
}
LibraryController.java
@RestController
public class LibraryController {
@Autowired
LibraryRepository repository;
@Autowired
LibraryService libraryService;
@GetMapping("/getAllBooks")
public ResponseEntity<List<Library>> getAllBooks(){
return ResponseEntity.ok(repository.findAll());
}
application.properties
# Datasource
spring.datasource.url=jdbc:mysql://localhost:3306/APIDevelopSpringBoot?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#com.mysql.jdbc.Driver
spring.jpa.generate-ddl=true
# Jpa/Hibernate :
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
#spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto = update
spring.test.database.replace=none
#Generate Logs
logging.file.name=log/application.log
#spring.profiles.active=dev
spring.main.allow-circular-references: true
Library.java
@Data
@Entity
@Table(name="LibraryDemo")
public class Library {
@Column(name="book_name")
private String book_name;
@Id
@Column(name="id")
private String id;
@Column(name="isbn")
private String isbn;
@Column(name="aisle")
private int aisle;
@Column(name="author")
private String author;
}
I am newbie in Spring.I am trying to get some data from a MYSQL database. Unfortunately spring data jpa returns empty list. But one record exist in db: How can I access this record?
libraryRepository.java
public interface LibraryRepository extends JpaRepository<Library, String>,LibraryRespositoryCustom{
}
LibraryController.java
@RestController
public class LibraryController {
@Autowired
LibraryRepository repository;
@Autowired
LibraryService libraryService;
@GetMapping("/getAllBooks")
public ResponseEntity<List<Library>> getAllBooks(){
return ResponseEntity.ok(repository.findAll());
}
application.properties
# Datasource
spring.datasource.url=jdbc:mysql://localhost:3306/APIDevelopSpringBoot?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#com.mysql.jdbc.Driver
spring.jpa.generate-ddl=true
# Jpa/Hibernate :
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
#spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto = update
spring.test.database.replace=none
#Generate Logs
logging.file.name=log/application.log
#spring.profiles.active=dev
spring.main.allow-circular-references: true
Library.java
@Data
@Entity
@Table(name="LibraryDemo")
public class Library {
@Column(name="book_name")
private String book_name;
@Id
@Column(name="id")
private String id;
@Column(name="isbn")
private String isbn;
@Column(name="aisle")
private int aisle;
@Column(name="author")
private String author;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如我们在此查询中看到的,它正在查询表library_demo,但您有一个名为LibraryDemo 的表。因此,您必须将名称更改为library_demo。
As we can see in this query, it is querying on the table library_demo but you have a table named LibraryDemo. So, you have to change the name to library_demo.
您需要将表名称命名为library_demo 才能正常工作。无论您提供什么名称,它都会被替换为 _ 格式。
试试这个
You need to have the table name as library_demo for that to work. Whatever name you give, it gets replaced with _ format.
Try this