在过去的几天里,我正在研究Spring Boot的各个方面,今天我对JpaRepository感到沮丧。这个例子是介绍性的,MySQL JPA,围绕MVC设计构建。我之前有过与Core Java的MySQL集成,但没有Spring Boot。
代码如下:
绒球.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.fanshawe</groupId>
<artifactId>springboot-mysql-jpa-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-mysql-jpa-demo</name>
<description>Demo project for Spring Boot, Maven, Spring JPA and MySQL</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
BlogRepo.java:
package com.example.springbootmysqljpademo.repo;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
//to import quickly, click Ctrl-Shift-O
import org.springframework.stereotype.Repository;
import com.example.springbootmysqljpademo.model.Blog;
@Repository
public interface BlogRepo extends JpaRepository<Blog, Integer>{
List<Blog> findByTitleContainingOrContentContaining(String text, String textAgain);
Blog findOne(int blogId);
}
博客.java:
package com.example.springbootmysqljpademo.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Blog {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String title;
private String content;
public Blog() {
// TODO Auto-generated constructor stub
}
public Blog(String title, String content) {
this.setTitle(title);
this.setContent(content);
}
public Blog(int id, String title, String content) {
this.setId(id);
this.setTitle(title);
this.setContent(content);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "Blog [id=" + id + ", title=" + title + ", content=" + content + "]";
}
}
BlogController.java:
package com.example.springbootmysqljpademo.controller;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.springbootmysqljpademo.model.Blog;
import com.example.springbootmysqljpademo.repo.BlogRepo;
@RestController
public class BlogController {
@Autowired
BlogRepo blogRepo;
@GetMapping("/blog")
public List<Blog> displayAllBlogs() {
return blogRepo.findAll();
}
@GetMapping("/blog/{id}")
public Blog show(@PathVariable String id) {
int blogId = Integer.parseInt(id);
return blogRepo.findOne(blogId);
}
@PostMapping("/blog/search")
public List<Blog> searchBlogs(@RequestBody Map<String, String> body) {
String searchTerm = body.get("text");
return blogRepo.findByTitleContainingOrContentContaining(searchTerm, searchTerm);
}
@PostMapping("/blog")
public Blog create(@RequestBody Map<String, String> body) {
String title = body.get("title");
String content = body.get("content");
return blogRepo.save(new Blog(title, content));
}
@PutMapping("/blog/{id}")
public Blog update(@PathVariable String id, @RequestBody Map<String, String> body) {
int blogId = Integer.parseInt(id);
Blog blog = blogRepo.findOne(blogId);
blog.setTitle(body.get("title"));
blog.setContent(body.get("content"));
return blogRepo.save(blog);
}
@DeleteMapping("/blog/{id}")
public boolean delete(@PathVariable String id) {
int blogId = Integer.parseInt(id);
blogRepo.deleteById(blogId);
return true;
}
}
和主应用程序文件:
package com.example.springbootmysqljpademo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@ComponentScan
@EnableJpaRepositories
public class SpringbootMysqlJpaDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMysqlJpaDemoApplication.class, args);
}
}
应用mysql配置的属性:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/new_db
spring.datasource.username=root
spring.datasource.password=password
堆栈跟踪:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'blogController': Unsatisfied dependency
这是我试图解决的问题:
findById
与findOne
方法时遇到了问题,所以我必须这样做才能使用findOne
方法。@Controller
需要@RestController
才能正确编译。@Repository
注释。未满足的依赖项异常
JPA Crud回购
Spring无法创建bean
除此之外,我怀疑这是我做注释的方式的问题,尽管在阅读了各种来源后,即使我对它们没有最复杂的理解,也没有任何东西让我印象深刻。在这一点上,我一般不知道如何处理它。
先谢谢你!
使用组件扫描,您也应该添加基本包,在这样做之前,请尝试将它们全部放在一个包中,这将告诉您是否是包装的问题。https://www.baeldung.com/spring-unsatisfied-dependency,此链接涵盖了未满足的依赖异常的原因,如果您没有,请查看。