上一講講解了如何使用Spring Data JPA封裝一個(gè)自己的BaseRespoistory工廠耙饰,這個(gè)在實(shí)際開發(fā)中是非常有必要的俊犯,今天我們來進(jìn)一步封裝Spring Data JPA的查詢。
Spring Data JPA已經(jīng)幫助我們很大程度上簡(jiǎn)化了我們的查詢操作分瘦,我們甚至只要寫一個(gè)接口银锻,然后單純的寫一些方法就可以完成各式各樣的查詢酥筝,但是對(duì)于我們程序設(shè)計(jì)人員而言咖熟,總希望所有的查詢變得更加的簡(jiǎn)單方便圃酵,為了給程序人員進(jìn)行再一次的封裝,Spring Data JPA提供了Specification的方式進(jìn)行查詢馍管,在前面的內(nèi)容已經(jīng)演示過這種查詢了郭赐,但是,我們?cè)谑褂玫倪^程中發(fā)現(xiàn)這種查詢異常的繁瑣和復(fù)雜确沸,接下來的內(nèi)容就是我們有效的對(duì)Specification進(jìn)行封裝來快速實(shí)現(xiàn)一些簡(jiǎn)單的查詢操作捌锭。當(dāng)然如果涉及到更為復(fù)雜的操作,依然建議寫個(gè)方法來自己實(shí)現(xiàn)罗捎。
封裝自己的Specification的實(shí)現(xiàn)有很多種方法观谦,我這里只給出了相對(duì)簡(jiǎn)單的一種,而且并沒有考慮太復(fù)雜的查詢桨菜,個(gè)人感覺過于復(fù)雜的查詢還不如直接使用SQL或者HQL來處理方便坎匿,以下是幾個(gè)比較重要的類
/**
* Created by konghao on 2016/12/15.
* 操作符類,這個(gè)類中存儲(chǔ)了鍵值對(duì)和操作符號(hào)雷激,另外存儲(chǔ)了連接下一個(gè)條件的類型是and還是or
* 創(chuàng)建時(shí)通過 id>=7,其中id就是key,>=就是oper操作符替蔬,7就是value
* 特殊的自定義幾個(gè)操作符(:表示like %v%,b:表示v%,:b表示%v)
*/
public class SpecificationOperator {
/**
* 操作符的key屎暇,如查詢時(shí)的name,id之類
*/
private String key;
/**
* 操作符的value承桥,具體要查詢的值
*/
private Object value;
/**
* 操作符,自己定義的一組操作符根悼,用來方便查詢
*/
private String oper;
/**
* 連接的方式:and或者or
*/
private String join;
...../*省略了getter和setter*/
}
SpecificationOperator
表示操作符類凶异,用來確定查詢條件和值。
接下來創(chuàng)建SimpleSpecification
來實(shí)現(xiàn)Specification
接口挤巡,并且根據(jù)條件生成Specification
對(duì)象剩彬,因?yàn)樵谧詈蟛樵兊臅r(shí)候需要這個(gè)對(duì)象
package org.konghao.specification;
import org.springframework.data.jpa.domain.Specification;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;
/**
* Created by konghao on 2016/12/15.
*/
public class SimpleSpecification<T> implements Specification<T> {
/**
* 查詢的條件列表,是一組列表
* */
private List<SpecificationOperator> opers;
public SimpleSpecification(List<SpecificationOperator> opers) {
this.opers = opers;
}
@Override
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
int index = 0;
//通過resultPre來組合多個(gè)條件
Predicate resultPre = null;
for(SpecificationOperator op:opers) {
if(index++==0) {
resultPre = generatePredicate(root,criteriaBuilder,op);
continue;
}
Predicate pre = generatePredicate(root,criteriaBuilder,op);
if(pre==null) continue;
if("and".equalsIgnoreCase(op.getJoin())) {
resultPre = criteriaBuilder.and(resultPre,pre);
} else if("or".equalsIgnoreCase(op.getJoin())) {
resultPre = criteriaBuilder.or(resultPre,pre);
}
}
return resultPre;
}
private Predicate generatePredicate(Root<T> root,CriteriaBuilder criteriaBuilder, SpecificationOperator op) {
/*
* 根據(jù)不同的操作符返回特定的查詢*/
if("=".equalsIgnoreCase(op.getOper())) {
System.out.println(op.getKey()+","+op.getValue());
return criteriaBuilder.equal(root.get(op.getKey()),op.getValue());
} else if(">=".equalsIgnoreCase(op.getOper())) {
return criteriaBuilder.ge(root.get(op.getKey()), (Number)op.getValue());
} else if("<=".equalsIgnoreCase(op.getOper())) {
return criteriaBuilder.le(root.get(op.getKey()),(Number)op.getValue());
} else if(">".equalsIgnoreCase(op.getOper())) {
return criteriaBuilder.gt(root.get(op.getKey()),(Number)op.getValue());
} else if("<".equalsIgnoreCase(op.getOper())) {
return criteriaBuilder.lt(root.get(op.getKey()),(Number)op.getValue());
} else if(":".equalsIgnoreCase(op.getOper())) {
return criteriaBuilder.like(root.get(op.getKey()),"%"+op.getValue()+"%");
} else if("l:".equalsIgnoreCase(op.getOper())) {
return criteriaBuilder.like(root.get(op.getKey()),op.getValue()+"%");
} else if(":l".equalsIgnoreCase(op.getOper())) {
return criteriaBuilder.like(root.get(op.getKey()),"%"+op.getValue());
} else if("null".equalsIgnoreCase(op.getOper())) {
return criteriaBuilder.isNull(root.get(op.getKey()));
} else if("!null".equalsIgnoreCase(op.getOper())) {
return criteriaBuilder.isNotNull(root.get(op.getKey()));
} else if("!=".equalsIgnoreCase(op.getOper())) {
return criteriaBuilder.notEqual(root.get(op.getKey()),op.getValue());
}
return null;
}
}
SimpleSpecification
是核心類型矿卑,用來根據(jù)條件生成Specification對(duì)象喉恋,這個(gè)SimpleSpecification
直接存儲(chǔ)了具體的查詢條件。
最后我們創(chuàng)建一個(gè)SimpleSpecificationBuilder
來具體創(chuàng)建SimpleSpecification
,這里為了方便調(diào)用簡(jiǎn)單進(jìn)行了一下設(shè)計(jì)母廷。
package org.konghao.specification;
import org.springframework.data.jpa.domain.Specification;
import java.util.ArrayList;
import java.util.List;
/**
* Created by konghao on 2016/12/15.
*/
public class SimpleSpecificationBuilder<T> {
/**
* 條件列表
*/
private List<SpecificationOperator> opers;
/**
* 構(gòu)造函數(shù)轻黑,初始化的條件是and
*/
public SimpleSpecificationBuilder(String key,String oper,Object value) {
SpecificationOperator so = new SpecificationOperator();
so.setJoin("and");
so.setKey(key);
so.setOper(oper);
so.setValue(value);
opers = new ArrayList<SpecificationOperator>();
opers.add(so);
}
public SimpleSpecificationBuilder() {
opers = new ArrayList<SpecificationOperator>();
}
/**
* 完成條件的添加
* @return
*/
public SimpleSpecificationBuilder add(String key,String oper,Object value,String join) {
SpecificationOperator so = new SpecificationOperator();
so.setKey(key);
so.setValue(value);
so.setOper(oper);
so.setJoin(join);
opers.add(so);
return this;
}
/**
* 添加or條件的重載
* @return this,方便后續(xù)的鏈?zhǔn)秸{(diào)用
*/
public SimpleSpecificationBuilder addOr(String key,String oper,Object value) {
return this.add(key,oper,value,"or");
}
/**
* 添加and的條件
* @return
*/
public SimpleSpecificationBuilder add(String key,String oper,Object value) {
return this.add(key,oper,value,"and");
}
public Specification generateSpecification() {
Specification<T> specification = new SimpleSpecification<T>(opers);
return specification;
}
}
現(xiàn)在幾個(gè)比較重要的類以及實(shí)現(xiàn)琴昆,接下來看看具體的調(diào)用氓鄙。首先創(chuàng)建一個(gè)StudentRepository
的接口實(shí)現(xiàn)JpaSpecificationExecutor
/**
* Created by konghao on 2016/12/16.
* 該接口實(shí)現(xiàn)了上一節(jié)介紹的BaseRepository和JpaSpecificationExecutor
* JpaSpecificationExecutor可以通過findAll方法傳入SimpleSpecification來進(jìn)行查詢
*/
public interface StudentRepository extends BaseRepository<Student,Integer>,JpaSpecificationExecutor<Student> {
}
大家注意這個(gè)接口中沒有任何的查詢,基本就是一個(gè)空接口业舍,按照原來JPA的處理方式抖拦,我們需要為一些簡(jiǎn)單的查詢寫一些類似findByUsername
的方法∩ǎ現(xiàn)在我們已經(jīng)封裝了自己的SimpleSpecification
,我們來看看測(cè)試類如何調(diào)用态罪。
package org.konghao;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.konghao.model.Student;
import org.konghao.repository.StudentRepository;
import org.konghao.specification.SimpleSpecificationBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private StudentRepository studentRepository;
@Test
public void testFind() {
/**
* 這里的查詢表示id大于4或者name中包含a
* 現(xiàn)在我們發(fā)現(xiàn)在SimpleSpecificationBuilder的add或者addOr方法中返回this的好處了
*/
List<Student> stus = studentRepository.findAll(
new SimpleSpecificationBuilder("id",">",4)
.addOr("name",":","a")
.generateSpecification());
Assert.assertEquals(5,stus.size());
}
}
我們完成了一個(gè)不算太復(fù)雜的查詢噩茄,如果你原來認(rèn)為在接口中寫衍生查詢的方法太復(fù)雜,用現(xiàn)在這種方式是不是簡(jiǎn)單了許多呢向臀?好了巢墅,這一講就到這里,下一講我們可以結(jié)束Spring Data JPA了券膀。就差兩個(gè)非常簡(jiǎn)單的小知識(shí):分頁和事務(wù)處理君纫。、
本文的源代碼在這里:源代碼