https://www.cnblogs.com/Donnnnnn/p/6277872.html

方式一:

  1. 第一步:EmpAccNumService
  1. package com.payease.scfordermis.service;
  2.  
  3. import com.payease.scfordermis.bo.ResultBo;
  4. import com.payease.scfordermis.bo.requestBo.ReqEmpAccNumBean;
  5.  
  6. /**
  7. * @Created By liuxiaoming
  8. * @CreateTime 2018/1/15 下午1:55
  9. **/
  10. public interface EmpAccNumService {
  11.  
  12. ResultBo getEmpAccNumList(ReqEmpAccNumBean reqEmpAccNumBean);
  13.  
  14. }

第二步:EmpAccNumServiceImpl

  1. package com.payease.scfordermis.service.impl;
  2.  
  3. import com.payease.scfordermis.bo.ResultBo;
  4. import com.payease.scfordermis.bo.requestBo.ReqEmpAccNumBean;
  5. import com.payease.scfordermis.bo.responseBo.PageResponseCommBean;
  6. import com.payease.scfordermis.bo.responseBo.RespEmpAccNumBean;
  7. import com.payease.scfordermis.dao.DepartmentInfoDao;
  8. import com.payease.scfordermis.dao.EmployeeInfoDao;
  9. import com.payease.scfordermis.entity.TDepartmentInfoEntity;
  10. import com.payease.scfordermis.entity.TEmployeeInfoEntity;
  11. import com.payease.scfordermis.service.EmpAccNumService;
  12. import com.payease.scfordermis.utils.MapUtil;
  13. import org.apache.commons.lang.StringUtils;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.data.domain.Page;
  16. import org.springframework.data.domain.PageRequest;
  17. import org.springframework.data.domain.Pageable;
  18. import org.springframework.data.domain.Sort;
  19. import org.springframework.data.jpa.domain.Specification;
  20. import org.springframework.stereotype.Service;
  21.  
  22. import javax.persistence.criteria.CriteriaBuilder;
  23. import javax.persistence.criteria.CriteriaQuery;
  24. import javax.persistence.criteria.Predicate;
  25. import javax.persistence.criteria.Root;
  26. import java.util.ArrayList;
  27. import java.util.List;
  28. import java.util.Map;
  29.  
  30. /**
  31. * @Created By liuxiaoming
  32. * @CreateTime 2018/1/15 下午1:55
  33. **/
  34. @Service
  35. public class EmpAccNumServiceImpl implements EmpAccNumService {
  36.  
  37. @Autowired
  38. EmployeeInfoDao employeeInfoDao;
  39. @Autowired
  40. DepartmentInfoDao departmentInfoDao;
  41.  
  42. /**
  43. * 员工列表查询
  44. * @param
  45. * @return
  46. */
  47. @Override
  48. public ResultBo getEmpAccNumList(ReqEmpAccNumBean req) {
  49.  
  50. ResultBo result = ResultBo.build();
  51. //入参
  52. /**
  53. * json.put("depParentId",depParentId);
  54. json.put("departmentId", departmentId);
  55. json.put("other", other);
  56. json.put("page",super.page);
  57. json.put("size",super.size);
  58. */
  59. Map<String,Object> map1 = req.getMap();
  60. MapUtil map = new MapUtil(map1);
  61.  
  62. //todo
  63. Page<TEmployeeInfoEntity> page = this.pageTEmp(map);
  64. //todo
  65. PageResponseCommBean resp =this.pageToResp(page);
  66. System.out.println("=========resp.getTotalPages():"+resp.getTotalPages());
  67. System.out.println("=========resp.getTotalElements():"+resp.getTotalElements());
  68. System.out.println("=========resp.getContent():"+resp.getContent());
  69. //======================================================
  70. result.setResultBody(resp);
  71. return result;
  72.  
  73. }
  74.  
  75. /**
  76. * 从数据库中查询数据
  77. * @param map
  78. * @return
  79. */
  80. public Page<TEmployeeInfoEntity> pageTEmp(MapUtil map){
  81. Integer depParentId = map.getInteger("depParentId");
  82. Integer departmentId = map.getInteger("departmentId");
  83. String other = map.getString("other");
  84. //分页 当前页 每页显示条数 按照字段排序
  85. Pageable pageable = new PageRequest(map.getInteger("page") - 1, map.getInteger("size"), new Sort(Sort.Direction.DESC, "fId"));
  86. Page<TEmployeeInfoEntity> page = employeeInfoDao.findAll(
  87. new Specification<TEmployeeInfoEntity>() {
  88.  
  89. public Predicate toPredicate(Root<TEmployeeInfoEntity> root,
  90. CriteriaQuery<?> query, CriteriaBuilder cb) {
  91. List<Predicate> lstPredicates = new ArrayList<Predicate>();
  92. //部门-1 全查
  93. //部门非-1
  94. // 看 父ID 0 根节点
  95. // 子节点
  96. if (departmentId != 0){
  97. if(departmentId != -1){
  98. if(depParentId == 0) {
  99. // 映射实体类字段 请求入参字段
  100. lstPredicates.add(cb.equal(root.get("fDepartIdOne").as(Integer.class), departmentId));
  101. }else{
  102. lstPredicates.add(cb.equal(root.get("fDepartIdTwo").as(Integer.class), departmentId));
  103.  
  104. }
  105. }
  106. }
  107. if (StringUtils.isNotBlank(other)) {
  108. lstPredicates.add(cb.like(root.get("fSearchKey").as(String.class), "%"+ other +"%"));
  109. }
  110. lstPredicates.add(cb.notEqual(root.get("fIsDelete").as(String.class), "yes" ));
  111. Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
  112. return cb.and(lstPredicates.toArray(arrayPredicates));
  113. }
  114. }, pageable);
  115. return page;
  116. }
  117.  
  118. public PageResponseCommBean pageToResp(Page<TEmployeeInfoEntity> page){
  119. List<TEmployeeInfoEntity> list = page.getContent();
  120. PageResponseCommBean bean = new PageResponseCommBean();
  121. List<RespEmpAccNumBean> respList = new ArrayList<>();
  122. for (TEmployeeInfoEntity entity : list){
  123.  
  124. RespEmpAccNumBean resp = new RespEmpAccNumBean();
  125. resp.setfId(entity.getfId());
  126. if(StringUtils.isNotBlank(String.valueOf(entity.getfAccount()))){
  127. resp.setfAccount(entity.getfAccount());
  128. }
  129. if(StringUtils.isNotBlank(String.valueOf(entity.getfName()))){
  130. resp.setfName(entity.getfName());
  131. }
  132. if(StringUtils.isNotBlank(String.valueOf(entity.getfPosition()))){
  133. resp.setfPosition(entity.getfPosition());
  134. }
  135. if(StringUtils.isNotBlank(String.valueOf(entity.getfDepartIdTwo()))){
  136. TDepartmentInfoEntity one = departmentInfoDao.findOne(entity.getfDepartIdTwo());
  137. if(one == null){
  138. resp.setfDepartIdTwo("");
  139. }else{
  140. resp.setfDepartIdTwo(one.getfName());
  141. }
  142. }
  143. if(StringUtils.isNotBlank(entity.getfMobile())){
  144. resp.setfMobile(entity.getfMobile());
  145. }
  146.  
  147. if(StringUtils.isNotBlank(entity.getfEmail())){
  148. resp.setfEmail(entity.getfEmail());
  149. }
  150.  
  151. if(StringUtils.isNotBlank(entity.getfStatus())){
  152. resp.setfStatus(entity.getfStatus());
  153. }
  154. respList.add(resp);
  155. }
  156. bean.setContent(respList);
  157. bean.setNumber(page.getNumber());
  158. bean.setSize(page.getSize());
  159. bean.setTotalElements(Integer.valueOf(String.valueOf(page.getTotalElements())));
  160. bean.setTotalPages(page.getTotalPages());
  161. return bean;
  162. }
  163.  
  164. // public Boolean cheackMap(Map<String,Object> map) {
  165. // boolean bo = true;
  166. // Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
  167. // while (it.hasNext()) {
  168. // Map.Entry<String, Object> entry = it.next();
  169. // System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
  170. // if(map.get(entry.getKey())==null){
  171. // return false;
  172. // }
  173. // }
  174. // return true;
  175. // }
  176. }

第三步:MapUtil

  1. package com.payease.scfordermis.utils;
  2.  
  3. import java.math.BigDecimal;
  4. import java.util.Date;
  5. import java.util.Map;
  6.  
  7. /**
  8. * @Created By liuxiaoming
  9. * @CreateTime 2018/1/16 上午9:43
  10. **/
  11. public class MapUtil {
  12. private Map<String,Object> map;
  13.  
  14. public MapUtil(Map<String,Object> map){
  15. this.map = map;
  16. }
  17.  
  18. /**
  19. * <p>
  20. * 根据Key返回一个Double型
  21. * </p>
  22. * @param key
  23. * @return Double
  24. */
  25. public Double getDouble(String key){
  26. if(map.get(key)!=null){
  27. if(map.get(key) instanceof Double){
  28. return (Double)map.get(key);
  29. }else{
  30. return 0.0;
  31. }
  32. }else{
  33. return 0.00;
  34. }
  35. }
  36.  
  37. /**
  38. * <p>
  39. * 根据Key返回一个String
  40. * </p>
  41. * @param key
  42. * @return String
  43. */
  44. public String getString(String key){
  45. if(map.get(key)!=null){
  46. if(map.get(key) instanceof String){
  47. return (String)map.get(key);
  48. }else{
  49. return "";
  50. }
  51. }else{
  52. return "";
  53. }
  54. }
  55.  
  56. /**
  57. * <p>
  58. * 根据Key返回一个Date
  59. * </p>
  60. * @param key
  61. * @return Date
  62. */
  63. public Date getDate(String key){
  64. if(map.get(key)!=null){
  65. if(map.get(key) instanceof Date){
  66. return (Date)map.get(key);
  67. }else{
  68. return null;
  69. }
  70. }else{
  71. return null;
  72. }
  73. }
  74.  
  75. /**
  76. * <p>
  77. * 根据Key返回一个Integer
  78. * </p>
  79. * @param key
  80. * @return Integer
  81. */
  82. public Integer getInteger(String key){
  83. if(map.get(key)!=null){
  84. if(map.get(key) instanceof Integer){
  85. return (Integer)map.get(key);
  86. }else{
  87. return 0;
  88. }
  89. }else{
  90. return 0;
  91. }
  92. }
  93.  
  94. /**
  95. * <p>
  96. * 根据一个Key返回一个Map<String,String>
  97. * </p>
  98. * @param key
  99. * @return Map<String,String>
  100. */
  101. @SuppressWarnings("unchecked")
  102. public Map<String,String> getMap(String key){
  103. if(map.get(key)!=null){
  104. if(map.get(key) instanceof Map){
  105. return (Map<String,String>)map.get(key);
  106. }else{
  107. return null;
  108. }
  109. }else{
  110. return null;
  111. }
  112. }
  113.  
  114. /**
  115. * <p>
  116. * 根据key返回BigDecimal
  117. * 如果为null,则返回 new BigDecimal(0)
  118. * </p>
  119. * @param key
  120. * @return BigDecimal
  121. */
  122. public BigDecimal getBigDecimal(String key){
  123. if(map.containsKey(key)){
  124. if(map.get(key) instanceof BigDecimal){
  125. return (BigDecimal)map.get(key);
  126. }else{
  127. return new BigDecimal(0);
  128. }
  129. }else{
  130. return new BigDecimal(0);
  131. }
  132. }
  133. }

第四步:RespEmpAccNumBean

  1. package com.payease.scfordermis.bo.responseBo;
  2.  
  3. import io.swagger.annotations.ApiModel;
  4. import io.swagger.annotations.ApiModelProperty;
  5.  
  6. /**
  7. * @Created By liuxiaoming
  8. * @CreateTime 2018/1/10 下午2:23
  9. **/
  10. @ApiModel(value = "员工列表查询实体",description = "描述员工列表查询类")
  11. public class RespEmpAccNumBean {
  12. @ApiModelProperty(value = "序号",dataType = "long",required = true)
  13. private long fId;
  14. @ApiModelProperty(value = "部门",dataType = "string",required = true)
  15. private String fDepartIdTwo;
  16. @ApiModelProperty(value = "账号",dataType = "string",required = true)
  17. private String fAccount;
  18. @ApiModelProperty(value = "账号状态: open-开通 close-禁用 ",dataType = "string",required = true)
  19. private String fStatus;
  20. @ApiModelProperty(value = "姓名",dataType = "string",required = true)
  21. private String fName;
  22. @ApiModelProperty(value = "手机",dataType = "string",required = true)
  23. private String fMobile;
  24. @ApiModelProperty(value = "邮箱",dataType = "string",required = true)
  25. private String fEmail;
  26. @ApiModelProperty(value = "职位",dataType = "string",required = true)
  27. private String fPosition;
  28.  
  29. public long getfId() {
  30. return fId;
  31. }
  32.  
  33. public void setfId(long fId) {
  34. this.fId = fId;
  35. }
  36.  
  37. public String getfDepartIdTwo() {
  38. return fDepartIdTwo;
  39. }
  40.  
  41. public void setfDepartIdTwo(String fDepartIdTwo) {
  42. this.fDepartIdTwo = fDepartIdTwo;
  43. }
  44.  
  45. public String getfAccount() {
  46. return fAccount;
  47. }
  48.  
  49. public void setfAccount(String fAccount) {
  50. this.fAccount = fAccount;
  51. }
  52.  
  53. public String getfStatus() {
  54. return fStatus;
  55. }
  56.  
  57. public void setfStatus(String fStatus) {
  58. this.fStatus = fStatus;
  59. }
  60.  
  61. public String getfName() {
  62. return fName;
  63. }
  64.  
  65. public void setfName(String fName) {
  66. this.fName = fName;
  67. }
  68.  
  69. public String getfMobile() {
  70. return fMobile;
  71. }
  72.  
  73. public void setfMobile(String fMobile) {
  74. this.fMobile = fMobile;
  75. }
  76.  
  77. public String getfEmail() {
  78. return fEmail;
  79. }
  80.  
  81. public void setfEmail(String fEmail) {
  82. this.fEmail = fEmail;
  83. }
  84.  
  85. public String getfPosition() {
  86. return fPosition;
  87. }
  88.  
  89. public void setfPosition(String fPosition) {
  90. this.fPosition = fPosition;
  91. }
  92.  
  93. @Override
  94. public String toString() {
  95. return "RespEmpAccNumBean{" +
  96. "fId=" + fId +
  97. ", fDepartIdTwo='" + fDepartIdTwo + '\'' +
  98. ", fAccount='" + fAccount + '\'' +
  99. ", fStatus='" + fStatus + '\'' +
  100. ", fName='" + fName + '\'' +
  101. ", fMobile='" + fMobile + '\'' +
  102. ", fEmail='" + fEmail + '\'' +
  103. ", fPosition='" + fPosition + '\'' +
  104. '}';
  105. }
  106. }

第五步:PageResponseCommBean

  1. package com.payease.scfordermis.bo.responseBo;
  2.  
  3. import io.swagger.annotations.ApiModel;
  4. import io.swagger.annotations.ApiModelProperty;
  5.  
  6. /**
  7. * @Author : zhangwen
  8. * @Data : 2018/1/10
  9. * @Description :
  10. */
  11. @ApiModel(value = "分页公共返回类",description = "描述分页返回类")
  12. public class PageResponseCommBean<T> extends PageBean{
  13. @ApiModelProperty(value = "结果集",dataType = "list")
  14. private T content;
  15.  
  16. public T getContent() {
  17. return content;
  18. }
  19.  
  20. public void setContent(T content) {
  21. this.content = content;
  22. }
  23.  
  24. @Override
  25. public String toString() {
  26. return "PageResponseCommBean{" +
  27. "content=" + content +
  28. '}';
  29. }
  30. }

第六步:PageBean

  1. package com.payease.scfordermis.bo.responseBo;
  2.  
  3. import io.swagger.annotations.ApiModelProperty;
  4.  
  5. /**
  6. * @Created By liuxiaoming
  7. * @CreateTime 2018/1/10 下午2:02
  8. **/
  9. public class PageBean {
  10. @ApiModelProperty(value = "当前页",dataType = "int",required = true)
  11. private Integer number;
  12. @ApiModelProperty(value = "每页显示条数",dataType = "int",required = true)
  13. private Integer size;
  14. @ApiModelProperty(value = "总条数",dataType = "int",required = true)
  15. private Integer totalElements;
  16. @ApiModelProperty(value = "总页数",dataType = "int",required = true)
  17. private Integer totalPages;
  18.  
  19. public Integer getNumber() {
  20. return number;
  21. }
  22.  
  23. public void setNumber(Integer number) {
  24. this.number = number;
  25. }
  26.  
  27. public Integer getSize() {
  28. return size;
  29. }
  30.  
  31. public void setSize(Integer size) {
  32. this.size = size;
  33. }
  34.  
  35. public Integer getTotalElements() {
  36. return totalElements;
  37. }
  38.  
  39. public void setTotalElements(Integer totalElements) {
  40. this.totalElements = totalElements;
  41. }
  42.  
  43. public Integer getTotalPages() {
  44. return totalPages;
  45. }
  46.  
  47. public void setTotalPages(Integer totalPages) {
  48. this.totalPages = totalPages;
  49. }
  50.  
  51. @Override
  52. public String toString() {
  53. return "PageBean{" +
  54. "number=" + number +
  55. ", size=" + size +
  56. ", totalElements=" + totalElements +
  57. ", totalPages=" + totalPages +
  58. '}';
  59. }
  60. }

第七步:测试

  1. package com.payease.scfordermis;
  2.  
  3. import com.payease.scfordermis.bo.ResultBo;
  4. import com.payease.scfordermis.bo.requestBo.ReqEmpAccNumBean;
  5. import com.payease.scfordermis.service.EmpAccNumService;
  6. import org.junit.Test;
  7. import org.junit.runner.RunWith;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.boot.test.context.SpringBootTest;
  10. import org.springframework.test.context.junit4.SpringRunner;
  11.  
  12. @RunWith(SpringRunner.class)
  13. @SpringBootTest
  14. public class ScfOrderMisApplicationTests {
  15. @Autowired
  16. private EmpAccNumService empAccNumService;
  17.  
  18. @Test
  19. public void contextLoads() {
  20. ReqEmpAccNumBean req = new ReqEmpAccNumBean();
  21. //req.setDepartmentId(-1);
  22. //req.setDepParentId();
  23. req.setDepartmentId(2);
  24. req.setDepParentId(1);
  25. req.setOther("1");
  26. ResultBo result = empAccNumService.getEmpAccNumList(req);
  27. System.out.println(result.toString());
  28.  
  29. // Map<String,Object> map = req.getMap();
  30. // Boolean bo = new EmpAccNumServiceImpl().cheackMap(map);
  31. // System.out.println(bo);
  32. }
  33.  
  34. }

spring jpa : 多条件查询的更多相关文章

  1. jpa多条件查询重写Specification的toPredicate方法(转)

    Spring Data JPA支持JPA2.0的Criteria查询,相应的接口是JpaSpecificationExecutor.Criteria 查询:是一种类型安全和更面向对象的查询 . 这个接 ...

  2. jpa多条件查询

    首先继承JpaSpecificationExecutor<T>接口 需要用到JpaSpecificationExecutor<T>中的Page<T> findAll ...

  3. Spring Boot Jpa 多条件查询+排序+分页

    事情有点多,于是快一个月没写东西了,今天补上上次说的. JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将 ...

  4. spring data jpa 组合条件查询封装

    /** * 定义一个查询条件容器 * @author lee * * @param <T> */ public class Criteria<T> implements Spe ...

  5. springboot jpa 多条件查询(多表)

    前几天写的,贴上来. 实体类. package com.syl.demo.daomain; import lombok.Data; import javax.persistence.*; /** * ...

  6. springboot jpa 多条件查询(单表)

    需要实现的功能: 多个搜索输入框:全部不填,则查出所有列表:填了条件,就按条件查找:填的条件个数不定. 方法实现的核心:jpa自带的Specification<T> (目前只需要单表,多表 ...

  7. spring jpa 自定义查询数据库的某个字段

    spring jpa 提供的查询很强大, 就看你会不会用了. 先上代码, 后面在解释吧 1. 想查单个表的某个字段 在repository中 @Query(value = "select i ...

  8. 使用Spring JPA中Page、Pageable接口和Sort类完成分页排序

    显示时,有三个参数,前两个必填,第几页,一页多少个size,第三个参数默认可以不填. 但是发现这个方法已经过时了,通过查看它的源码发现,新方法为静态方法PageRequest of(page,size ...

  9. Spring MVC和Spring Data JPA之按条件查询和分页(kkpaper分页组件)

    推荐视频:尚硅谷Spring Data JPA视频教程,一学就会,百度一下就有, 后台代码:在DAO层继承Spring Data JPA的PagingAndSortingRepository接口实现的 ...

随机推荐

  1. 很实用的linux 上的svn安装和svnserver 的重启

    虽然在windows上搭建SVN很简单,但是效能却不高,这当然是和linux相比了.然而在linux上搭建SVN却非常繁琐,所以今天这篇文章就来一步一步教您如何在Centos上搭建SVN 安装 #yu ...

  2. java中定时器总结

    java实现定时器的四种方式: 一. /** * 延迟20000毫秒执行 java.util.Timer.schedule(TimerTask task, long delay) */ public ...

  3. xslt 简单的语法

    1. 循环 <xsl:for-each select="catalog/cd"> 1 </xsl:for-each> 2. 定义变量赋值使用 <xsl ...

  4. #微码分享#AES算法的C++包装类

    AES为Advanced Encryption Standard的缩写,中文名:高级加密标准,在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准,用来替代DES.基于std:: ...

  5. 从npm到vue和nodejs

    https://www.npmjs.com.cn/ vue和nodejs Windows 下安装NPM:https://www.cnblogs.com/interdrp/p/6779973.html ...

  6. PHP与Python哪个做网站产品好?

    虽然python现在比较火,但在传统的LAMP组合里Linux+apache/tomcat+MySql+PHP里是PHP做网站的脚本语言,但现在已经变了:https://baike.baidu.com ...

  7. docker+phantomjs+haproxy 搭建phantomjs集群

    目标: 搭建一个远程的phantomjs服务器,提供高可用服务,支持并发. 原料: 1.docker环境.docker-compose环境 2.phantomjs镜像: docker.io/werni ...

  8. noip第17课作业

    1.  召见骑士 [问题描述] 某王国有5位骑士,每位骑士都有自己的编号,且这个王国的编号都为奇数,分别为1,3,5,7,9,在国王召见他们之前他们都必须经过只能从一边进出的长廊,长廊的宽度只能坐一个 ...

  9. Java学习--数组与方法

    1. public class MethodDemo01{ public static void main(String args[]){ printInfo() ; // 调用printInfo() ...

  10. fastscript例子一

    fastscript例子一   fastscript例子一 unit Unit1; interface usesWinapi.Windows, Winapi.Messages, System.SysU ...