Specification 分组查询

一,定义dao继承JpaSpecificationExecutor。

二,实现Specification接口。

下面给出示列:

1,实体:
    

mport javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name = Person.TableName)
public class Person
{
	
  /**
   * 数据库表名.
   */
  public static final String TableName = "PERSON";
  
  private String rid;
  
	private String name;
	
	private Integer age;
	
	private String address;

	
	
	 @Id
	 @Column(name = "RID")
	public String getRid()
	{
		return rid;
	}

	public void setRid(String rid)
	{
		this.rid = rid;
	}

	@Column(name="NAME")
	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	@Column(name="AGE")
	public Integer getAge()
	{
		return age;
	}

	public void setAge(Integer age)
	{
		this.age = age;
	}

	@Column(name="ADDRESS")
	public String getAddress()
	{
		return address;
	}

	public void setAddress(String address)
	{
		this.address = address;
	}

	public static String getTablename()
	{
		return TableName;
	}
	
	
}


2,实现dao

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;
@Repository(PersonDao.BeanName) 
public interface PersonDao extends PagingAndSortingRepository,JpaSpecificationExecutor{

//Spring注入bean名称。
	 
	 public static final String BeanName = "PersonDao";
	
}

3,实现Specification接口:

    注意在root取值时,应该对应实体get方法小写第一个字母。

import java.util.ArrayList;
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.springframework.data.jpa.domain.Specification;
public class IPersonSpecification implements Specification
{

	@SuppressWarnings("unchecked")
	@Override
	public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb)
	{
		@SuppressWarnings("rawtypes")
		List eList=new ArrayList<>();
		eList.add(root.get("name"));
		eList.add(root.get("age"));

		query.groupBy(eList);
		 query.having(cb.gt( cb.count(root), 1));
		return query.getRestriction();
	}

}


多条件查询:

import java.util.ArrayList;
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.springframework.data.jpa.domain.Specification;


public class testSpecification implements Specification
{
	
	String userId="";
	String sqrmc="";

	public testSpecification (String userId, String sqrmc){
super();this.userId = userId;this.sqrmc = sqrmc;
}
@Overridepublic 
Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb){
List predicates = new ArrayList(); 
if(!CheckUtil.isNullorEmpty(userId)){
 predicates.add(cb.equal(root.get("userId"),userId)); 
} 
if(!CheckUtil.isNullorEmpty(sqrmc)){ 
predicates.add(cb.like(root.get("sqrmc"),"%"+sqrmc+"%")); 
} 
}
return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
}}



 
  

分组查询对应service:

List list=personDao.findAll(new IPersonSpecification());

多条件查询对应:

Pageable pageable = new PageRequest(page-1, rows);
Page resutls =buildingTableObtainApplicantDAo.findAll(new IObtainApplicantSpecification(userId, sqrmc, dlrmc, sqrzj, dlrzj) ,pageable);





你可能感兴趣的:(java)