DAO:
パッケージcom.tensquare.base.dao。
輸入com.tensquare.base.pojo.Lable。
輸入org.springframework.data.jpa.repository.JpaRepository;
輸入org.springframework.data.jpa.repository.JpaSpecificationExecutor。
パブリックインターフェースLableDaoはJpaRepository <Lable、文字列>、延びJpaSpecificationExecutor <Lable> {
}を
サービス:
/ **
*単一のメソッド呼び出しを容易にするために、抽出された動的条件は、
* @param
* @return
* /
プライベート仕様<Lable> createSpecification(Lable Lable){
(新しい新しい仕様<Lable>を返す){
@Override
公共toPredicate述語(ルート<?> <Lable>根、CriteriaQueryクエリ、CriteriaBuilder CB){
一時的に格納するためのコレクション//クエリ
のArrayList <述語>リスト=新しい新しいのArrayListを<>();
IF(lable.getLabelname()= NULL &&「! ".equals(lable.getLabelname())){
述語predicateA = cb.like(root.get("ラベル名")、" % "+ lable.getLabelname()+" %「); //ラベル名XXX様% %
List.add(predicateA)。
}
(!lable.getState()= NULL && "は"。等しい(lable.getState())){場合
述述語= cb.equal(root.get( "状態")、lable.getState()); //状態= XXX
list.add(述語)。
}
//最终将查询条件拼好然后返す
述語[]述語=新しい述語〔はlist.size()]。
戻りcb.and(list.toArray(述語)); //ここ%のXXX%及び状態= XXXのようなラベル名
}
}。
}
不分页:
公共の一覧<Lable> findSearch(lable Lable){
//春データJPA使用JpaSpecificationExecutor构建条件查询
lableDao.findAll(createSpecification(lable))を返します。
}
分页:
パブリックページ<Lable> pageQuery(INT PAGENUM、INTサイズ、Lable lable){
//封装分页对象
ページング可能ページング= PageRequest.of(PAGENUM - 1、サイズ)。
lableDao.findAll(createSpecification(lable)、ページング可能)を返します。
}
コントローラ。
@RequestMapping(値= "/検索"、メソッド= RequestMethod.POST) |
|