我正在做一个在线教程,在我需要处理数据库和添加jpa的步骤中,我无法在我的项目中访问它。
我已经成功地将依赖项放入了pom.xml文件中,并且没有收到任何错误。我还可以通过我的www.example.com文件访问spring jpaapplication.properties。问题是当我想扩展CrudRepository时,它没有被识别...然后我意识到当我手动导入时,它不会导入org.springframework.data。
我已将Maven设置为始终更新快照。
这是我的pom.xml文件。我为格式问题道歉,但我无法让它以intellij中的正确格式显示在stackoverflow上:
<?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 http://maven.apache.org/xsd/ maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo3</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
和我的“application.properties”文件:
spring.datasource.url=***
spring.datasource.username=***
spring.datasource.password=***
spring.jpa.database=MYSQL
spring.jpa.hibernate.ddl-auto = update
spring.jpa.show-sql=false
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
奶酪类:
package com.example.demo.models;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
@Entity
public class Cheese {
@Id
@GeneratedValue
private int id;
@NotNull
@Size(min=3, max=20)
private String name;
@NotNull
@Size(min=1, message = "Description must not be empty")
private String description;
public Cheese(String name, String description) {
this.name = name;
this.description = description;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
CheeseDao(使用Spring Data /CrudRepository的接口)
package com.example.demo.models;
import com.example.demo.models.Cheese;
public class CheeseDao extends CrudRepository<Cheese, Integer> {
}
4条答案
按热度按时间5sxhfpxr1#
将注解添加到DAO中,它应该是接口而不是类,
但是,我建议您使用
JpaRepository
而不是CrudRepository
。有关详细信息,请参阅this stackoverflow thread。现在您可以从任何带Spring注解的类访问它们,如下所示,
brqmpdu12#
看起来您缺少Hibernate实体依赖项,要解决此问题,只需在下面添加依赖项
6tdlim6h3#
替换
与
bogh5gae4#
代替
尝试
CrudRepository〈〉需要类型。