java+jpa+多条件查询_spring jpa : 多条件查询

packagecom.payease.scfordermis.service.impl;importcom.payease.scfordermis.bo.ResultBo;importcom.payease.scfordermis.bo.requestBo.ReqEmpAccNumBean;importcom.payease.scfordermis.bo.responseBo.PageResponseCommBean;importcom.payease.scfordermis.bo.responseBo.RespEmpAccNumBean;importcom.payease.scfordermis.dao.DepartmentInfoDao;importcom.payease.scfordermis.dao.EmployeeInfoDao;importcom.payease.scfordermis.entity.TDepartmentInfoEntity;importcom.payease.scfordermis.entity.TEmployeeInfoEntity;importcom.payease.scfordermis.service.EmpAccNumService;importcom.payease.scfordermis.utils.MapUtil;importorg.apache.commons.lang.StringUtils;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.data.domain.Page;importorg.springframework.data.domain.PageRequest;importorg.springframework.data.domain.Pageable;importorg.springframework.data.domain.Sort;importorg.springframework.data.jpa.domain.Specification;importorg.springframework.stereotype.Service;importjavax.persistence.criteria.CriteriaBuilder;importjavax.persistence.criteria.CriteriaQuery;importjavax.persistence.criteria.Predicate;importjavax.persistence.criteria.Root;importjava.util.ArrayList;importjava.util.List;importjava.util.Map;/*** @Created By liuxiaoming

* @CreateTime 2018/1/15 下午1:55

**/@Servicepublic class EmpAccNumServiceImpl implementsEmpAccNumService {

@Autowired

EmployeeInfoDao employeeInfoDao;

@Autowired

DepartmentInfoDao departmentInfoDao;/*** 员工列表查询

*@param*@return

*/@OverridepublicResultBo getEmpAccNumList(ReqEmpAccNumBean req) {

ResultBo result=ResultBo.build();//入参

/*** json.put("depParentId",depParentId);

json.put("departmentId", departmentId);

json.put("other", other);

json.put("page",super.page);

json.put("size",super.size);*/Map map1 =req.getMap();

MapUtil map= newMapUtil(map1);//todo

Page page = this.pageTEmp(map);//todo

PageResponseCommBean resp =this.pageToResp(page);

System.out.println("=========resp.getTotalPages():"+resp.getTotalPages());

System.out.println("=========resp.getTotalElements():"+resp.getTotalElements());

System.out.println("=========resp.getContent():"+resp.getContent());//======================================================

result.setResultBody(resp);returnresult;

}/*** 从数据库中查询数据

*@parammap

*@return

*/

public PagepageTEmp(MapUtil map){

Integer depParentId= map.getInteger("depParentId");

Integer departmentId= map.getInteger("departmentId");

String other= map.getString("other");//分页 当前页 每页显示条数 按照字段排序

Pageable pageable = new PageRequest(map.getInteger("page") - 1, map.getInteger("size"), new Sort(Sort.Direction.DESC, "fId"));

Page page =employeeInfoDao.findAll(new Specification() {public Predicate toPredicate(Rootroot,

CriteriaQuery>query, CriteriaBuilder cb) {

List lstPredicates = new ArrayList();//部门-1 全查//部门非-1//看 父ID 0 根节点//子节点

if (departmentId != 0){if(departmentId != -1){if(depParentId == 0) {//映射实体类字段 请求入参字段

lstPredicates.add(cb.equal(root.get("fDepartIdOne").as(Integer.class), departmentId));

}else{

lstPredicates.add(cb.equal(root.get("fDepartIdTwo").as(Integer.class), departmentId));

}

}

}if(StringUtils.isNotBlank(other)) {

lstPredicates.add(cb.like(root.get("fSearchKey").as(String.class), "%"+ other +"%"));

}

lstPredicates.add(cb.notEqual(root.get("fIsDelete").as(String.class), "yes"));

Predicate[] arrayPredicates= newPredicate[lstPredicates.size()];returncb.and(lstPredicates.toArray(arrayPredicates));

}

}, pageable);returnpage;

}public PageResponseCommBean pageToResp(Pagepage){

List list =page.getContent();

PageResponseCommBean bean= newPageResponseCommBean();

List respList = new ArrayList<>();for(TEmployeeInfoEntity entity : list){

RespEmpAccNumBean resp= newRespEmpAccNumBean();

resp.setfId(entity.getfId());if(StringUtils.isNotBlank(String.valueOf(entity.getfAccount()))){

resp.setfAccount(entity.getfAccount());

}if(StringUtils.isNotBlank(String.valueOf(entity.getfName()))){

resp.setfName(entity.getfName());

}if(StringUtils.isNotBlank(String.valueOf(entity.getfPosition()))){

resp.setfPosition(entity.getfPosition());

}if(StringUtils.isNotBlank(String.valueOf(entity.getfDepartIdTwo()))){

TDepartmentInfoEntity one=departmentInfoDao.findOne(entity.getfDepartIdTwo());if(one == null){

resp.setfDepartIdTwo("");

}else{

resp.setfDepartIdTwo(one.getfName());

}

}if(StringUtils.isNotBlank(entity.getfMobile())){

resp.setfMobile(entity.getfMobile());

}if(StringUtils.isNotBlank(entity.getfEmail())){

resp.setfEmail(entity.getfEmail());

}if(StringUtils.isNotBlank(entity.getfStatus())){

resp.setfStatus(entity.getfStatus());

}

respList.add(resp);

}

bean.setContent(respList);

bean.setNumber(page.getNumber());

bean.setSize(page.getSize());

bean.setTotalElements(Integer.valueOf(String.valueOf(page.getTotalElements())));

bean.setTotalPages(page.getTotalPages());returnbean;

}//public Boolean cheackMap(Map map) {//boolean bo = true;//Iterator> it = map.entrySet().iterator();//while (it.hasNext()) {//Map.Entry entry = it.next();//System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());//if(map.get(entry.getKey())==null){//return false;//}//}//return true;//}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值