【问题标题】:I can't figure out why my springboot application can not query database我不知道为什么我的 springboot 应用程序无法查询数据库
【发布时间】:2020-11-10 04:39:56
【问题描述】:

我的 Spring Boot 应用程序没有查询数据库。我正在尝试从 mysql 表中检索我的数据并使用 thymeleaf 在 HTML 表中显示数据。我添加了 spring web、mysql 驱动程序、spring data jpa 和 thymeleaf 依赖项。程序运行没有任何错误,但没有在表中给出输出。 下面是我的代码

应用程序属性

spring.datasource.url=jdbc:mysql://localhost:3306/sales?useSSL=false
spring.datasource.username=root
spring.datasource.password=2556b11j

spring.jpa.properties.dialect=org.hibernate.dialect.MYSQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

实体类

package com.chigudu.Entities;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;



@Entity
@Table(name="product")
public class Product {
    
    
    private Long id;
    private String name;
    private String brand;
    private String madein;
    private float price;

    
    // this is a constructor from the main class
    
    public Product() {
        super();
        
    }

//==============================================
    //this are getters and setters
    
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    
    public void setId(Long id) {
        id = id;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public String getBrand() {
        return brand;
    }


    public void setBrand(String brand) {
        this.brand = brand;
    }


    public String getMadein() {
        return madein;
    }


    public void setMadein(String madein) {
        this.madein = madein;
    }


    public float getPrice() {
        return price;
    }


    public void setPrice(float price) {
        this.price = price;
    }
    
    
}

存储库接口

package com.chigudu.ProductRepository;



import org.springframework.data.jpa.repository.JpaRepository;

import com.chigudu.Entities.Product;

public interface ProductRespository extends JpaRepository<Product, Long> {
    

}

服务类

package com.chigudu.ProductServices;
import java.util.List;
import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chigudu.ProductRepository.ProductRespository;
import com.chigudu.Entities.Product;

@Service
public class ProductService {
    
    @Autowired
    private ProductRespository repo;
    
    public List<Product> listAll(){
        return repo.findAll();
    }

    public void save(Product product) {
        repo.save(product);
        
    }
    
    public Product get(Long id) {
        return repo.findById(id).get();
    }
    
    private void delete(Long id) {
        repo.deleteById(id);
    }
}


控制器类

package com.chigudu.AppControllers;


import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.chigudu.Entities.Product;
import com.chigudu.ProductServices.ProductService;

@Controller
public class AppController {

    @Autowired
    private ProductService service;
    
    // view the Application home page
    @RequestMapping("/")
    public String ViewHomePage(Model model) {
        
        List<Product> listProducts=service.listAll();
        model.addAttribute("listProducts", listProducts);
        
        return "index";
    }
    
}

Thymeleaf 页面

<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">

    <title>Home</title>
  </head>
  <body>

    
    <div class="container">
    <h1>Product Manager</h1>
    <table border="1">
        
        <thead>
            <tr>
            <th>Product ID</th>
            <th>Name</th>
            <th>Brand</th>
            <th>Made In</th>
            <th>Price</th>
        </tr>
     </thead>
    
    <tbody>
        <tr th:each="product : ${listProducts}">
            <td th:text="${product.id}">Product ID</td>
            <td th:text="${product.name}">Name</td>
            <td th:text="${product.brand}">Brand</td>
            <td th:text="${product.madein}">Made in</td>
            <td th:text="${product.price}">Price</td>
            <td>
            
            </td>
        </tr>
    
    </tbody>
        
 </table>
        
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
  </body>
</html>

【问题讨论】:

  • 您如何存储数据?我在您的控制器中没有看到 post
  • 我使用mysql工作台存储数据,我只是想使用spring boot从表中读取
  • 编写一个api来检查表中有多少行。通过这种方式,您可以检查您是否正在查询正确的模式。使用 repository.count() 方法

标签: java mysql spring-boot hibernate jpa


【解决方案1】:

您在 Repository 接口上缺少@Repository 注释。在基于注释的配置中没有@Service、@Repository、@Component 或@Controller 注释,将不会考虑一个类进行spring bean 初始化。

【讨论】:

  • thanx,我放了@Repository 注释但问题并没有消失,也许我错过了另一个概念
  • 请启用 spring jpa 日志以查看查询期间发生的情况。
猜你喜欢
  • 1970-01-01
  • 2022-11-20
  • 1970-01-01
  • 1970-01-01
  • 2022-10-25
  • 2016-12-17
  • 1970-01-01
  • 2017-08-13
  • 1970-01-01
相关资源
最近更新 更多