当前位置: 首页 > 知识库问答 >
问题:

用自定义方法和JpaRepository实现接口

鲁彬炳
2023-03-14
com.wizardry.witchcraft.domain.model.StudentModel;  
com.wizardry.witchcraft.infraestructure.repository.CustomRepositoryImpl;  
com.wizardry.witchcraft.domain.repository.IStudentRepository;  
com.wizardry.witchcraft.domain.repository.ICustomRepository;
com.wizardry.witchcraft.api.controller.TesteController2;
com.wizardry.witchcraft.api.controller.StudentController;
 import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    import org.springframework.stereotype.Repository;
    import com.wizardry.witchcraft.domain.model.StudentModel;
    import com.wizardry.witchcraft.domain.repository.ICustomRepository;

        @Repository
        public class CustomRepositoryImpl implements ICustomRepository {
        
        @PersistenceContext
            private EntityManager manager;
            
            @Override
            public List<StudentModel> findCustom (String name){
                //METHOD
    
            }
        
        }
import com.wizardry.witchcraft.domain.model.StudentModel;

public interface ICustomRepository {

    List<StudentModel> findCustom(String name);

}

为了测试它,我创建了一个新的控制器,以避免干扰我正在工作的控制器TesteController2,它运行良好。因此,我的下一步是扩展IStudentRepository中的ICustomRepository,在TesteController2中进行更改,然后Spring将不再找到我的findCustom方法,它尝试将该方法创建为JPA关键字并返回和错误。这是我的存储库接口

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import com.wizardry.witchcraft.domain.model.StudentModel;

@Repository
public interface IStudentRepository extends ICustomRepository, JpaRepository<StudentModel, Long> {
    
      List<StudentModel> queryByName(String name, @Param ("id") Long school);
      
      List<StudentModel> queryFirstByNameContaining(String name);
      
      List<StudentModel> queryTop2ByNameContaining(String name);
      
      int countByWizardingSchoolModelId(Long school);
     

}

和TesteController2:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.wizardry.witchcraft.domain.model.StudentModel;
import com.wizardry.witchcraft.domain.repository.IStudentRepository;

@RestController
@RequestMapping(value = "/test")
public class TesteController2 {

    @Autowired
    private IStudentRepository iStudentRepository;

    @ResponseStatus(HttpStatus.ACCEPTED)
    @GetMapping
    public List<StudentModel> findCustom2(String name) {
        return iStudentRepository.findCustom(name);
    }
    
}

PS:我有一个服务层com.wizardry.witchcraft.domain.Service.RegisterStudentService,但是这个方法还没有通过它因为我正在测试,所以我试图通过服务,看看发生了什么,但给出了相同的错误。

*错误:org.springframework.beans.factory.unsatifiedDependencyException:创建名为“Student Controller”的bean时出错:通过“Register StudentService”字段表示不满足的依赖关系;嵌套异常是org.springframework.beans.factory.UnsatifiedDependencyException:创建名为“Register StudentService”的bean时出错:通过字段“i StudentRepository”表示未满足的依赖关系;嵌套异常是org.springframework.beans.factory.beanCreationException:在jparepositoriesRegistrar.enableJParepositoriesConfiguration上声明的@enableJParepositories中创建名为'IstudentRepository中定义的名为'IstudentRepository'的bean时出错:调用init方法失败;嵌套异常是java.lang.IllegalArgumentException:未能为方法public abstract java.util.list com.wizardry.witchcraft.domain.repository.icustomrepository.findCustom(java.lang.string)创建查询!找不到StudentModel类型的属性findCustom!*

提前感谢!我在这里真的迷路了。

共有1个答案

汪阳飇
2023-03-14

只需将您的类CustomRepositoryImpl重命名为IStudentRepositoryImpl,它就可以工作了。

      @Repository
    public class IStudentRepositoryImpl implements ICustomRepository {
    
    @PersistenceContext
        private EntityManager manager;
        
        @Override
        public List<StudentModel> findCustom (String name){
            //METHOD

        }
    
    }

下面是Spring的文档

配置如果使用命名空间配置,存储库基础结构将通过扫描我们在其中找到存储库的包下面的类来自动检测自定义实现。这些类需要遵循命名约定,将命名空间元素的属性repository-impl-postfix附加到所找到的存储库接口名称。此后缀默认为Impl。

 类似资料:
  • 我是Spring Boot的新手,所以我有几个基本问题。 当我们定义一个扩展JpaRepostory的Repostory接口时 这些方法可以从 Service 类调用(我们可能甚至不需要提及这两个方法)。到目前为止,一切都很好。 问题: > 方法是如何定义的?它在幕后是如何被称为的? 如果我们想用原生SQL添加一个自定义方法< code>findUserAction呢?在这种情况下,我们需要在<

  • 我读到了Crudrepository,这是一个接口,用于在特定类型的存储库上进行通用CRUD操作。 但是我们可以创建我们的自定义接口并扩展CrudRepository。

  • 本文向大家介绍C#自定义签名章实现方法,包括了C#自定义签名章实现方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了C#自定义签名章实现方法。分享给大家供大家参考。具体实现方法如下: 希望本文所述对大家的C#程序设计有所帮助。

  • 本文向大家介绍thinkPHP自定义类实现方法详解,包括了thinkPHP自定义类实现方法详解的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了thinkPHP自定义类实现方法。分享给大家供大家参考,具体如下: 1.通过Model调用 没有继承Model,否则会因为表不存在而报错。 调用, 调用确实方便,但是总感觉有点不合理。这个D毕竟是操作数据库的。 2.通过引入类实现,把类放到ORG下

  • 本文向大家介绍Android自定义dialog简单实现方法,包括了Android自定义dialog简单实现方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Android自定义dialog简单实现方法。分享给大家供大家参考,具体如下: 更多关于Android开发相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》 希望本文所述对大家Android程序设计有所帮助。

  • 本文向大家介绍MFC自定义消息的实现方法,包括了MFC自定义消息的实现方法的使用技巧和注意事项,需要的朋友参考一下 一、概述: 消息机制是windows程序的典型运行机制,在MFC中有很多已经封装好了的消息,如WM_BTN**等。但是在有些特殊情况下我们需要自定义一些消息去完成一些我们所需要的功能,这时候MFC的向导不能帮助我们做到这一点。对此,我们可以通过添加相应的代码去完成这个功能。 二、实现