baseentity java_BaseMapEntity

这是一个Java工具类,扩展了LinkedHashMap,实现了根据Map键获取不同类型的值(如int, long, double, String, Date, BigDecimal等),并提供了日志处理、字段名下划线到驼峰命名转换、Map到BaseMapEntity转换等功能,适用于处理和转换Map数据。
摘要由CSDN通过智能技术生成

import com.alibaba.fastjson.JSON;

import org.apache.log4j.LogManager;

import org.apache.log4j.Logger;

import java.io.Serializable;

import java.math.BigDecimal;

import java.text.SimpleDateFormat;

import java.util.*;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class BaseMapEntity extends LinkedHashMap implements Serializable {

private static final long serialVersionUID = 1L;

/** 日志 */

protected final Logger logger = LogManager.getLogger(this.getClass());

/**

* 根据map键获取对应的int类型的值

*

* @param mapKey map键

* @return map值

*/

public Integer getInt(String mapKey) {

// 根据key获取值

Object mapValue = this.get(mapKey);

Integer mapValueInt = null;

try {

if (null == mapValue) {

mapValueInt = null;

} else if (mapValue instanceof Integer) {

mapValueInt = (Integer) mapValue;

} else if (mapValue instanceof String) {

mapValueInt = Integer.parseInt((String) mapValue);

} else {

mapValueInt = Integer.parseInt(String.valueOf(mapValue));

}

} catch (Exception e) {

logger.warn("根据map键[" + mapKey + "]获取对应的int类型的值异常,原值[" + mapValue + "]");

}

// 返回结果

return mapValueInt;

}

public int getInt(String mapKey,int defaultValue) {

Integer integer = getInt(mapKey);

if(integer == null)

return defaultValue;

return integer;

}

/**

* 根据map键获取对应的long类型的值

* @param mapKey map键

* @return map值

*/

public Long getLong(String mapKey) {

// 根据key获取值

Object mapValue = this.get(mapKey);

Long mapValueLong = null;

try {

if (null == mapValue) {

mapValueLong = null;

} else if (mapValue instanceof Long) {

mapValueLong = (Long) mapValue;

} else if (mapValue instanceof String) {

mapValueLong = Long.parseLong((String) mapValue);

} else {

mapValueLong = Long.parseLong(String.valueOf(mapValue));

}

} catch (Exception e) {

logger.warn("根据map键[" + mapKey + "]获取对应的long类型的值异常,原值[" + mapValue + "]");

}

// 返回结果

return mapValueLong;

}

/**

* 根据map键获取对应的double类型的值

* @param mapKey map键

* @return map值

*/

public Double getDouble(String mapKey) {

// 根据key获取值

Object mapValue = this.get(mapKey);

Double mapValueDouble = null;

try {

if (null == mapValue) {

mapValueDouble = null;

} else if (mapValue instanceof Double) {

mapValueDouble = (Double) mapValue;

} else if (mapValue instanceof String) {

mapValueDouble = Double.parseDouble((String) mapValue);

} else {

mapValueDouble = Double.parseDouble(String.valueOf(mapValue));

}

} catch (Exception e) {

logger.warn("根据map键[" + mapKey + "]获取对应的double类型的值异常,原值[" + mapValue + "]");

}

// 返回结果

return mapValueDouble;

}

/**

* 根据map键获取对应的String类型的值

*

* @param mapKey map键

* @return map值

*/

public String getString(String mapKey) {

//定义返回结果变量

String resultStr = null;

// 根据key获取值

Object mapValue = this.get(mapKey);

// 变量赋值

if(null == mapValue){

resultStr = null;

} else if (mapValue instanceof String) {

resultStr = (String) mapValue;

}else{

resultStr = String.valueOf(mapValue);

}

// 返回结果

return resultStr;

}

/**

* 根据map键获取对应的Date类型的值

* @param mapKey map键

* @return map值

*/

public Date getDate(String mapKey) {

//定义返回结果变量

Date resultDate = null;

// 根据key获取值

Object mapValue = this.get(mapKey);

try {

// 变量赋值

if(null == mapValue){

resultDate = null;

} else if (mapValue instanceof Date) {

resultDate = (Date) mapValue;

}else{

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

resultDate = sdf.parse(String.valueOf(mapValue));

}

} catch (Exception e) {

logger.warn("根据map键[" + mapKey + "]获取对应的Date类型的值异常,原值[" + mapValue + "]");

}

// 返回结果

return resultDate;

}

/**

* 根据map键获取对应的BigDecimal类型的值

* @param mapKey map键

* @return map值

*/

public BigDecimal getBigDecimal(String mapKey) {

//定义返回结果变量

BigDecimal resultBig = null;

// 根据key获取值

Object mapValue = this.get(mapKey);

try {

// 变量赋值

if(null == mapValue){

resultBig = null;

}else if (mapValue instanceof BigDecimal) {

resultBig = (BigDecimal) mapValue;

}else{

resultBig = new BigDecimal(String.valueOf(mapValue));

}

} catch (Exception e) {

logger.warn("根据map键[" + mapKey + "]获取对应的BigDecimal类型的值异常,原值[" + mapValue + "]");

}

// 返回结果

return resultBig;

}

/**

* 根据map键获取对应类型的值

* @param mapKey map键

* @param classType 类型

* @return map值

*/

public T getObject(String mapKey,Class classType) {

//定义返回结果变量

T resultObject = null;

// 根据key获取值

Object mapValue = this.get(mapKey);

try {

// 变量赋值

if(null == mapValue){

resultObject = null;

}else if (classType.isInstance(mapValue)) {

resultObject = (T) mapValue;

}else{

if(classType == BigDecimal.class){

resultObject = (T)getBigDecimal(mapKey);

}else if(classType == Date.class){

resultObject = (T)getDate(mapKey);

}

}

} catch (Exception e) {

logger.warn("根据map键[" + mapKey + "]获取对应的" + classType + "类型的值异常,原值[" + mapValue + "]");

}

// 返回结果

return resultObject;

}

public void reloadForPage(){

Integer pageStart = this.getInt("start");

Integer pageSize = this.getInt("pageSize");

if(null == pageSize){

return;

}

if(null == pageStart){

Integer pageNo = this.getInt("pageIndex");

if(null == pageNo){

return;

}

pageStart = pageNo * pageSize;

}

int pageLength = pageSize;

this.put("pageStart",pageStart);

this.put("pageLength",pageLength);

}

/**

* 将下划线的字符串转化为驼峰命名

* @param column

* @return

*/

private String renameForUnderlineToHump(String column) {

if(null == column){

return column;

}

if(!column.contains("_")){

return column;

}

column = column.toLowerCase();

StringBuffer sb = new StringBuffer();

Pattern p = Pattern.compile("_(\\w)");

Matcher m = p.matcher(column);

while (m.find()){

m.appendReplacement(sb,m.group(1).toUpperCase());

}

m.appendTail(sb);

return sb.toString();

}

/**

* 重写put方法

* @param key

* @param value

* @return

*/

@Override

public Object put(Object key, Object value) {

if(value instanceof Date){

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

value = sdf.format(value);

}

if(key instanceof String){

String keyStr = (String) key;

String newKey = renameForUnderlineToHump(keyStr);

return super.put(newKey,value);

}

return super.put(key, value);

}

/**

* 重写get方法

* @param key

* @return

*/

@Override

public Object get(Object key) {

if(key instanceof String){

String keyStr = (String) key;

String newKey = renameForUnderlineToHump(keyStr);

return super.get(newKey);

}

return super.get(key);

}

/**

* map转BaseMapEntity

* @param map map对象

* @return BaseMapEntity

*/

public static BaseMapEntity getBaseMapEntity(Map map) {

// 定义resultMapVO

BaseMapEntity resultMapVO = new BaseMapEntity();

if (null == map) {

return resultMapVO;

}

// 将map放入resultMapVO中

resultMapVO.putAll(map);

// 返回结果

return resultMapVO;

}

/**

* obj转BaseMapEntity

* @param obj map对象

* @return BaseMapEntity

*/

public static BaseMapEntity getBaseMapEntityForObj(Object obj) {

if(null == obj){

return null;

}

String objStr = JSON.toJSONString(obj);

BaseMapEntity resultMapVO = JSON.parseObject(objStr,BaseMapEntity.class);

// 返回结果

return resultMapVO;

}

/**

* 转化成页面需要的格式

*/

public void tranToCMSPageObj(){

this.put("recordsTotal",this.get("total"));

this.put("recordsFiltered",this.get("total"));

this.put("start",this.get("pageStart"));

this.put("length",this.get("pageLength"));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值