2 love java123 love_Java123 于 2017.09.08 11:25 提问

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个回答

u013646636
u013646636   2017.09.08 13:44

service层加@service

love_Java123
love_Java123 加了,我如果不用@query写sql语句是没问题的,但是加上@query自己写sql就有问题
2 个月之前 回复
oops_bao
oops_bao   2017.09.08 14:58

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

hometing218
hometing218   2017.09.08 15:27

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

lilili9309
lilili9309   2017.11.10 10:14

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

Csdn user default icon
上传中...
上传图片
插入图片