2 love java123 love_Java123 于 2017.09.08 14:32 提问

SpringBoot集成SpringData遇到问题

我的代码走通了,但是我想在Repository中尝试用@Query自己写sql语句的时候发生了错误
Repository层的代码如下:
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);
service层的代码:
public User findUserByPassword(String password);
public User findUserByUsernameAndPassword(String username,String password);
public User findUserByUserName(String username);
service层的实现类:
@Autowired
private UserRepository userRepository;

@Override
public User findUserByUserName(String username) {
    User user = userRepository.findByUsername(username);
    return user;
}

@Override
public User findUserByPassword(String password) {
    User user = userRepository.findByPassword(password);
    return user;
}

@Override
public User findUserByUsernameAndPassword(String username, String password) {
    User user = userRepository.findByUsernameAndPassword(username, password);
    return user;
}

加上@Query后,项目启动就报错
错误如下:
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]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
at com.tky.test.SpringdataDemoApplication.main(SpringdataDemoApplication.java:10) [classes/:na]

4个回答

AAAAABBBBBYYYYY
AAAAABBBBBYYYYY   2017.09.08 14:39
已采纳

@query 里面的表 user 要跟你的实体类名一样 应该是User。或者你要想写原生sql要加上nativeQuery = true

AAAAABBBBBYYYYY
AAAAABBBBBYYYYY 回复love_Java123: 可以了哈?
2 个月之前 回复
love_Java123
love_Java123 谢谢
2 个月之前 回复
Small_Mouse0
Small_Mouse0   Ds   Rxr 2017.09.08 14:37

UserRepository这个接口在哪尼??

 //JpaRepository<User, Long>,,第一个是
public interface UserRepository extends JpaRepository<User, Long> {

}

继承了没??,

love_Java123
love_Java123 我把泛型的serializable改成long还是不行
2 个月之前 回复
Small_Mouse0
Small_Mouse0 回复love_Java123: Serializable可序列化的接口??,,写这个不合适吧,,第二个写的是主键类型,String,Long等
2 个月之前 回复
love_Java123
love_Java123 public interface UserRepository extends JpaRepository<User, Serializable>{ 我是这样写的
2 个月之前 回复
qq_25324335
qq_25324335   2017.09.08 14:46

@Query写的不是sql,而是JPQL,里面from后面接的是实体,而不是数据库的表名

hometing218
hometing218   2017.09.08 15:18

@Query后面应该是跟实体类名,字段也要用实体类里面定义的字段.如:表名:user,类名:User,那就得用User,表字段名user_name,实体字段:userName,那么用的时候就得用userName

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