controller
@RestController @RequestMapping("demo") public class UserController { @Autowired private UserService userService; @GetMapping("/queryUser") public List<UserEntity> queryUser(@Param("name") String name) { return userService.queryUser(name); } @GetMapping("/queryUserByName") public List<UserEntity> queryUserByName(@Param("name") String name) { return userService.queryUserByName(name); } @GetMapping("/queryUserByNameAndAge") public List<UserEntity> queryUserByNameAndAge(@Param("name") String name, @Param("age") Integer age) { return userService.queryUserByNameAndAge(name, age); } @PostMapping("/queryUserByEntity") public List<UserEntity> queryUserByEntity(@RequestBody UserEntity user) { return userService.queryUserByEntity(user); } @PostMapping("/saveUser") public UserEntity saveUser(@RequestBody UserEntity user) { return userService.saveUser(user); } @PostMapping("/updateUserById") public UserEntity updateUserById(@RequestBody UserEntity user) { return userService.updateUserById(user); } @PostMapping("/updateUserByAges") public void updateUserByAges(@RequestBody UserEntity user) { userService.updateUserByAges(user); } @PostMapping("/deleteUserById") public boolean deleteUserById(@RequestBody UserEntity user) { return userService.deleteUserById(user); } @PostMapping("/pageUser") public Page<UserEntity> pageUser(@RequestBody UserEntity user) { return userService.pageUser(user); } @PostMapping("/specificationQueryUser") public Page<UserEntity> specificationQueryUser(@RequestBody UserEntity user) { return userService.specificationQueryUser(user); } @PostMapping("/exampleQueryUser") public Page<UserEntity> exampleQueryUser(@RequestBody UserEntity user) { return userService.exampleQueryUser(user); } }
service IMPL
@Service public class UserServiceImpl implements UserService { @Autowired private UserRepository userRepository; /** * @return */ @Transactional(readOnly = true) @Override public List<UserEntity> queryUser(String name) { return userRepository.queryByName(name); } /** * @param name * @return */ @Override public List<UserEntity> queryUserByName(String name) { return userRepository.queryUserByName(name); } /** * @param name * @param age * @return */ @Override public List<UserEntity> queryUserByNameAndAge(String name, Integer age) { return userRepository.queryUserByNameAndAge(name, age); } @Override public List<UserEntity> queryUserByEntity(UserEntity user) { return userRepository.queryUserByEntity(user); } /** * @param user * @return */ @Override public void updateUserByAges(UserEntity user) { userRepository.updateUserByName(); } /** * @param user * @return */ @Override @Transactional public UserEntity saveUser(UserEntity user) { return userRepository.save(user); } /** * @param user * @return */ @Override @Transactional public UserEntity updateUserById(UserEntity user) { return userRepository.save(user); } /** * @param user * @return */ @Override public boolean deleteUserById(UserEntity user) { userRepository.delete(user); return true; } /** * @param user * @return */ @Override public Page<UserEntity> pageUser(UserEntity user) { Pageable pageable = PageRequest.of(1 - 1, 10); return userRepository.queryByName(user.getName(), pageable); } @Override public Page<UserEntity> specificationQueryUser(UserEntity user) { Specification<UserEntity> specification = new Specification<UserEntity>() { /** * 构造断言 * @param root 实体对象引用 * @param query 规则查询对象 * @param cb 规则构建对象 * @return 断言 */ @Override public Predicate toPredicate(Root<UserEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) { List<Predicate> predicates = new ArrayList<>(); //所有的断言 if (user.getName() != null) { //添加断言 Predicate likeNickName = cb.like(root.get("name").as(String.class), user.getName() + "%"); predicates.add(likeNickName); } if (user.getAge() != null) { //添加断言 Predicate eqAge = cb.equal(root.get("age").as(Integer.class), user.getAge()); predicates.add(eqAge); } Predicate[] p = new Predicate[predicates.size()]; query.where(cb.and(predicates.toArray(p))); query.orderBy(cb.desc(root.get("age")), cb.desc(root.get("name"))); return query.getGroupRestriction(); } }; Pageable pageable = PageRequest.of(1 - 1, 10); return userRepository.findAll(specification, pageable); } /** * @param user * @return */ @Override public Page<UserEntity> exampleQueryUser(UserEntity user) { // 会忽略空 字符串能模糊匹配 非字符串只精确匹配 ExampleMatcher exampleMatcher = ExampleMatcher.matching() //模糊匹配 .withMatcher("name", ExampleMatcher.GenericPropertyMatcher::startsWith); //准确匹配 // .withMatcher("age", ExampleMatcher.GenericPropertyMatcher::exact); Example<UserEntity> example = Example.of(user, exampleMatcher); // 构造分页参数 Pageable pageable = PageRequest.of(1 - 1, 10); // 按照条件查询,并分页返回结果 return userRepository.findAll(example, pageable); }
dao
@Repository public interface UserRepository extends JpaRepository<UserEntity, Long> { @Modifying(clearAutomatically = true) @Transactional @Query(value = "update public.user set name ='1'", nativeQuery = true) void updateUserByName(); List<UserEntity> queryByName(String name); Page<UserEntity> queryByName(String name, Pageable pageable); Page<UserEntity> findAll(Specification<UserEntity> specification, Pageable pageable); @Query(nativeQuery = true, value = "select * from public.user where name = ? ") List<UserEntity> queryUserByName(String name); @Query(nativeQuery = true, value = "select * from public.user where name = ?1 and age = ?2 ") List<UserEntity> queryUserByNameAndAge(String name, Integer age); @Query(nativeQuery = true, name = "ss", value = "select * from public.user where name = :#{#userEntity.name} and age = :#{#userEntity.age}") List<UserEntity> queryUserByEntity(UserEntity userEntity); }
报错
ror starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-03-24 15:17:19.379 ERROR 23044 --- [ main] o.s.boot.SpringApplication : Application run failed
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 'userServiceImpl': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository' defined in com.example.jpademo.repository.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract void com.example.jpademo.repository.UserRepository.updateUserByName(); Reason: null; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.25.jar:5.3.25]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.25.jar:5.3.25]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.9.jar:2.7.9]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731) [spring-boot-2.7.9.jar:2.7.9]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) [spring-boot-2.7.9.jar:2.7.9]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-2.7.9.jar:2.7.9]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) [spring-boot-2.7.9.jar:2.7.9]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) [spring-boot-2.7.9.jar:2.7.9]
at com.example.jpademo.JpademoApplication.main(JpademoApplication.java:15) [classes/:na]
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository' defined in com.example.jpademo.repository.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract void com.example.jpademo.repository.UserRepository.updateUserByName(); Reason: null; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1391) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657) ~[spring-beans-5.3.25.jar:5.3.25]
... 20 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository' defined in com.example.jpademo.repository.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract void com.example.jpademo.repository.UserRepository.updateUserByName(); Reason: null; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1391) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657) ~[spring-beans-5.3.25.jar:5.3.25]
... 34 common frames omitted
Caused by: org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract void com.example.jpademo.repository.UserRepository.updateUserByName(); Reason: null; nested exception is java.lang.NullPointerException
at org.springframework.data.repository.query.QueryCreationException.create(QueryCreationException.java:101) ~[spring-data-commons-2.7.8.jar:2.7.8]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:107) ~[spring-data-commons-2.7.8.jar:2.7.8]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lambda$mapMethodsToQuery$1(QueryExecutorMethodInterceptor.java:95) ~[spring-data-commons-2.7.8.jar:2.7.8]
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_333]
at java.util.Iterator.forEachRemaining(Iterator.java:116) ~[na:1.8.0_333]
at java.util.Collections$UnmodifiableCollection$1.forEachRemaining(Collections.java:1051) ~[na:1.8.0_333]
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) ~[na:1.8.0_333]
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482) ~[na:1.8.0_333]
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472) ~[na:1.8.0_333]
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_333]
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_333]
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[na:1.8.0_333]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.mapMethodsToQuery(QueryExecutorMethodInterceptor.java:97) ~[spring-data-commons-2.7.8.jar:2.7.8]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lambda$new$0(QueryExecutorMethodInterceptor.java:87) ~[spring-data-commons-2.7.8.jar:2.7.8]
at java.util.Optional.map(Optional.java:215) ~[na:1.8.0_333]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.<init>(QueryExecutorMethodInterceptor.java:87) ~[spring-data-commons-2.7.8.jar:2.7.8]
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:365) ~[spring-data-commons-2.7.8.jar:2.7.8]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:323) ~[spring-data-commons-2.7.8.jar:2.7.8]
at org.springframework.data.util.Lazy.getNullable(Lazy.java:231) ~[spring-data-commons-2.7.8.jar:2.7.8]
at org.springframework.data.util.Lazy.get(Lazy.java:115) ~[spring-data-commons-2.7.8.jar:2.7.8]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:329) ~[spring-data-commons-2.7.8.jar:2.7.8]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:144) ~[spring-data-jpa-2.7.8.jar:2.7.8]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.25.jar:5.3.25]
... 44 common frames omitted
Caused by: java.lang.NullPointerException: null
at org.springframework.data.jpa.repository.query.QueryUtils.createCountQueryFor(QueryUtils.java:607) ~[spring-data-jpa-2.7.8.jar:2.7.8]
at org.springframework.data.jpa.repository.query.DefaultQueryEnhancer.createCountQueryFor(DefaultQueryEnhancer.java:49) ~[spring-data-jpa-2.7.8.jar:2.7.8]
at org.springframework.data.jpa.repository.query.StringQuery.deriveCountQuery(StringQuery.java:119) ~[spring-data-jpa-2.7.8.jar:2.7.8]
at org.springframework.data.jpa.repository.query.AbstractStringBasedJpaQuery.<init>(AbstractStringBasedJpaQuery.java:72) ~[spring-data-jpa-2.7.8.jar:2.7.8]
at org.springframework.data.jpa.repository.query.NativeJpaQuery.<init>(NativeJpaQuery.java:53) ~[spring-data-jpa-2.7.8.jar:2.7.8]
at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java:51) ~[spring-data-jpa-2.7.8.jar:2.7.8]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:169) ~[spring-data-jpa-2.7.8.jar:2.7.8]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:253) ~[spring-data-jpa-2.7.8.jar:2.7.8]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:93) ~[spring-data-jpa-2.7.8.jar:2.7.8]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:103) ~[spring-data-commons-2.7.8.jar:2.7.8]
... 66 common frames omitted
你这个update方法参数少了。你写的sql中有标记参数,但是你的方法又是一个无参构造。
那个是我写死的 更新成1 不会影响的,我也尝试过不写参数也不行
检查:
启动类加有没有这个注解@EnableJpaRepositories
UserEntity.class中有没有加注解@Entity
标黄处的 @Modify 和 @Transaction上下顺序换一换
启动类我尝试加过@EnableJpaRepositories
UserEntity.class 加了@entity 注解
尝试了@Modify 和 @Transaction 调换顺序 还是不行
好神奇 报的是注入失败为啥呢
vx 820398513 方便一起交流一下吗?