分页查询,Record,map2entity工具类
1.page
package com.WT.bigdata.dsjmh.api.utils;
import com.jfinal.plugin.activerecord.Record;
import java.util.List;
/**
* 分页查询
* @author lkx
* @version 1.0
* @date 2020/2/27
*/
public class Page {
private List rows;
private Long total = -1L;
private Long totalPage ;
private Integer pageNum;
private Integer pageSize;
public List getRows() {
return rows;
}
public void setRows(List rows) {
this.rows = rows;
}
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
public Long getTotalPage() {
this.totalPage = (long) Math.ceil((double)total/pageSize);
return totalPage;
}
public void setTotalPage(Long totalPage) {
this.totalPage = totalPage;
}
public Integer getPageNum() {
return pageNum;
}
public void setPageNum(Integer pageNum) {
this.pageNum = pageNum;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Page() {
}
public Page(com.jfinal.plugin.activerecord.Page<Record> pageR) {
this.rows= RecordUtils.toMap(pageR.getList());
this.total=(long)pageR.getTotalRow();
this.pageNum=pageR.getPageNumber();
this.pageSize=pageR.getPageSize();
}
}
2.Record
package com.bcht.bigdata.dsjmh.api.utils;
import com.bcht.ms.commons.utils.Page;
import com.jfinal.plugin.activerecord.Record;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Record工具
* @author lkx
* @version 1.0
* @date 2020/2/27
*/
public class RecordUtils {
public static List<Map> toMap(List<Record> listR){
List<Map> list = new ArrayList<Map>();
if(listR!=null){
for(Record record : listR){
list.add(toMap(record));
}
}
return list;
}
public static Map toMap(Record record){
if(record!=null){
return record.getColumns();
}
return null;
}
public static Page toMap(com.jfinal.plugin.activerecord.Page<Record> pageR) {
Page page = new Page();
page.setRows(RecordUtils.toMap(pageR.getList()));
page.setTotal((long)pageR.getTotalRow());
page.setPageNum(pageR.getPageNumber());
page.setPageSize(pageR.getPageSize());
return page;
}
}
3.map2entity
package com.bcht.bigdata.dsjmh.api.utils;
/**
* ClassName: MapToEntity <br/>
* Description: <br/>
* date: 2019/7/31<br/>
*
* @since JDK 1.8
*/
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.Date;
import java.util.Map;
/**
* map转实体
*/
public class MapToEntity {
/** map
* 转实体类
* @param clazz
* @param map
* @param <T>
* @return*/
public static Object mapToObject(Class clazz, Map<String,String> map) throws IntrospectionException, IllegalAccessException, InstantiationException {
BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
Object obj = clazz.newInstance();
// 给 JavaBean 对象的属性赋值
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (int i = 0; i < propertyDescriptors.length; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
if (map.containsKey(propertyName)) {
Object value = map.get(propertyName);
if ("".equals(value)) {
value = null;
}
Object[] args = new Object[1];
args[0] = value;
try {
descriptor.getWriteMethod().invoke(obj, args);
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
System.out.println(obj.toString());
return obj;
}
/**
* Map转成实体对象
*
* @param map map实体对象包含属性
* @param clazz 实体对象类型
* @return
*/
public static <T> T map2Object(Map<String, Object> map, Class<T> clazz) {
if (map == null) {
return null;
}
T obj = null;
try {
obj = clazz.newInstance();
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
int mod = field.getModifiers();
if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
continue;
}
field.setAccessible(true);
String filedTypeName = field.getType().getName();
if (filedTypeName.equalsIgnoreCase("java.util.date")) {
String datetimestamp = String.valueOf(map.get(field.getName()));
if (datetimestamp.equalsIgnoreCase("null")) {
field.set(obj, null);
} else {
field.set(obj, new Date(Long.parseLong(datetimestamp)));
}
} else {
field.set(obj, map.get(field.getName()));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
}
标签:java,return,分页,map2entity,Record,map,obj,import,public
From: https://www.cnblogs.com/atao-BigData/p/16857548.html