栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Specifications动态查询

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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();
    }
 

测试结果:

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号