十 JpaSpecificationExecutor接口

一 单条件查询

1.1 创建dao接口

package com.bjsxt.dao;

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

import com.bjsxt.pojo.Users;

/**
 * JpaSpecificationExecutor:不能单独使用,需要配合jpa中的其他接口一起使用
 * @author guozi
 *
 */
public interface UserDaoJpaSpecifictionExecutor extends JpaRepository, JpaSpecificationExecutor{

}

1.2 创建测试类

package com.bjsxt.dao;

import java.util.List;

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

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.bjsxt.pojo.Users;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class UserDaoJpaSpecifictionExecutorTest {
	
	@Autowired
	private UserDaoJpaSpecifictionExecutor userDaoJpaSpecifictionExecutor;

	/**
	 *	Hibernate: select users0_.id as id1_0_, users0_.age as age2_0_, users0_.user_name as user_nam3_0_ from ts_users users0_ where users0_.user_name=?
		Users(id=5, userName=路飞-海贼王, age=18)
	 */
	@Test
	public void testFindAllSpecificationOfT() {
		Specification spec = new Specification() {

			/**
			 * Predicate:定义了查询条件
			 * Root root:根对象,封装了查询条件的对象
			 * CriteriaQuery query:定义一个基本的查询,一般不使用
			 * CriteriaBuilder cb:创建一个查询的条件
			 */
			@Override
			public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
				return cb.equal(root.get("userName"), "路飞-海贼王");
			}
		};
		List userList = this.userDaoJpaSpecifictionExecutor.findAll(spec);
		for (Users users : userList) {
			System.out.println(users);
		}
	}

}

二 多条件查询

	/**
	 * 多条件查询方式1
	 * 需求:使用用户名及年龄查询
	 * 	Hibernate: select users0_.id as id1_0_, users0_.age as age2_0_, users0_.user_name as user_nam3_0_ from ts_users users0_ where users0_.user_name=? and users0_.age=18
		Users(id=5, userName=路飞-海贼王, age=18)
	 */
	@Test
	public void test2() {
		Specification spec = new Specification() {
 			@Override
			public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
 				Predicate predicateUserName = cb.equal(root.get("userName"), "路飞-海贼王");
 				Predicate predicateAge = cb.equal(root.get("age"), 18);
 				List list = new ArrayList<>();
 				list.add(predicateUserName);
 				list.add(predicateAge);
 				// 此时条件直接是没有任何关系的
 				Predicate[] arr = new Predicate[list.size()];
				return cb.and(list.toArray(arr));
			}
		};
		List userList = this.userDaoJpaSpecifictionExecutor.findAll(spec);
		for (Users users : userList) {
			System.out.println(users);
		}
	}
	/**
	 * 多条件查询方式2
	 * 需求:使用用户名及年龄或id为10的查询
	 * 	Hibernate: select users0_.id as id1_0_, users0_.age as age2_0_, users0_.user_name as user_nam3_0_ from ts_users users0_ where users0_.user_name=? and users0_.age=18 or users0_.id=10
		Users(id=5, userName=路飞-海贼王, age=18)
		Users(id=10, userName=娜美, age=18)
	 */
	@Test
	public void test3() {
		Specification spec = new Specification() {
			@Override
			public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
				return cb.or(
						cb.and(cb.equal(root.get("userName"), "路飞-海贼王"), cb.equal(root.get("age"), 18)),
						cb.equal(root.get("id"), 10)
						);
			}
		};
		List userList = this.userDaoJpaSpecifictionExecutor.findAll(spec);
		for (Users users : userList) {
			System.out.println(users);
		}
	}

三 分页处理

	/**
	 * 分页处理
	 * 需求:查询年龄在17-30的用户,并分页处理
	 * 	Hibernate: select count(users0_.id) as col_0_0_ from ts_users users0_ where users0_.age between 17 and 30
		Hibernate: select users0_.id as id1_0_, users0_.age as age2_0_, users0_.user_name as user_nam3_0_ from ts_users users0_ where users0_.age between 17 and 30 limit ?, ?
		总记录数:7
		总页数:4
		Users(id=6, userName=索罗-二把手, age=28)
		Users(id=9, userName=乌索普, age=27)
	 */
	@Test
	public void test4() {
		Specification spec = new Specification() {
			@Override
			public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
				return cb.between(root.get("age"), 17, 30);
			}
		}; 
		Pageable pageable = new PageRequest(1, 2);
		Page userPage = this.userDaoJpaSpecifictionExecutor.findAll(spec, pageable);
		System.out.println("总记录数:" + userPage.getTotalElements());
		System.out.println("总页数:" + userPage.getTotalPages());
		List userList = userPage.getContent();
		for (Users users : userList) {
			System.out.println(users);
		}
	}

四 排序处理

	/**
	 * 排序处理
	 * 需求:查询年龄在17-30的用户,并按age降序排序,如果age相同则按id升序排序
	 * 	Hibernate: select users0_.id as id1_0_, users0_.age as age2_0_, users0_.user_name as user_nam3_0_ from ts_users users0_ where users0_.age between 17 and 30 order by users0_.age desc, users0_.id asc
		Users(id=6, userName=索罗-二把手, age=28)
		Users(id=9, userName=乌索普, age=27)
		Users(id=12, userName=弗兰奇, age=20)
		Users(id=11, userName=罗宾, age=19)
		Users(id=4, userName=??, age=18)
		Users(id=5, userName=路飞-海贼王, age=18)
		Users(id=10, userName=娜美, age=18)
	 */
	@Test
	public void test5() {
		Specification spec = new Specification() {
			@Override
			public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
				return cb.between(root.get("age"), 17, 30);
			}
		}; 
		Order orderAge = new Order(Direction.DESC, "age");
		Order orderId = new Order(Direction.ASC, "id");
		Sort sort = new Sort(orderAge, orderId);
		List userList = this.userDaoJpaSpecifictionExecutor.findAll(spec, sort);
		for (Users users : userList) {
			System.out.println(users);
		}
	}

五 分页排序处理

	/**
	 * 分页排序处理
	 * 需求:查询年龄在17-30的用户,按age降序排序,如果age相同则按id升序排序,并分页处理
	 * 	Hibernate: select count(users0_.id) as col_0_0_ from ts_users users0_ where users0_.age between 17 and 30
		Hibernate: select users0_.id as id1_0_, users0_.age as age2_0_, users0_.user_name as user_nam3_0_ from ts_users users0_ where users0_.age between 17 and 30 order by users0_.age desc, users0_.id asc limit ?
		总记录数:7
		总页数:3
		Users(id=6, userName=索罗-二把手, age=28)
		Users(id=9, userName=乌索普, age=27)
		Users(id=12, userName=弗兰奇, age=20)
	 */
	@Test
	public void test6() {
		Specification spec = new Specification() {
			@Override
			public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
				return cb.between(root.get("age"), 17, 30);
			}
		}; 
		Order orderAge = new Order(Direction.DESC, "age");
		Order orderId = new Order(Direction.ASC, "id");
		Sort sort = new Sort(orderAge, orderId);
		Pageable pageable = new PageRequest(0, 3, sort);
		Page userPage = this.userDaoJpaSpecifictionExecutor.findAll(spec, pageable);
		System.out.println("总记录数:" + userPage.getTotalElements());
		System.out.println("总页数:" + userPage.getTotalPages());
		List userList = userPage.getContent();
		for (Users users : userList) {
			System.out.println(users);
		}
	}

 

你可能感兴趣的:(Spring,Data)