Java 我能';我不明白为什么我的springboot应用程序不能查询数据库

Java 我能';我不明白为什么我的springboot应用程序不能查询数据库,java,mysql,spring-boot,hibernate,jpa,Java,Mysql,Spring Boot,Hibernate,Jpa,我的Spring启动应用程序没有查询数据库。我试图从mysql表中检索数据,并使用thymeleaf在HTML表中显示数据。我添加了SpringWeb、mysql驱动程序、SpringDataJPA和thymeleaf依赖项。程序运行时没有任何错误,但没有在表中给出输出。 下面是我的代码 应用程序属性 spring.datasource.url=jdbc:mysql://localhost:3306/sales?useSSL=false spring.datasource.username=ro

我的Spring启动应用程序没有查询数据库。我试图从mysql表中检索数据,并使用thymeleaf在HTML表中显示数据。我添加了SpringWeb、mysql驱动程序、SpringDataJPA和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.ProductRepository;
导入org.springframework.data.jpa.repository.JpaRepository;
进口com.chigudu.Entities.Product;
公共接口ProductRespository扩展了JPA pository{
}
服务等级

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.ProductServices;
导入java.util.List;
导入java.util.ArrayList;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Service;
导入com.chigudu.ProductRepository.ProductRespository;
进口com.chigudu.Entities.Product;
@服务
公共类产品服务{
@自动连线
私人产品回购;
公共列表listAll(){
返回回购findAll();
}
公共作废保存(产品){
回购保存(产品);
}
公共产品获取(长id){
返回repo.findById(id.get();
}
私有无效删除(长id){
回购协议删除id(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";
    }
    
}
package com.chigudu.AppControllers;
导入java.util.List;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Controller;
导入org.springframework.ui.Model;
导入org.springframework.web.bind.annotation.RequestMapping;
进口com.chigudu.Entities.Product;
导入com.chigudu.ProductServices.ProductService;
@控制器
公共类AppController{
@自动连线
私人产品服务;
//查看应用程序主页
@请求映射(“/”)
公共字符串视图主页(模型){
List listProducts=service.listAll();
model.addAttribute(“listProducts”,listProducts);
返回“索引”;
}
}
百里香叶

<!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>

家
产品经理
产品ID
名称
烙印
制造
价格
产品ID
名称
烙印
制造
价格

您在存储库接口上缺少@Repository注释。如果在基于注释的配置中没有@Service、@Repository、@Component或@Controller注释,则SpringBean初始化不会考虑类。

您在存储库接口上缺少@Repository注释。如果没有@Service,@Repository、@Component或@Controller注释在基于注释的配置中,Springbean初始化不考虑类。

如何存储数据?我没有在您的控制器中看到
post
,我使用mysql workbench存储数据,我只是尝试使用spring bootWrite从表中读取数据,并编写一个api来检查表中有多少行。通过这种方式,您可以检查您是否查询了正确的模式。使用repository.count()方法如何存储数据?我没有在您的控制器中看到
post
,我使用mysql workbench存储数据,我只是尝试使用spring bootWrite从表中读取数据,并编写一个api来检查表中有多少行。通过这种方式,您可以检查您是否查询了正确的模式。使用repository.count()方法thanx,我放了@repository注释,但问题没有解决,可能我遗漏了另一个概念。请启用spring jpa日志以查看查询过程中发生的情况。thanx,我放了@repository注释,但问题没有解决,也许我遗漏了另一个概念,请启用spring jpa日志以查看查询过程中发生了什么。