Spring Data JPA 动态拼接条件的通用设计模式
2020-12-13 15:20
标签:executor zab exception arc display mdt hashmap art etop import java.sql.Timestamp; 下面是实际开发例子: controller层 DTO service 层 dao层 entity层 转自: https://blog.csdn.net/dgutliangxuan/article/details/78644464 https://blog.csdn.net/u011726984/article/details/72627706 参考: https://www.cnblogs.com/vcmq/p/9484398.html https://www.cnblogs.com/g-smile/p/9177841.html Spring Data JPA 动态拼接条件的通用设计模式 标签:executor zab exception arc display mdt hashmap art etop 原文地址:https://www.cnblogs.com/zt007/p/11578504.html
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.xxx.controller.logManage.LogSearchParamDTO;
import com.xxx.controller.trade.TradeParams;
/**
* 改进方向 1:能不能 通过反射 ,只要---
* 相关知识请自行查阅JPA Criteria查询
// 过滤条件
// 1:过滤条件会被应用到SQL语句的FROM子句中。在criteria
// 查询中,查询条件通过Predicate或Expression实例应用到CriteriaQuery对象上。
// 2:这些条件使用 CriteriaQuery .where 方法应用到CriteriaQuery 对象上
// 3:CriteriaBuilder也作为Predicate实例的工厂,通过调用CriteriaBuilder 的条件方法(
// equal,notEqual, gt, ge,lt, le,between,like等)创建Predicate对象。
// 4:复合的Predicate 语句可以使用CriteriaBuilder的and, or andnot 方法构建。
* @author 小言
* @date 2017年11月27日
* @time 上午10:44:53
* @version ╮(╯▽╰)╭
*/
public class SpecificationBuilderForOperateLog {
public static
final LogSearchParamDTO logSearchParamDTO) {
return new Specification
@Override
public Predicate toPredicate(Root
List
Timestamp startTime = logSearchParamDTO.getStartTime();
Timestamp endTime = logSearchParamDTO.getEndTime();
// 时间段
if (startTime != null && endTime != null) {
predicate.add(cb.between(root.
}
// 操作日志查询栏
String searchCondition = logSearchParamDTO.getSearchCondition();
if (searchCondition != null && !searchCondition.equals("")) {
predicate.add(cb.or(cb.equal(root.
cb.equal(root.
}
// 操作日志用户类型
String operatorType = logSearchParamDTO.getOperatorType();
System.out.println("operatorType=="+operatorType);
if (operatorType != null ){
predicate.add(cb.equal(root.
}
Predicate[] pre = new Predicate[predicate.size()];
// System.out.println("pre=="+predicate.toArray(pre));
query.where(predicate.toArray(pre));
return query.getRestriction();
}
};
}
} 1 @Controller
2 @RequestMapping(value = "/operateLog")
3 public class BgOperateLogController {
4
5 @Autowired
6 private BgOperateLogService bgOperateLogService;
7
8 @ResponseBody
9 @PostMapping("/findOperateLogByCondition")
10 public Result findOperateLogByCondition(@RequestBody LogSearchParamDTO logSearchParamDTO) {
11 System.out.println("logSearchParamDTO="+logSearchParamDTO);
12 Map
1 @Data
2 public class LogSearchParamDTO {
3 //前端传来的时间参数
4 private String start;
5 private String end;
6 private Timestamp startTime;
7 private Timestamp endTime;
8 private String searchCondition;
9 //操作日志查询参数
10 //操作用户类型(0,消费者,1商家,2后台人员)
11 private String operatorType;
12 private Integer pageNumber;
13 private Integer pageSize;
14 //登陆日志查询条件
15 public LogSearchParamDTO(Timestamp startTime, Timestamp endTime, String searchCondition) {
16 this.startTime = startTime;
17 this.endTime = endTime;
18 this.searchCondition = searchCondition;
19 }
20 public LogSearchParamDTO() {}
21 //操作日志查询条件
22 public LogSearchParamDTO(Timestamp startTime, Timestamp endTime, String searchCondition, String operatorType) {
23 this.startTime = startTime;
24 this.endTime = endTime;
25 this.searchCondition = searchCondition;
26 this.operatorType = operatorType;
27 }
28 }
1 @Override
2 public Page
1 import java.io.Serializable;
2 import org.springframework.data.jpa.repository.JpaRepository;
3 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
4 import org.springframework.stereotype.Repository;
5 import com.xxx.entity.BgOperateLog;
6 @Repository
7 public interface BgOperateLogDao extends JpaRepository
1 @Data
2 @Entity
3 public class BgOperateLog implements java.io.Serializable {
4 @Id
5 @GeneratedValue(strategy = GenerationType.AUTO)
6 private Integer id;
7 private String logText;
8 private String operatorId;
9 private String operatorName;
10 private String operatorType;
11 private String ip;
12 @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
13 private Timestamp logTime;
14 }
上一篇:错误解决:There is no getter for property named 'id' in class 'java.lang.String&a
下一篇:Python私有属性
文章标题:Spring Data JPA 动态拼接条件的通用设计模式
文章链接:http://soscw.com/essay/35040.html