运行 Spring Boot 应用程序时创建 bean 时出错

问题描述 投票:0回答:1

我正在使用 Spring Boot、JPA 和 Lombok 创建一个博客应用程序,其中包含三个实体,即帖子、用户和类别,其中类别和用户是帖子的父实体。创建此帖子实体后,在运行此应用程序时出现错误 -

创建名称为“postController”的 bean 时出错:依赖关系不满足 通过字段“postService”表示:创建名称为 bean 时出错 “postServiceImpl”:通过字段表达的不满足的依赖关系 'postRepo':创建名称为'postRepo'的bean时定义错误 com.backendapi.blogapp.repositories.PostRepo 定义于 @EnableJpaRepositories 在 BlogAppApplication 上声明:无法 为公共抽象 java.util.List 创建查询 com.backendapi.blogapp.repositories.PostRepo.findByuser(com.backendapi.blogapp.entities.user); 原因:无法为方法公共摘要创建查询 java.util.List com.backendapi.blogapp.repositories.PostRepo.findByuser(com.backendapi.blogapp.entities.user); 找不到类型“帖子”的属性“用户”;您指的是“我们”吗

表已在 mySQL 数据库中成功创建。目前,我仅在邮递员上测试一种方法,即 POST,我面临此错误。我使用 Model Mapper 将实体 obj 映射到 dto,反之亦然。 我的代码:

POST 实体

package com.backendapi.blogapp.entities;
import java.util.Date;
import com.backendapi.blogapp.entities.Post;
import com.backendapi.blogapp.entities.user;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.*;

@Entity
@Table(name="post")
@Getter
@Setter
@NoArgsConstructor
public class Post {
    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer postId;
    
    @Column(name="post_title",length=100,nullable=false)
    private String title;
    
    @Column(length=10000)
    private String content;
    private String Imgname;
    private Date addDate;
    
    @ManyToOne
    @JoinColumn(name="category_id")
    private Category category;
    
    @ManyToOne
    private user us;
}

用户实体**

package com.backendapi.blogapp.entities;

import java.util.ArrayList;
import java.util.List;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Table(name="users")
@NoArgsConstructor
@Getter
@Setter
public class user {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    
    @Column(name="user_nmae", nullable=false, length=100)
    private String name;
    private String email;
    private String password;
    private String about;
    
    @OneToMany(mappedBy="us",cascade=CascadeType.ALL,fetch=FetchType.LAZY)
    private List<Post> posts=new ArrayList<>();
}

类别实体

package com.backendapi.blogapp.entities;

import java.util.ArrayList;
import java.util.List;

import jakarta.persistence.*;
import lombok.*;

@Entity
@Table(name="categories")
@Getter
@Setter
@NoArgsConstructor
public class Category {
    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer categoryId;
    
    @Column(name="title",length=10)
    private String categoryTitle;
    
    @Column(name="description")
    private String categoryDesc;
    
    @OneToMany(mappedBy="category", cascade=CascadeType.ALL,fetch=FetchType.LAZY)
    private List<Post> posts=new ArrayList<>();
    
    
}

DTO 课程后

package com.backendapi.blogapp.payloads;

import java.util.Date;

import com.backendapi.blogapp.entities.Category;
import com.backendapi.blogapp.entities.user;
import lombok.*;

@Getter
@Setter
@NoArgsConstructor
public class PostDto {
    
    private String title;
    private String content;
    private String Imgname;
    private Date addDate;
    private Category category;
    private user us;
    
    
}

后期存储库类

package com.backendapi.blogapp.repositories;

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.backendapi.blogapp.entities.Category;
import com.backendapi.blogapp.entities.Post;
import com.backendapi.blogapp.entities.user;

@Repository
public interface PostRepo extends JpaRepository<Post,Integer> {

    List<Post> findByuser(user us);
    List<Post> findByCategory(Category cat);
}

后期服务界面

package com.backendapi.blogapp.services;

import java.util.List;

import com.backendapi.blogapp.entities.Post;
import com.backendapi.blogapp.payloads.PostDto;

public interface PostService {

    //create
    PostDto createPost(PostDto postDto,Integer userId,Integer categoryId);
    
    //update
    PostDto updatePost(PostDto postDto,Integer postId);
    
    //delete
    void deletePost(Integer postId);
    
    //get by postId
    PostDto getPostById(Integer postId);
    
    //get by categoryId
    PostDto getPostByCategoryId(Integer categoryId);
    
    //get by userId
    PostDto getPostByUserId(Integer userId);
    
    //getAll
    List<PostDto> getAllPost();
    
    //search post
    List<PostDto> searchPost(String keyword);
    
}

POST SERVICE 实施课程

package com.backendapi.blogapp.services.impl;

import java.util.Date;
import java.util.List;

import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.backendapi.blogapp.entities.Category;
import com.backendapi.blogapp.entities.Post;
import com.backendapi.blogapp.entities.user;
import com.backendapi.blogapp.exceptions.ResourceNotFoundException;
import com.backendapi.blogapp.payloads.PostDto;
import com.backendapi.blogapp.repositories.CategoryRepo;
import com.backendapi.blogapp.repositories.PostRepo;
import com.backendapi.blogapp.repositories.UserRepo;
import com.backendapi.blogapp.services.PostService;

@Service
public class PostServiceImpl implements PostService {

    @Autowired
    private PostRepo postRepo;
    
    @Autowired
    private UserRepo userRepo;
    
    @Autowired
    private CategoryRepo categoryRepo;
    
    @Autowired
    private ModelMapper modelMapper;
    
    @Override
    public PostDto createPost(PostDto postDto,Integer userId,Integer categoryId) {
        
        user us=this.userRepo.findById(userId).orElseThrow(()->new ResourceNotFoundException("User","User Id",userId));
        Category cat=this.categoryRepo.findById(categoryId).orElseThrow(()->new ResourceNotFoundException("Category","Category Id",categoryId));
        
        Post post=this.dtoToPost(postDto);
        post.setImgname("D:\\My Data\\projects\\major\\Sentiment_Analysis\\practice\\Sundar Pichai\\gettyimages-174342043-612x612.jpg");
        post.setAddDate(new Date());
        post.setUs(us);
        post.setCategory(cat);
        Post savedPost=this.postRepo.save(post);
        return this.postToDto(savedPost);
    }

    @Override
    public PostDto updatePost(PostDto postDto, Integer postId) {
        
        return null;
    }

    @Override
    public void deletePost(Integer postId) {
        // TODO Auto-generated method stub

    }

    @Override
    public PostDto getPostById(Integer postId) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public PostDto getPostByCategoryId(Integer categoryId) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public PostDto getPostByUserId(Integer userId) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<PostDto> getAllPost() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<PostDto> searchPost(String keyword) {
        // TODO Auto-generated method stub
        return null;
    }

    public PostDto postToDto(Post post) {
        PostDto pDto=this.modelMapper.map(post,PostDto.class);
        return pDto;
    }
    
    public Post dtoToPost(PostDto pDto) {
        Post post=this.modelMapper.map(pDto,Post.class);
        return post;
    }
}

后置控制器类

package com.backendapi.blogapp.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.backendapi.blogapp.entities.Post;
import com.backendapi.blogapp.payloads.PostDto;
import com.backendapi.blogapp.services.PostService;

@RestController
@RequestMapping("/api")
public class PostController {
    
    @Autowired
    PostService postService;
    
    //Post
    @PostMapping("/user/{userId}/category/{categoryId}/posts")
    public ResponseEntity<PostDto> createPost(@RequestBody PostDto postDto,@PathVariable Integer userId,@PathVariable Integer categoryId){
        PostDto post=this.postService.createPost(postDto,userId,categoryId);
        return new ResponseEntity<>(post,HttpStatus.CREATED);
    }
    
    //Put
    
    //Delete
    
    //Get
    
    //GetAll
    
    //Search
}

如何解决这个错误?

mysql spring-boot spring-data-jpa spring-tool-suite modelmapper
1个回答
0
投票

正如错误消息所示:

无法为方法 public Abstract java.util.List 创建查询 com.backendapi.blogapp.repositories.PostRepo.findByuser(com.backendapi.blogapp.entities.user); 找不到类型“帖子”的属性“用户”;您指的是“我们”吗

您在实体中没有财产

user
POST

所以方法必须命名为:

List<Post> findByUs(user us);
© www.soinside.com 2019 - 2024. All rights reserved.