I’m a new to java and trying to connect mysql database using springboot. But the data returned from the database.table is empty. Below is my code, where is the problem? There is no error code, which means that there is indeed a connection to the database.table. And my database does have data. I have read several similar questions, but none of them can solve my doubts.
application.yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/library?serverTimezone=GMT%2B8&characterEncoding=utf-8
username: root
password: 1234
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
show-sql: true
properties:
hibernate:
ddl-auto: update
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
format_sql: true
server:
port: 8181
Book.java
package com.example.demo.model;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@Entity
@Table(name = "book")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="isbn")
private String ISBN;
@Column(name="name")
private String Name;
@Column(name="author")
private String Author;
@Column(name="introduction")
private String Introduction;
}
BookRepository.java
package com.example.demo.dao;
import org.springframework.data.repository.CrudRepository;
import com.example.demo.model.Book;
public interface BookRepository extends CrudRepository<Book, Integer> {
}
BookController.java
package com.example.demo.controller;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.model.Book;
import com.example.demo.dao.BookRepository;
import jakarta.annotation.Resource;
@RestController
public class BookController {
@GetMapping("/test")
public String test() {
System.out.println("ok");
return "TEST";
}
@Resource
private BookRepository bookrepository;
@RequestMapping(value = "/allbook", method = RequestMethod.GET)
public List<Book> getAll() {
List<Book> books = (List<Book>) bookrepository.findAll();
System.out.println("data size : "+books.size());
return (List<Book>) bookrepository.findAll();
}
}
On the webpage it looks like this…
http://localhost:8181/allbook
[{},{},{},{}]
And the console on my eclipse is output like this…
data size : 4
Hibernate:
select
b1_0.isbn,
b1_0.author,
b1_0.introduction,
b1_0.name
from
book b1_0
2
Answers
Make fields camelcase format and it works as getter/setters are not matching which are generated by Lombok.
The signature of your repository is wrong. Change this:
like this:
The second type must match the type of the ID field.
Also add a no-args constructor to the entity class. This is needed for Hibernate to map data from the query results.