spring data 接口之 JpaRepository,JpaSpecificationExecutor

实体类

package com.wangh.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
//可以使用name属性指定表名
@Entity(name="Person")
public class Person {

    private Integer id;
    private String name;
    private Integer age;

    @GeneratedValue
    @Id
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
    }


}

接口

package com.wangh.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import com.wangh.model.Person;
/**
 * JpaSpecificationExecutor 查询条件、分页和排序接口.
 * JpaRepository 继承于 PagingAndSortingRepository 接口, 拥有PagingAndSortingRepository 的所有方法,
 * 而JpaSpecificationExecutor 不属于Repository 体系。由于JpaSpecificationExecutor 并不继承repository 接口,
 * 所以它不能单独使用,只能和jpa Repository 一起用。
* @ClassName: PersonJpaSpecifactionRepository
* @Description: TODO
* @author Wanghao
* @date 2017年6月13日 上午9:25:45
 */
public interface PersonJpaSpecifactionRepository extends JpaSpecificationExecutor<Person>,JpaRepository<Person, Integer>{

}

service

package com.wangh.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wangh.model.Person;
import com.wangh.repository.PersonCrudRepository;
import com.wangh.repository.PersonJpaSpecifactionRepository;
import com.wangh.repository.PersonPagingAndSortRepository;

@Service
public class PersonService {

@Resource
    private PersonJpaSpecifactionRepository personJpaSpecifactionRepository;
        /**
     * 查询
    * @param spec 查询条件
    * @param pageable  分页排序
    * @return
    * Page
    * @author Wanghao
     */

    public Page query(Specification spec, Pageable pageable){
        return personJpaSpecifactionRepository.findAll(spec, pageable);
    }
}

测试类

package springdata;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.Specification;

import com.wangh.model.Person;
import com.wangh.service.PersonService;

public class PersonRepositoryTest {

    private ApplicationContext ctx = null;
    private PersonService psv = null;

    @Before
    public void before(){
        ctx = new ClassPathXmlApplicationContext("spring.xml");
        psv = ctx.getBean(PersonService.class);
        System.out.println("before");
    }

    @After
    public void after(){
        ctx = null;
        psv = null;
        System.out.println("after");
    }
    /**
     * 
    * 按id降序并且age<15,分页
    * @author Wangh
     */
    @Test
    public void query(){
        Specification spec = new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
                Path path = root.get("age");
                return cb.lt(path, 15);
            }
        };
        Order order = new Order(Direction.DESC, "id");
        Sort sort = new Sort(order);
        Pageable pageable = new PageRequest(0, 3, sort);
        Page page = psv.query(spec, pageable);

        System.out.println("当前页码上行数:"+page.getNumberOfElements());
        System.out.println("当前第几页:"+page.getNumber()+1);
        System.out.println("总数:"+page.getTotalElements());
        System.out.println("总页数:"+page.getTotalPages());
        System.out.println("当前页内容:"+page.getContent());
    }
}

测试结果

spring data 接口之 JpaRepository,JpaSpecificationExecutor_第1张图片

你可能感兴趣的:(springboot)