diff --git a/src/com/engine/salary/util/page/Column.java b/src/com/engine/salary/util/page/Column.java new file mode 100644 index 000000000..71dad2257 --- /dev/null +++ b/src/com/engine/salary/util/page/Column.java @@ -0,0 +1,16 @@ +package com.engine.salary.util.page; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class Column { + String title; + String dataIndex; + String key; +} diff --git a/src/com/engine/salary/util/page/DataSource.java b/src/com/engine/salary/util/page/DataSource.java new file mode 100644 index 000000000..379b99023 --- /dev/null +++ b/src/com/engine/salary/util/page/DataSource.java @@ -0,0 +1,9 @@ +package com.engine.salary.util.page; + +import lombok.Data; + +@Data +public class DataSource { + String key; + String title; +} diff --git a/src/com/engine/salary/util/page/PageInfo.java b/src/com/engine/salary/util/page/PageInfo.java new file mode 100644 index 000000000..af8d06447 --- /dev/null +++ b/src/com/engine/salary/util/page/PageInfo.java @@ -0,0 +1,43 @@ +package com.engine.salary.util.page; + +import com.engine.salary.annotation.TableTitle; +import lombok.Data; +import lombok.ToString; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; + +@Data +@ToString +public class PageInfo extends com.github.pagehelper.PageInfo { + Class clazz; + List columns = new ArrayList<>(); + List dataSource = new ArrayList<>(); + + public PageInfo(List list) { + super(list); + } + + public PageInfo(List list, Class clazz) { + super(list); + this.clazz = clazz; + } + + + public List getColumns() { + Field[] fields = clazz.getDeclaredFields(); + for (Field f : fields) { + boolean isanno = f.isAnnotationPresent(TableTitle.class); + if (isanno) { + TableTitle annotation = f.getAnnotation(TableTitle.class); + String title = annotation.title(); + String dataIndex = annotation.dataIndex(); + String key = annotation.key(); + Column column = Column.builder().title(title).dataIndex(dataIndex).key(key).build(); + columns.add(column); + } + } + return columns; + } +} diff --git a/src/com/engine/salary/util/page/PageUtil.java b/src/com/engine/salary/util/page/PageUtil.java new file mode 100644 index 000000000..ab4aa5314 --- /dev/null +++ b/src/com/engine/salary/util/page/PageUtil.java @@ -0,0 +1,45 @@ +package com.engine.salary.util.page; + +import com.github.pagehelper.Page; +import com.github.pagehelper.PageHelper; +import org.apache.commons.collections4.CollectionUtils; + +import java.util.Collections; +import java.util.List; + + +public class PageUtil { + + public static void start(Integer pageNum, Integer pageSize) { + pageNum = pageNum == null || pageNum <= 0 ? 1 : pageNum; + pageSize = pageSize == null || pageSize <= 0 ? 10 : pageSize; + PageHelper.startPage(pageNum, pageSize); + } + + + public static Page buildPage(Integer pageNo, Integer pageSize) { + pageNo = pageNo == null || pageNo <= 0 ? 1 : pageNo; + pageSize = pageSize == null || pageSize <= 0 ? 10 : pageSize; + return new Page(pageNo, pageSize, true); + } + + /** + * 分页 + * + * @param pageNo 页码(从1开始) + * @param pageSize 每页条数 + * @param source 待分页的数据 + * @param 范型制定类 + * @return + */ + public static List subList(int pageNo, int pageSize, List source) { + if (CollectionUtils.isEmpty(source)) { + return Collections.emptyList(); + } + int endIndex = pageNo * pageSize; + int startIndex = (pageNo - 1) * pageSize; + startIndex = startIndex < 0 ? 0 : startIndex; + return source.subList(startIndex > source.size() ? source.size() : startIndex, + endIndex > source.size() ? source.size() : endIndex); + } +}