在过去的几天里,我正在研究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,此链接涵盖了未满足的依赖异常的原因,如果您没有,请查看。
这边已经存在模型和表了,但是创建居然会出错?不知道哪里出错了?
英文原文:http://emberjs.com/guides/controllers/dependencies-between-controllers/ 有时候,特别是在嵌套资源时,可能需要为两个控制器建立某种联系。以下面的路由为例: 1 2 3 4 5 App.Router.map(function() { this.resource("post", { path: "/posts/:po
通过go get 下载了依赖 通过go mod vendor 将依赖复制到vendor目录下 问题 有的依赖在$GOPATH 时存在包的 但是就是没有复制到vendor下 为什么?
我开始tomcat时,我得到了以下错误。由于循环依赖关系,tomcat 容器无法启动并在日志中显示 StackOverflowError。我尝试将-Xss值设置为4M,但没有帮助。无法弄清楚哪个是导致依赖关系问题的确切jar。我使用的是tomcat 7.0.40。 有人能帮忙吗。
我有一个问题,当我建立我的Maven项目在网豆。当在我的固定电脑上,它工作得很好,但在我的笔记本电脑上,我不能让它工作。我得到以下错误: 无法在project SnakeAppWeb页上执行目标:无法解析project se的依赖项。查默斯。snake:SnakeAppWebpage:war:1.0:org.apache.derby:derbyclient:jar:10.6.1.0(编译)、org
麻烦问下,我现在是已经安装好了node包的依赖,如何需要run dist,变异之后再运行,现在问题就是在run dist的时候,报上图的错误,很明显的是缺少了一个v,请问这个怎么处理呢, angular项目