分页查询,Record,map2entity工具类
1.page
package com.WT.bigdata.dsjmh.api.utils;
import com.jfinal.plugin.activerecord.Record;
import java.util.List;
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;
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;
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;
public class MapToEntity {
public static Object mapToObject(Class clazz, Map<String,String> map) throws IntrospectionException, IllegalAccessException, InstantiationException {
BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
Object obj = clazz.newInstance();
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;
}
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;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】