Specifications dynamic query

Conditions inquiry

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class SpecTest {
    @Autowired
    private CustomerDao customerDao;

    //根据条件查询单个对象
    @Test
    public void testSpec(){
         // 案例:根据客户名称查询
         // 查询条件的构成:
         //      1.查询方式(CriteriaBuilder中)
         //      2.比较的属性名称(root中)
        Specification<Customer> spec = new Specification<Customer>() {
            public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //1.获取比较的属性:
                Path<Object> custName = root.get("custName");
                //参数1:需要比较的属性(Path对象) 参数2:当前需要比较的取值
                //equal是进行精准匹配(比较的属性,比较的属性的取值)
                Predicate predicate = cb.equal(custName, "Lee");
                return predicate;
            }
        };
        Customer customer = customerDao.findOne(spec);
        System.out.println(customer);
    }

    //多条件查询
    //案例:根据客户名称和所属行业查询
    @Test
    public void  testSpec1(){
        Specification<Customer> spec = new Specification<Customer>() {
            public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //获取属性
                //1.获取客户名
                Path<Object> custName = root.get("custName");
                //2.获取所属行业
                Path<Object> custIndustry = root.get("custIndustry");
                //构造查询
                //1.构造客户名的精准匹配查询
                Predicate p1 = cb.equal(custName,"传智播客");
                //2.构造所属行业的精准匹配查询
                Predicate p2 = cb.equal(custIndustry, "体育");
                //3.将以上两个查询联系起来 (关系:与  或)
                //cb.and:以与的形式拼接多个条件查询 cb.or: 以或的形式拼接多个条件查询
                Predicate predicate = cb.and(p1, p2);
                return predicate;
            }
        };
        Customer customer = customerDao.findOne(spec);
        System.out.println(customer);
    }

    //案例:完成根据客户名称的模糊匹配,返回客户列表
    // equal:直接得到path对象,然后进行比较即可
    // gt,lt,ge,le,like:得到path对象,根据path对象指定比较的参数类型,然后再进行比较
    //       指定参数类型的方法:path.as(类型的字节码对象)
    @Test
    public void testSpec2(){
        Specification<Customer> spec = new Specification<Customer>() {
            public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //获取属性
                //1.获取客户名
                Path<Object> custName = root.get("custName");
                //构造查询
                //1.指定比较参数的类型
                Expression<String> expression = custName.as(String.class);
                //1.构造客户名的模糊匹配的查询
                Predicate predicate = cb.like(expression,"传智播客%");
                return predicate;
            }
        };
    //   List<Customer> list = customerDao.findAll(spec);
    //   for(Customer customer : list){
    //       System.out.println(customer); 
    //   }
        //添加排序
        //创建排序对象,构造方法实例化对象
        //参数1:排序的顺序(倒序:Sort.Direction.DESC,正序:Sort.Direction.ASC)
        //参数2:排序的属性名称;
        Sort sort = new Sort(Sort.Direction.DESC,"custId");
        List<Customer> list = customerDao.findAll(spec, sort);
        for(Customer customer : list){
            System.out.println(customer);
        }
    }

Paging query

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class SpecTest {

    @Autowired
    private CustomerDao customerDao;

    //分页查询
    // Specification:查询条件
    // Pageable:分页参数
    //	 分页参数:查询的页码,每页查询的条数
    // 分页查询的方法:
    //  findAll(Specification,Pageable):带有条件的分页
    //  findAll(Pageable):不带条件的分页
    // 返回:Page对象(Spring Data Jpa为我们封装好的pageBean对象,可以获取数据列表,总条数)
    @Test
    public void testSpec3(){
        //PageRequest是Pageable的实现类
        //参数1:当前查询的页数(从 0 开始)   参数2:每页查询的数量
        Pageable page = new PageRequest(0,2);
        Page<Customer> pages = customerDao.findAll(page);
        // pages.getContext();  得到数据集合列表
        // pages.getTotalPages(); 得到总条数
        // pages.getTotalElements();  得到总页数
        List<Customer> list = pages.getContent();
        for(Customer customer : list){
            System.out.println(customer);
        }
    }
}

Published 163 original articles · won praise 8 · views 8932

Guess you like

Origin blog.csdn.net/wait_13/article/details/104332847