# SpringBootJpa动态查询工具 **Repository Path**: kingons/spring-boot-jpa-query-util ## Basic Information - **Project Name**: SpringBootJpa动态查询工具 - **Description**: SpringBootJpa动态查询工具 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 0 - **Created**: 2021-05-14 - **Last Updated**: 2022-06-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Spring Boot Jpa 查询工具 工具来源: https://gitee.com/kingons/eladmin 前置条件:`Repository接口需继承JpaSpecificationExecutor接口` 测试类: ``` @Entity @Data public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column private String name; } ------------------------ @Data public class UserQueryCriteria { /* 精确,值null时忽略 */ @Query private Integer id; /* 精确, 即name = :name,值null时忽略 */ @Query private String name; /* 模糊查询,即 name like %:name%,值null时忽略,更多类型请查看club.kingon.jpa.query.annotation.Query注解 */ @Query(propName="name", type=Query.Type.INNER_LIKE) private String likeName; } ------------------------ public interface UserRepository extends JpaRepository, JpaSpecificationExecutor { } ``` 查询代码如下: ``` @RestController @RequestMapping("/api/user") @RequiredArgsConstructor public class UserController { private final UserRepository userRepository; @GetMapping public Page queryAll(UserQueryCriteria criteria, Pageable pageable) { return userRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelper.getPredicate(root, criteria, criteriaBuilder), pageable ); } } ``` 查询示例: ``` /* 用户ID精确查询 */ curl http://127.0.0.1:8080/api/user?id=1 curl http://127.0.0.1:8080/api/user?id=1&page=0&size=10 /* 用户名精确查询 */ curl http://127.0.0.1:8080/api/user?name=dragons curl http://127.0.0.1:8080/api/user?name=dragons&page=0&size=10 /* 用户名模糊查询 */ curl http://127.0.0.1:8080/api/user?likeName=dra curl http://127.0.0.1:8080/api/user?likeName=dra&page=0&size=10 ```