Springboot+MySQL表返回为空。

huangapple go评论65阅读模式
英文:

Springboot+MySQL table return empty

问题

以下是翻译好的部分:

"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();
    }
}

网页上显示的内容:

http://localhost:8181/allbook
[{},{},{},{}]

Eclipse 控制台输出的内容:

data size : 4
Hibernate: 
    select
        b1_0.isbn,
        b1_0.author,
        b1_0.introduction,
        b1_0.name 
    from
        book b1_0

希望这些信息对你有帮助。如果你有其他问题或需要进一步的帮助,请随时提出。

英文:

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&amp;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 = &quot;book&quot;)
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name=&quot;isbn&quot;)
    private String ISBN;
    
    @Column(name=&quot;name&quot;)
    private String Name;
    
    @Column(name=&quot;author&quot;)
    private String Author;
    
    @Column(name=&quot;introduction&quot;)
    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&lt;Book, Integer&gt; {

}

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(&quot;/test&quot;)
	public String test() {
		System.out.println(&quot;ok&quot;);
		return &quot;TEST&quot;;
	}
	
    @Resource
    private BookRepository bookrepository;
	
	@RequestMapping(value = &quot;/allbook&quot;, method = RequestMethod.GET)
	public List&lt;Book&gt; getAll() {
		
		List&lt;Book&gt; books = (List&lt;Book&gt;) bookrepository.findAll();
		System.out.println(&quot;data size : &quot;+books.size());
		
		return (List&lt;Book&gt;) 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

答案1

得分: 0

将字段转换为驼峰命名格式,并使其能够正常工作,因为由Lombok生成的getter/setter与它们不匹配。

private String isbn;
private String name;
private String author;
private String introduction;
英文:

Make fields camelcase format and it works as getter/setters are not matching which are generated by Lombok.

private String isbn;
private String name:
private String author;
private String introduction;

答案2

得分: 0

仓库的签名不正确。将这个:

CrudRepository<Book, Integer>

改成这样:

CrudRepository<Book, String>

第二个类型必须与ID字段的类型匹配。

还要在实体类中添加一个无参数构造函数。这对于Hibernate来自查询结果映射数据是必要的。

英文:

The signature of your repository is wrong. Change this:

CrudRepository&lt;Book, Integer&gt;

like this:

CrudRepository&lt;Book, String&gt;

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.

huangapple
  • 本文由 发表于 2023年6月5日 23:41:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定