Specifications动态查询

Specifications动态查询,第1张

Specifications动态查询

目录

1 Specifications单条件查询

 2 Specifications多条件查询

 3 Specifications模糊查询

4  Specifications的分页查询

 5 Specifications的排序分页查询


root:Root接口,代表查询的根对象,可以通过root获取实体中的属性

query:代表一个顶层查询对象,用来自定义查询

cb:用来构建查询,此对象里有很多条件方法

相关依赖:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.0
         
    
    com.liubujun
    springboot-jpa
    0.0.1-SNAPSHOT
    springboot-jpa
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-web
            2.4.0-SNAPSHOT
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
            2.4.0-SNAPSHOT
        
        
            mysql
            mysql-connector-java
            8.0.22
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    


相关配置文件:

server:
  port: 8099

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/skywalking?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: lj123456
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    properties:
      hibernate:
        format_sql: true

dao层配置:

public interface UserRepository extends JpaRepository, JpaSpecificationExecutor {
}

实体类:

@Entity
@Table(name = "t_user")
@Data
public class User implements Serializable {
    @Id
    private Integer id;
    private String name;
    private String address;
    private Integer age;
    private Date createTime;
    
}
1 Specifications单条件查询

测试类:

    @Test
    public void testSpeci(){
        Optional one = userRepository.findOne(new Specification() {
            
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
                Path name = root.get("name");
                Predicate predicate = cb.equal(name, "王美丽");
                return predicate;
            }
        });
        System.out.println(one.get());
    } 

输出结果:

 2 Specifications多条件查询
    @Test
    public void testSpeci2(){
        List all = userRepository.findAll(new Specification() {
            
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
                Path name = root.get("name");
                Predicate namePredicate = cb.equal(name, "王美丽");

                Path address = root.get("address");
                Predicate addressPredicate = cb.equal(address, "地球村");
                Predicate predicate = cb.or(namePredicate, addressPredicate);
                return predicate;
            }
        });
        System.out.println(all.size());
    } 

测试结果:

 3 Specifications模糊查询
    @Test
    public void testSpeci3(){
        List all = userRepository.findAll(new Specification() {
            
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
                Path name = root.get("name");
                Predicate namePredicate = cb.like(name.as(String.class), "王美丽");

                Path address = root.get("address");
                Predicate addressPredicate = cb.equal(address, "地球村");
                Predicate predicate = cb.or(namePredicate, addressPredicate);
                return predicate;
            }
        });
        System.out.println(all.size());
    } 

测试结果:

4  Specifications的分页查询
    @Test
    public void testSpeci3(){
        //当前页是从0开始的
        PageRequest pageRequest = PageRequest.of(0,1);
        Page page = userRepository.findAll(new Specification() {
            
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
                Path name = root.get("name");
                Predicate namePredicate = cb.like(name.as(String.class), "王美丽");

                Path address = root.get("address");
                Predicate addressPredicate = cb.equal(address, "地球村");
                Predicate predicate = cb.or(namePredicate, addressPredicate);
                return predicate;
            }
        },pageRequest);
        System.out.println("总记录数:"+page.getTotalElements());
        System.out.println("总页数:"+page.getTotalPages());
        List content = page.getContent();
        content.forEach(System.out::println);
    } 

测试结果:

 5 Specifications的排序分页查询
@Test
    public void testSpeci3(){
       

        //当前页是从0开始的
        PageRequest pageRequest =  PageRequest.of(0,1,Sort.by(Sort.Direction.DESC,"createTime"));
        Page page = userRepository.findAll(new Specification() {
            
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
                Path name = root.get("name");
                Predicate namePredicate = cb.like(name.as(String.class), "王美丽");

                Path address = root.get("address");
                Predicate addressPredicate = cb.equal(address, "地球村");
                Predicate predicate = cb.or(namePredicate, addressPredicate);
                return predicate;
            }
        },pageRequest);
        System.out.println("总记录数:"+page.getTotalElements());
        System.out.println("总页数:"+page.getTotalPages());
        List content = page.getContent();
    }
 

测试结果:

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5562953.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)