QCospProduct qCospProduct = QCospProduct.cospProduct;
ArrayList<Predicate> listPredicate = new ArrayList<Predicate>();
if(StringUtils.isNotEmpty(name)){
Predicate predicate = qCospProduct.name.like("%!" + name + "%"+'!');
listPredicate.add(predicate);
}
if(StringUtils.isNotEmpty(parentId)) {
Predicate predicate = qCospProduct.typeId.eq(parentId);
listPredicate.add(predicate);
}
Pageable pageable = PageRequest.of(page.intValue() - 1, size.intValue());
return this.jpaQueryFactory.selectFrom(qCospProduct).where(ExpressionUtils.allOf(listPredicate)).offset(pageable.getOffset()).
limit(pageable.getPageSize()).orderBy(qCospProduct.createTime.desc()).fetch();
}
@Override
public long getCount(String name, String parentId) {
QCospProduct qCospProduct = QCospProduct.cospProduct;
ArrayList<Predicate> listPredicate = new ArrayList<Predicate>();
if (org.apache.commons.lang3.StringUtils.isNotEmpty(name)) {
Predicate predicate = qCospProduct.name.like("%" + name + "%");
listPredicate.add(predicate);
}
if(StringUtils.isNotEmpty(parentId)) {
Predicate predicate = qCospProduct.typeId.eq(parentId);
listPredicate.add(predicate);
}
return this.jpaQueryFactory.selectFrom(qCospProduct).where(ExpressionUtils.allOf(listPredicate)).fetchCount();
}
@ApiOperation("分页查询")
@GetMapping("/dim")
public ResultBody dim(String parentId,String name, Integer page, Integer size) {
try {
List<CospProduct> list = cospProductService.page(parentId,name, page, size);
long count = cospProductService.getCount(name,parentId);
return ResultBody.success(new GridView(list, count));
} catch (Exception e) {
log.error("查询失败",e);
return ResultBody.failure("查询失败");
}
}
package com.meritdata.cloud.base.mvc.entity;
import java.util.List;
public class GridView<T> {
private List<T> rows;
private long total;
private int pageSize;
private int pageNumber;
private int totalPages;
private int records;
public List<T> getRows() {
return this.rows;
}
public void setRows(List<T> rows) {
this.rows = rows;
}
public long getTotal() {
return this.total;
}
public void setTotal(long total) {
this.total = total;
}
public int getPageSize() {
return this.pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageNumber() {
return this.pageNumber;
}
public void setPageNumber(int pageNumber) {
this.pageNumber = pageNumber;
}
public int getTotalPages() {
return this.totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public int getRecords() {
return this.records;
}
public void setRecords(int records) {
this.records = records;
}
public GridView() {
}
public GridView(List<T> rows, long total) {
this.setRows(rows);
this.setTotal(total);
}
public GridView(List<T> rows, long total, int pageSize, int PageNumber) {
this.setRows(rows);
this.setTotal(total);
this.setPageSize(pageSize);
this.setPageNumber(PageNumber);
int totalPages = (int)(total / (long)pageSize);
if (total % (long)pageSize > 0L) {
this.setTotalPages(totalPages + 1);
} else {
this.setTotalPages(totalPages);
}
}
public GridView(List<T> rows, long total, int records, int pageSize, int PageNumber) {
this.setRows(rows);
this.setRecords(records);
this.setPageSize(pageSize);
this.setPageNumber(PageNumber);
int totalPages = (int)(total / (long)pageSize);
if (total % (long)pageSize > 0L) {
this.setTotal((long)(totalPages + 1));
} else {
this.setTotal((long)totalPages);
}
}
}
package com.meritdata.cloud.resultmodel;
import com.meritdata.cloud.base.DisableEncrypt;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@DisableEncrypt
public class ResultBody<T> implements Serializable {
private static final long serialVersionUID = -3948389268046368059L;
private Date timestamp = new Date();
private Integer status;
private String error;
private String message;
private String stackmessage;
private String path;
private T data;
private Map<String, String> extraInfo = new HashMap();
private boolean success;
public ResultBody() {
}
public Date getTimestamp() {
return this.timestamp;
}
public static long getSerialVersionUID() {
return -3948389268046368059L;
}
public String getPath() {
HttpServletRequest request = ((ServletRequestAttributes)((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes())).getRequest();
return request.getRequestURI().toString();
}
public T getData() {
return this.data;
}
public void setData(T data) {
this.data = data;
}
public Integer getStatus() {
return this.status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getMessage() {
return this.message != null ? this.message : ResultStatus.getMessage(this.status);
}
public boolean isSuccess() {
return this.success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getStackmessage() {
try {
if (this.stackmessage != null) {
String var1 = this.stackmessage;
}
} catch (Exception var5) {
var5.printStackTrace();
} finally {
return this.stackmessage;
}
}
public void setStackmessage(String stackmessage) {
this.stackmessage = stackmessage;
}
public void addExtraInfo(String key, String value) {
this.extraInfo.put(key, value);
}
public Map<String, String> getExtraInfo() {
return this.extraInfo;
}
public static ResultBody success() {
ResultBody result = new ResultBody();
result.setResultStatus(ResultStatus.SUCCESS);
result.setSuccess(true);
return result;
}
public static ResultBody success(Object data) {
ResultBody result = new ResultBody();
result.setResultStatus(ResultStatus.SUCCESS);
result.setData(data);
result.setSuccess(true);
return result;
}
public static ResultBody failure(String message) {
ResultBody result = new ResultBody();
result.setResultStatus(ResultStatus.FAILURE);
result.setMessage(message);
result.setSuccess(false);
return result;
}
public static ResultBody failure(ResultStatus resultCode) {
ResultBody result = new ResultBody();
result.setResultStatus(resultCode);
result.setSuccess(false);
return result;
}
public static ResultBody failure(ResultStatus resultCode, Throwable e) {
ResultBody result = new ResultBody();
result.setResultStatus(resultCode);
result.setStackmessage(getStackTrace(e));
result.setMessage(e.getMessage());
result.setSuccess(false);
return result;
}
public static ResultBody failure(ResultStatus resultStatus, Object data) {
ResultBody result = new ResultBody();
result.setResultStatus(resultStatus);
result.setData(data);
result.setSuccess(false);
return result;
}
public static ResultBody failure(Integer status, String message, Object data, Throwable e) {
ResultBody result = new ResultBody();
result.setStatus(status);
result.setMessage(message);
result.setData(data);
result.setStackmessage(getStackTrace(e));
result.setSuccess(false);
return result;
}
public void setResultStatus(ResultStatus resultStatus) {
this.status = resultStatus.status();
this.message = resultStatus.message();
}
public void setMessage(String message) {
this.message = message;
}
public static String getStackTrace(Throwable t) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
String var3;
try {
if (t != null) {
t.printStackTrace(pw);
var3 = sw.toString();
return var3;
}
var3 = "";
} finally {
pw.close();
}
return var3;
}
}