我从 Spring Boot 数据库教程开始,但想对 SQL 查询添加一些限制。
我添加了分页,但出现错误。
我有以下存储库类。
有什么想法吗?
谢谢。
package com.example.accessingdatamysql;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.CrudRepository;
import com.example.accessingdatamysql.User;
// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
// CRUD refers Create, Read, Update, Delete
public interface UserRepository extends CrudRepository<Consignment, Integer> {
Page<Example> findByValidIsTrue(Pageable pageable);
}
错误如下
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mainController': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository' defined in com.example.accessingdatamysql.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 org.springframework.data.domain.Page com.example.accessingdatamysql.UserRepository.findConsignmentsByAccount(org.springframework.data.domain.Pageable)! Reason: Failed to create query for method public abstract org.springframework.data.domain.Page com.example.accessingdatamysql.UserRepository.findConsignmentsByAccount(org.springframework.data.domain.Pageable)! Method public abstract org.springframework.data.domain.Page com.example.accessingdatamysql.UserRepository.findConsignmentsByAccount(org.springframework.data.domain.Pageable) expects at least 1 arguments but only found 0. This leaves an operator of type SIMPLE_PROPERTY for property account unbound.; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract org.springframework.data.domain.Page com.example.accessingdatamysql.UserRepository.findConsignmentsByAccount(org.springframework.data.domain.Pageable)! Method public abstract org.springframework.data.domain.Page com.example.accessingdatamysql.UserRepository.findConsignmentsByAccount(org.springframework.data.domain.Pageable) expects at least 1 arguments but only found 0. This leaves an operator of type SIMPLE_PROPERTY for property account unbound.
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) ~[spring-beans-5.3.8.jar:5.3.8]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.3.8.jar:5.3.8]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.8.jar:5.3.8]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.8.jar:5.3.8]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1413) ~[spring-beans-5.3.8.jar:5.3.8]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601) ~[spring-beans-5.3.8.jar:5.3.8]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.8.jar:5.3.8]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.8.jar:5.3.8]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.8.jar:5.3.8]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.8.jar:5.3.8]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.8.jar:5.3.8]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.8.jar:5.3.8]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.8.jar:5.3.8]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.8.jar:5.3.8]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.5.2.jar:2.5.2]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-2.5.2.jar:2.5.2]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[spring-boot-2.5.2.jar:2.5.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) ~[spring-boot-2.5.2.jar:2.5.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-2.5.2.jar:2.5.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) ~[spring-boot-2.5.2.jar:2.5.2]
at com.example.accessingdatamysql.AccessingDataMysqlApplication.main(AccessingDataMysqlApplication.java:10) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.5.2.jar:2.5.2]
下面是否使用了可能导致问题的控制器。我尝试删除@Autowired,但仍然无效。
package com.example.accessingdatamysql;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@Controller // This means that this class is a Controller
@RequestMapping(path="/demo") // This means URL's start with /demo
public class MainController {
@Autowired // This means to get the bean called userRepository
// Which is auto-generated by Spring, we will use it to
handle the data
private UserRepository userRepository;
@PostMapping(path="/add") // Map ONLY POST Requests
public @ResponseBody String addNewUser (@RequestParam String name
, @RequestParam String email) {
// @ResponseBody means the returned String is the response, not a view name
// @RequestParam means it is a parameter from the GET or POST request
return "Saved";
}
@GetMapping(path="/all")
@ResponseBody
public String getAllUsers() {
// This returns a JSON or XML with the users
PageRequest req = PageRequest.of(0,10); // I just want 10 record
Page<Example> page = userRepository.findByValidIsTrue(req );
List<Example> nRecords = page.getContent();
Gson json = new Gson();
return json.toJson(nRecords);
}
}
答案 0 :(得分:1)
嗨@Chris,很少有问题CrudRepository
不支持 Pageable
如果要使用 Pageable 使用分页,请使用 JpaRepository
表单 Spring Data
public interface UserRepository extends JpaRepository<Example, Integer> {
Page<Example> findByValidIsTrue(Pageable pageable);
}
注意:-您提供与返回类型类相同的类
答案 1 :(得分:0)
你也可以用 PagingAnsSortingRespository 代替。
公共接口 UserRepository extends PagingAnsSortingRespository
答案 2 :(得分:0)
在我看来,您的问题是 bean 创建问题。您可以在代码中验证以下步骤。
在 SpringBootApplication (Main Class)
@EntityScan("com.example.*") //packages for entity classes
@EnableJpaRepositories("com.example.*")//packages for repo classes
将 @Repository
和 JpaRepository
添加到存储库类
@Repository
public interface UserRepository extends JpaRepository<Consignment, Integer> {
Page<Example> findByValidIsTrue(Pageable pageable);
}
验证 @Configuration
类是否存在。
实体类应该有 @Entity
、@Table
。
主键 @Id
在 Consignment
中应该是整数类型。如接口签名中所述。 CrudRepository<Consignment, Integer>