无法从我的Java类

时间:2018-05-09 04:22:28

标签: java spring jpa spring-data

我正在开发一个在线教程,在我需要使用数据库并添加jpa的步骤中,我无法在我的项目中访问它。

我已成功将依赖项放在我的pom.xml文件中,并且没有收到任何错误。我也可以通过我的application.properties文件访问spring jpa。问题是当我想扩展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 个答案:

答案 0 :(得分:0)

将注释添加到DAO中,它应该是接口而不是类,

primary surrogate key

但是,我建议您使用@Repository public interface CheeseDao extends JpaRepository<Cheese, Integer> { } 代替JpaRepository。有关详细信息,请参阅this stackoverflow thread

现在,您可以从任何带有弹簧注释的类中访问它们,如下所示

CrudRepository

答案 1 :(得分:0)

您似乎缺少了休眠实体依赖关系,只需在下面添加依赖关系即可解决

if not 'logged_user' in request.session:
    return redirect('mqr_admin:login', status=401)

答案 2 :(得分:0)

替换

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>mysql-connector-java</artifactId>
</dependency>

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
</dependency>

答案 3 :(得分:0)

代替

resource "aws_s3_bucket_policy" "aws_s3_bucket_resource_based_policy" {
  bucket = aws_s3_bucket.secret_bucket.id

  policy = jsonencode({
    Version = "2012-10-17"
    Id      = "allow_backend_access_id_policy"
    Statement = [
      {
        Sid       = "allow_backend_access"
        Effect    = "Allow"
        "Principal": {
        "AWS": [
          "123456789"
        ]
       },
        Action    = ["s3:*"]
        Resource = [
          aws_s3_bucket.secret_bucket.arn,
          "${aws_s3_bucket.secret_bucket.arn}/*",
        ]
        Condition = {
          StringEquals = {
            "aws:ResourceTag/group_name" = "backend"
          }
        }
      }
    ]
  })
}

试试

package com.example.demo.models;
import com.example.demo.models.Cheese;


public class CheeseDao extends CrudRepository<Cheese, Integer> {
}

CrudRepository<> 需要类型。