Spring Data JPA findAll() 从 MySQL 数据库返回空列表

发布于 2025-01-10 05:37:15 字数 2129 浏览 0 评论 0原文

我是 Spring 的新手。我正在尝试从 MYSQL 数据库获取一些数据。不幸的是 spring data jpa 返回空列表。但是数据库中存在一条记录:如何访问该记录?

Mysqlmysql2

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?

Mysql
mysql2

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜是你 2025-01-17 05:37:15
select library0_.id as id1_0_, library0_.aisle as aisle2_0_, library0_.author as author3_0_, library0_.book_name as book_nam4_0_, library0_.isbn as isbn5_0_ from library_demo library0_

正如我们在此查询中看到的,它正在查询表library_demo,但您有一个名为LibraryDemo 的表。因此,您必须将名称更改为library_demo。

select library0_.id as id1_0_, library0_.aisle as aisle2_0_, library0_.author as author3_0_, library0_.book_name as book_nam4_0_, library0_.isbn as isbn5_0_ from library_demo library0_

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.

伤痕我心 2025-01-17 05:37:15

您需要将表名称命名为library_demo 才能正常工作。无论您提供什么名称,它都会被替换为 _ 格式。

试试这个

@Entity
@Table(name = "library_demo")
public class Library {...}

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

@Entity
@Table(name = "library_demo")
public class Library {...}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文