Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does JPA use the criteria simple query tool class?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how JPA uses the criteria simple query tool class. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Before using jpa to write a conditional filter query data as follows, only to know that the slag, is a table, according to the front end to filter the data, written as follows

First of all, it is judged that there are so many parameters coming from the front end, and now it is found that it is the slag in the slag, and it is also time-consuming, and it can be done quickly with criteria.

First create the class and implement the Specification interface 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 ExpandCriteria implements Specification {private List criterions = new ArrayList () Public Predicate toPredicate (Root root, CriteriaQuery query, CriteriaBuilder builder) {if (! criterions.isEmpty ()) {List predicates = new ArrayList (); for (ExpandCriterion c: criterions) {predicates.add (c.toPredicate (root, query,builder)) } / / combine all conditions with and if (predicates.size () > 0) {return builder.and (predicates.toArray (new Predicate [predicates.size ()]));}} return builder.conjunction () } / * add a simple conditional expression * @ Methods Name add * @ Create In 2012-2-8 By lee * @ param expression0 void * / public void add (ExpandCriterion criterion) {if (criterion criteria null) {criterions.add (criterion) }} public static void main (String [] args) {/ / use example Demo// Criteria c = new Criteria (); / / c.add (Restrictions.like ("code", searchParam.getCode (), true)); / / c.add (Restrictions.eq ("level", searchParam.getLevel (), false)) / / c.add (Restrictions.eq ("mainStatus", searchParam.getMainStatus (), true)); / c.add (Restrictions.eq ("flowStatus", searchParam.getFlowStatus (), true)); / / c.add (Restrictions.eq ("createUser.userName", searchParam.getCreateUser (), true)); / / c.add (Restrictions.lte ("submitTime", searchParam.getStartSubmitTime (), true)) / / c.add (Restrictions.gte ("submitTime", searchParam.getEndSubmitTime (), true)); / c.add (Restrictions.eq ("needFollow", searchParam.getIsfollow (), true)); / / c.add (Restrictions.ne ("flowStatus", searchParam.getMainStatus () true)); / / c.add (Restrictions.in ("solveTeam.code", teamCodes, true)) / / repository.findAll (c);}} New ExpandCriterion interface import javax.persistence.criteria.CriteriaBuilder;import javax.persistence.criteria.CriteriaQuery;import javax.persistence.criteria.Predicate;import javax.persistence.criteria.Root; public interface ExpandCriterion {public enum Operator {EQ, NE, LIKE, GT, LT, GTE, LTE, AND, OR} public Predicate toPredicate (Root root, CriteriaQuery query, CriteriaBuilder builder);}

Create a new Restrictions.java

Import java.util.Collection; import org.springframework.util.StringUtils; import com.sll.iot.dao.base.criteria.ExpandCriterion.Operator; public class Restrictions {/ * equals * @ param fieldName * @ param value * @ param ignoreNull * @ return * / public static SimpleExpression eq (String fieldName, Object value, boolean ignoreNull) {if (StringUtils.isEmpty (value)) return null Return new Simple_Expression (fieldName, value, Operator.EQ);} / * is not equal to * @ param fieldName * @ param value * @ param ignoreNull * @ return * / public static SimpleExpression ne (String fieldName, Object value, boolean ignoreNull) {if (StringUtils.isEmpty (value)) return null Return new Simple_Expression (fieldName, value, Operator.NE);} / * * Fuzzy matching * @ param fieldName * @ param ignoreNull * @ return * / public static SimpleExpression like (String fieldName, String value, boolean ignoreNull) {if (StringUtils.isEmpty (value)) return null; return new Simple_Expression (fieldName, value, Operator.LIKE) } / * greater than * @ param fieldName * @ param value * @ param ignoreNull * @ return * / public static SimpleExpression gt (String fieldName, Object value, boolean ignoreNull) {if (StringUtils.isEmpty (value)) return null; return new Simple_Expression (fieldName, value, Operator.GT) } / * * less than * @ param fieldName * @ param value * @ param ignoreNull * @ return * / public static SimpleExpression lt (String fieldName, Object value, boolean ignoreNull) {if (StringUtils.isEmpty (value)) return null; return new Simple_Expression (fieldName, value, Operator.LT) } / * * greater than or equal to * @ param fieldName * @ param value * @ param ignoreNull * @ return * / public static SimpleExpression lte (String fieldName, Object value, boolean ignoreNull) {if (StringUtils.isEmpty (value)) return null; return new Simple_Expression (fieldName, value, Operator.GTE) } / * * less than or equal to * @ param fieldName * @ param value * @ return * / public static SimpleExpression gte (String fieldName, Object value, boolean ignoreNull) {if (StringUtils.isEmpty (value)) return null; return new Simple_Expression (fieldName, value, Operator.LTE) } / * * and * @ param criterions * @ return * / public static LogicalExpression and (ExpandCriterion... Criterions) {return new Logical_Expression (criterions, Operator.AND);} / * * or * @ param criterions * @ return * / public static LogicalExpression or (ExpandCriterion... Criterions) {return new Logical_Expression (criterions, Operator.OR);} / * is included in * @ param fieldName * @ param value * @ return * / @ SuppressWarnings ("rawtypes") public static LogicalExpression in (String fieldName, Collection value, boolean ignoreNull) {if (ignoreNull&& (value==null | | value.isEmpty () {return null } SimpleExpression [] ses = new SimpleExpression [value.size ()]; int iTun0; for (Object obj: value) {ses [I] = new Simple_Expression (fieldName,obj,Operator.EQ); iTunes;} return new Logical_Expression (ses,Operator.OR);}

Create a new SimpleExpression.java

Import javax.persistence.criteria.CriteriaBuilder;import javax.persistence.criteria.CriteriaQuery;import javax.persistence.criteria.Expression;import javax.persistence.criteria.Path;import javax.persistence.criteria.Predicate;import javax.persistence.criteria.Root; public class SimpleExpression implements ExpandCriterion {private String fieldName; / / attribute name private Object value; / / A corresponding value private Operator operator / / Calculator protected Simple_Expression (String fieldName, Object value, Operator operator) {this.fieldName = fieldName; this.value = value; this.operator = operator;} public String getFieldName () {return fieldName;} public Object getValue () {return value;} public Operator getOperator () {return operator } @ SuppressWarnings ({"rawtypes", "unchecked"}) public Predicate toPredicate (Root root, CriteriaQuery query, CriteriaBuilder builder) {Path expression = null; if (fieldName.contains (".")) {String [] names = fieldName.split ("."); expression = root.get (names [0]); for (int I = 1; I

< names.length; i++) { expression = expression.get(names[i]); } }else{ expression = root.get(fieldName); } switch (operator) { case EQ: return builder.equal(expression, value); case NE: return builder.notEqual(expression, value); case LIKE: return builder.like((Expression) expression, "%" + value + "%"); case LT: return builder.lessThan(expression, (Comparable) value); case GT: return builder.greaterThan(expression, (Comparable) value); case LTE: return builder.lessThanOrEqualTo(expression, (Comparable) value); case GTE: return builder.greaterThanOrEqualTo(expression, (Comparable) value); default: return null; } }} LogicalExpression.java 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; public class LogicalExpression implements ExpandCriterion { private ExpandCriterion[] criterion; // 逻辑表达式中包含的表达式 private Operator operator; //计算符 public Logical_Expression(ExpandCriterion[] criterions, Operator operator) { this.criterion = criterions; this.operator = operator; } public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder builder) { List predicates = new ArrayList(); for(int i=0;i 0) totalPageCount++; if (count == 0) { totalPageCount = 0; pageNo = 0; } firstRow = (pageNo - 1) * rowsPerPage + 1; if (count == 0) { firstRow = 0; } lastRow = (pageNo) * rowsPerPage; if (lastRow >

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report