love_Java123
love_Java123
采纳率42.9%
2017-09-08 03:25 浏览 7.7k
已采纳

SpringBoot集成SpringData自定义query语句时报错

我的Repositity的类是这样写的:
@Repository
public interface UserRepository extends JpaRepository{
//public User findByUsername(String username);
public User findByPassword(String password);
public User findByUsernameAndPassword(String username,String password);
@Query("select u from user u where u.username = :username")
public User findByUsername(@Param("username") String username);
}
serrvice接口是这样的:
public interface UserService {
public User findUserByPassword(String password);
public User findUserByUsernameAndPassword(String username,String password);
public User findUserByUserName(String username);
}

项目一启动就报错,关键的报错信息如下:
报错信息如下:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract com.tky.test.entity.User com.tky.test.repository.UserRepository.findByUsername(java.lang.String)!

            at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
            at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
            at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
            at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
            at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
            at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答 默认 最新

  • 已采纳
    hometing218 hometing218 2017-09-08 07:27

    @Query后面必须用类名,你应该是User吧

    点赞 4 评论 复制链接分享
  • u013646636 最讨厌取名字了 2017-09-08 05:44

    service层加@service

    点赞 评论 复制链接分享
  • oops_bao oops_bao 2017-09-08 06:58

    加上nativeQuery=true
    @query(value="select u from user u where u.username = :username",nativeQuery=true)
    因为是原生sql

    点赞 评论 复制链接分享
  • lilili9309 lilili9309 2017-11-10 02:14

    原生sql加上nativeQuery = true,不然就按照默认hql了

    点赞 评论 复制链接分享

相关推荐