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.fndsoft.springDataJpaProject.pojo.Student;
import com.fndsoft.springDataJpaProject.repository.StudentJpaSpecificationExcutor;
public class StudentJpaSpecificationExcutorTest {
private ApplicationContext ctx = null;
private StudentJpaSpecificationExcutor studentJpaSpecificationExcutor = null;
@Before
public void setUp(){
ctx = new ClassPathXmlApplicationContext("beans.xml");
studentJpaSpecificationExcutor = ctx.getBean(StudentJpaSpecificationExcutor.class);
System.out.println("setup");
}
@After
public void tearDown(){
ctx = null;
System.out.println("teardown");
}
//结合JpaSpecificationExcutor接口
//分页加排序加条件age>23
@Test
public void testPageAndSort(){
System.out.println("start");
Order order = new Order(Direction.ASC,"sage");
Sort sort = new Sort(order);
Pageable pageable = new PageRequest(0,2,sort);
Specification<Student> specification = new Specification<Student>() {
//toPredicate就是查询条件
//root根对象表示实体类,要查询的类型,query所需要添加查询条件,cb构建Predicate
@Override
public Predicate toPredicate(Root<Student> root,
CriteriaQuery<?> query,
CriteriaBuilder cb) {
// TODO Auto-generated method stub
Path path = root.get("sage");//能获取到Student的sage的路径
return cb.gt(path, 23);
}
};
Page<Student> page = studentJpaSpecificationExcutor.findAll(specification, pageable);
System.out.println("查询的总页数:"+page.getTotalPages());
System.out.println("查询的记录数:"+page.getTotalElements());
System.out.println("查询当前的第几页:"+(page.getNumber()+1));
System.out.println("查询当前页面的集合:"+page.getContent());
System.out.println("查询当前页面的记录数:"+page.getNumberOfElements());
System.out.println("started");
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
springDataJpaProject.zip (52个子文件)
springDataJpaProject
pom.xml 2KB
target
test-classes
StudentJpaRepositoryTest.class 3KB
UserServiceTest.class 3KB
dataSourceTest.class 994B
StudentPagingAndSortingRepositoryTest.class 4KB
com
fndsoft
springDataJpaProject
AppTest.class 651B
StudentJpaSpecificationExcutorTest.class 4KB
StudentJpaSpecificationExcutorTest$1.class 2KB
UserRepositoryTest.class 3KB
classes
beans.xml 3KB
com
fndsoft
springDataJpaProject
repository
StudentJpaRepository.class 390B
UserRepository.class 1KB
StudentJpaSpecificationExcutor.class 597B
StudentPagingAndSortingRepository.class 434B
App.class 579B
pojo
User.class 1KB
Student.class 1KB
service
UserService.class 2KB
db.properties 159B
springDataJpaProject.userlibraries 66B
.settings
org.maven.ide.eclipse.prefs 218B
org.eclipse.jdt.core.prefs 664B
src
test
test.eml 308B
test.iml 475B
.project 342B
.classpath 372B
java
com
fndsoft
springDataJpaProject
AppTest.java 660B
StudentJpaSpecificationExcutorTest.java 3KB
dataSourceTest.java 1KB
StudentJpaRepositoryTest.java 1KB
UserServiceTest.java 2KB
UserRepositoryTest.java 2KB
StudentPagingAndSortingRepositoryTest.java 3KB
main
resources
beans.xml 3KB
db.properties 159B
main.eml 243B
.project 342B
.classpath 301B
main.iml 424B
java
com
fndsoft
springDataJpaProject
repository
StudentPagingAndSortingRepository.java 298B
UserRepository.java 2KB
StudentJpaSpecificationExcutor.java 467B
StudentJpaRepository.java 300B
pojo
User.java 812B
Student.java 800B
App.java 195B
service
UserService.java 840B
.idea
misc.xml 258B
modules.xml 375B
workspace.xml 28KB
inspectionProfiles
.project 750B
.classpath 619B
共 52 条
- 1
资源评论
呢喃北上
- 粉丝: 114
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功