多条件分页查询spring jpa Specification应用

一,定义实体创建表。

二,定义Specification实体实现Specification接口,实现多条件查询。

三,dao层实现PagingAndSortingRepository和JpaSpecificationExecutor。

四,应用层实现。

一,



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

@Entity
@Table(name = ObtainApplicant.TableName)
public class ObtainApplicant {

	
	public static final String TableName = "MYTEST";

	private String gid;

	
	private String sqrmc;

	
	private String sqrlx;

	
	private String xb;

	
	private String dwxz;

	
	private String zjhm;

	
	private String zjzl;

	
	private String yb;

	
	private String dzyj;

	
	private String lxdh;

	
	private String lxr;

	
	private String txdz;

	
	private String gzdw;

	
	private String hjszss;

	
	private String gjdq;

	
	private String sshy;

	
	private String dljg;

	
	private String dlrdh;

	
	private String dlrmc;

	
	private String frdh;

	
	private String frmc;

	
	private String qlbl;

	
	private String gyqk;
	
	private String dlrzjhm;
	
	private String userId;

	@Id
	@Column(name="GID",length=36)
	public String getGid() {
		return gid;
	}

	public void setGid(String gid) {
		this.gid = gid;
	}
	@Column(name="SQRMC",length=100)
	public String getSqrmc() {
		return sqrmc;
	}

	public void setSqrmc(String sqrmc) {
		this.sqrmc = sqrmc;
	}

	@Column(name="SQRLX",length=100)
	public String getSqrlx() {
		return sqrlx;
	}

	public void setSqrlx(String sqrlx) {
		this.sqrlx = sqrlx;
	}
	@Column(name="XB",length=4)
	public String getXb() {
		return xb;
	}

	public void setXb(String xb) {
		this.xb = xb;
	}

	@Column(name="DWXZ",length=100)
	public String getDwxz() {
		return dwxz;
	}

	public void setDwxz(String dwxz) {
		this.dwxz = dwxz;
	}
	@Column(name="ZJHM",length=100)
	public String getZjhm() {
		return zjhm;
	}

	public void setZjhm(String zjhm) {
		this.zjhm = zjhm;
	}
	@Column(name="ZJZL",length=100)
	public String getZjzl() {
		return zjzl;
	}

	public void setZjzl(String zjzl) {
		this.zjzl = zjzl;
	}

	@Column(name="YB",length=50)
	public String getYb() {
		return yb;
	}

	public void setYb(String yb) {
		this.yb = yb;
	}

	@Column(name="DZYJ",length=50)
	public String getDzyj() {
		return dzyj;
	}

	public void setDzyj(String dzyj) {
		this.dzyj = dzyj;
	}

	@Column(name="LXDH",length=50)
	public String getLxdh() {
		return lxdh;
	}

	public void setLxdh(String lxdh) {
		this.lxdh = lxdh;
	}

	@Column(name="LXR",length=50)
	public String getLxr() {
		return lxr;
	}

	public void setLxr(String lxr) {
		this.lxr = lxr;
	}

	@Column(name="TXDZ",length=100)
	public String getTxdz() {
		return txdz;
	}

	public void setTxdz(String txdz) {
		this.txdz = txdz;
	}
	@Column(name="GZDW",length=100)
	public String getGzdw() {
		return gzdw;
	}

	public void setGzdw(String gzdw) {
		this.gzdw = gzdw;
	}
	@Column(name="HJSZSS",length=50)
	public String getHjszss() {
		return hjszss;
	}

	public void setHjszss(String hjszss) {
		this.hjszss = hjszss;
	}
	@Column(name="GJDQ",length=100)
	public String getGjdq() {
		return gjdq;
	}

	public void setGjdq(String gjdq) {
		this.gjdq = gjdq;
	}
	@Column(name="SSHY",length=50)
	public String getSshy() {
		return sshy;
	}

	public void setSshy(String sshy) {
		this.sshy = sshy;
	}
	@Column(name="DLJG",length=50)
	public String getDljg() {
		return dljg;
	}

	public void setDljg(String dljg) {
		this.dljg = dljg;
	}
	@Column(name="DLRDH",length=50)
	public String getDlrdh() {
		return dlrdh;
	}

	public void setDlrdh(String dlrdh) {
		this.dlrdh = dlrdh;
	}
	@Column(name="DLRMC",length=50)
	public String getDlrmc() {
		return dlrmc;
	}

	public void setDlrmc(String dlrmc) {
		this.dlrmc = dlrmc;
	}
	@Column(name="FRDH",length=50)
	public String getFrdh() {
		return frdh;
	}

	public void setFrdh(String frdh) {
		this.frdh = frdh;
	}
	@Column(name="FRMC",length=50)
	public String getFrmc() {
		return frmc;
	}

	public void setFrmc(String frmc) {
		this.frmc = frmc;
	}
	@Column(name="DLBL",length=50)
	public String getQlbl() {
		return qlbl;
	}

	public void setQlbl(String qlbl) {
		this.qlbl = qlbl;
	}
	@Column(name="GYQK",length=50)
	public String getGyqk() {
		return gyqk;
	}

	public void setGyqk(String gyqk) {
		this.gyqk = gyqk;
	}
	@Column(name="DLRZJHM",length=50)
	public String getDlrzjhm() {
		return dlrzjhm;
	}

	public void setDlrzjhm(String dlrzjhm) {
		this.dlrzjhm = dlrzjhm;
	}
	@Column(name="USERID",length=100)
	public String getUserId()
	{
		return userId;
	}

	public void setUserId(String userId)
	{
		this.userId = userId;
	}
	
	

}

二,实现Specification接口时要对应实体中有的属性。然后定义你想要查询那些条件,加入到该类的构造方法。


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;

import com.mytest.ObtainApplicant;



public class IObtainApplicantSpecification implements Specification
{
	
	String userId="";
	String sqrmc="";
	String dlrmc="";
  String zjhm="";
  String dlrzjhm="";
	
  
  

	public IObtainApplicantSpecification(String userId, String sqrmc, String dlrmc, String zjhm, String dlrzjhm)
	{
		super();
		this.userId = userId;
		this.sqrmc = sqrmc;
		this.dlrmc = dlrmc;
		this.zjhm = zjhm;
		this.dlrzjhm = dlrzjhm;
	}




	@Override
	public 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.equal(root.get("sqrmc"),sqrmc));
   }
		 if(!CheckUtil.isNullorEmpty(dlrmc)){
       predicates.add(cb.equal(root.get("dlrmc"),dlrmc));
   }
		 if(!CheckUtil.isNullorEmpty(zjhm)){
       predicates.add(cb.equal(root.get("zjhm"),zjhm));
   }
		 if(!CheckUtil.isNullorEmpty(dlrzjhm)){
       predicates.add(cb.equal(root.get("dlrzjhm"),dlrzjhm));
   }
			return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
	}

}

三,实现dao.


import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

import com.mytest.ObtainApplicant;


 @Repository(IObtainApplicantDAo.BeanName) 
 public interface IObtainApplicantDAo extends PagingAndSortingRepository,JpaSpecificationExecutor{

	 
	 
	 public static final String BeanName = "ObtainApplicantDAo";
	  
 }
四,应用实现
@Service(IObtainApplicantInterface.SERVICE_BEAN_NAME)
@Transactional
public class ObtainApplicantService implements IObtainApplicantInterface{
	
	
	@Autowired
	private IObtainApplicantDAo buildingTableObtainApplicantDAo;
	@SuppressWarnings("unchecked")
	@Override
	public void  getObtainApplicant(PageQueryData> pageQueryData, String userId,String sqrmc, String dlrmc,String sqrzj,String dlrzj, Integer page,Integer rows) {
		Pageable pageable = new PageRequest(page-1, rows);
		Page resutls =buildingTableObtainApplicantDAo.findAll(new IObtainApplicantSpecification(userId, sqrmc, dlrmc, sqrzj, dlrzj) ,pageable);
		long total=resutls.getTotalElements();
		pageQueryData.setTotal(((int) total));
		@SuppressWarnings("rawtypes")
		List list=Lists.newArrayList(resutls.iterator());
		pageQueryData.setQueryList(list);
	}
}



	 

你可能感兴趣的:(java)