为了方便项目中一些方法之间数据传输以及前端参数的接收,自定义多功能参数传递Dto接口,继承自map接口
package com.donlim.fms.common.dto;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
/**
* <b>数据传输对象接口</b>
* <p>
* 对原生Java Map类型的二次包装,提供<b><i>更加方便的存取API、更强的容错和类型转换机制。</i></b>
* 在平台二次开发过程中具有很强的实用价值。 开发人员需熟练掌握其提供的相关API。
* </p>
* 李安山
*
*/
public interface Dto extends Map<String, Object> {
// Dto里的一个缺省叫mycat的键
// public static final String KEY_MYCAT = "myCat";
/**
* 以Integer类型返回属性
*
* @param pKey
* @return Integer 键值
*/
Integer getInteger(String pKey);
/**
* 以BigInteger类型返回属性
*
* @param pKey
* @return BigInteger 键值
*/
BigInteger getBigInteger(String pKey);
/**
* 以Long类型返回属性
*
* @param pKey
* @return Long 键值
*/
Long getLong(String pKey);
/**
* 以String类型返回属性
*
* @param pKey
* @return String 键值
*/
String getString(String pKey);
/**
* 以BigDecimal类型返回属性
*
* @param pKey
* @return BigDecimal 键值
*/
BigDecimal getBigDecimal(String pKey);
/**
* 以Date类型返回属性
*
* @param pKey
* @return Date 键值(yyyy-MM-dd)
*/
Date getDate(String pKey);
/**
* 以Timestamp类型返回属性
*
* @param pKey
* @return Timestamp 键值(yyyy-MM-dd HH:mm:ss)
*/
Timestamp getTimestamp(String pKey);
/**
* 以Boolean类型返回属性
*
* @param pKey
* @return Boolean 键值
*/
Boolean getBoolean(String pKey);
/**
* 以List类型返回属性
*
* @param pKey
* @return List 键值
*/
List<? extends Object> getList(String pKey);
//
// /**
// * 获取缺省对象:myCat
// *
// * @return
// */
// Object getMyCat();
//
// /**
// * 设置缺省对象:myCat
// *
// * @param myCat
// */
// void setMyCat(Object myCat);
/**
* 支持链式put
*
* @param key
* 键
* @param value
* 值
* @return 当前对象
*/
Dto set(String key, Object value);
}
然后实现此Dto接口
package com.donlim.fms.common.dto;
import com.donlim.fms.common.dto.util.TypeConvertUtil;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
/**
* <b>数据传输对象实现</b>
* <p>
* 对原生Java Map类型的二次包装,提供<b><i>更加方便的存取API、更强的容错和类型转换机制。</i></b>
* 在平台二次开发过程中具有很强的实用价值。 开发人员需熟练掌握其提供的相关API。
* </p>
* - 基于MapUtil重新实现
* 李安山
*/
public class HashDto extends HashMap<String, Object> implements Dto{
private static final long serialVersionUID = 1L;
/**
* 缺省构造函数
*/
public HashDto() {
}
/**
* 以Integer类型返回属性
*
* @param pKey
* @return Integer 键值
* @throws Exception
*/
@Override
public Integer getInteger(String pKey) {
Object obj = TypeConvertUtil.convert(get(pKey), "Integer", null);
if (obj != null) {
return (Integer) obj;
} else {
return null;
}
}
/**
* 以BigInteger类型返回属性
*
* @param pKey
* @return BigInteger 键值
*/
@Override
public BigInteger getBigInteger(String pKey) {
BigInteger outValue = null;
Object obj = get(pKey);
if (obj instanceof BigInteger) {
outValue = (BigInteger) obj;
} else {
outValue = new BigInteger(getString(pKey));
}
return outValue;
}
/**
* 以Long类型返回属性
*
* @param pKey
* @return Long 键值
*/
@Override
public Long getLong(String pKey) {
Object obj = TypeConvertUtil.convert(get(pKey), "Long", null);
if (obj != null) {
return (Long) obj;
} else {
return null;
}
}
/**
* 以String类型返回属性
*
* @param pKey
* @return String 键值
*/
@Override
public String getString(String pKey) {
Object obj = TypeConvertUtil.convert(get(pKey), "String", null);
if (obj != null) {
return (String) obj;
} else {
return "";
}
}
/**
* 以BigDecimal类型返回属性
*
* @param pKey
* @return BigDecimal 键值
*/
@Override
public BigDecimal getBigDecimal(String pKey) {
Object obj = TypeConvertUtil.convert(get(pKey), "BigDecimal", null);
if (obj != null) {
return (BigDecimal) obj;
} else {
return null;
}
}
/**
* 以Date类型返回属性
*
* @param pKey
* @return Date 键值(yyyy-MM-dd)
*/
@Override
public Date getDate(String pKey) {
Object obj = TypeConvertUtil.convert(get(pKey), "Date", "yyyy-MM-dd");
if (obj != null) {
return (Date) obj;
} else {
return null;
}
}
/**
* 以Timestamp类型返回属性
*
* @param pKey
* @return Timestamp 键值(yyyy-MM-dd HH:mm:ss)
*/
@Override
public Timestamp getTimestamp(String pKey) {
Object obj = TypeConvertUtil.convert(get(pKey), "Timestamp", "yyyy-MM-dd HH:mm:ss");
if (obj != null) {
return (Timestamp) obj;
} else {
return null;
}
}
/**
* 以Boolean类型返回属性
*
* @param pKey
* @return Boolean 键值
*/
@Override
public Boolean getBoolean(String pKey) {
Object obj = TypeConvertUtil.convert(get(pKey), "Boolean", null);
if (obj != null) {
return (Boolean) obj;
} else {
return null;
}
}
/**
* 以List类型返回属性
*
* @param pKey
* @return List 键值
*/
@SuppressWarnings("unchecked")
@Override
public List<? extends Object> getList(String pKey) {
return (List<? extends Object>) get(pKey);
}
/**
* 支持链式put
* @param key
* @param value
* @return 当前对象
*/
@Override
public Dto set(String key, Object value) {
put(key, value);
return this;
}
}
然后就可以通过多态的形式使用此dto进行数据的传输
Dto dto = new HsahDto();
为了方便创造此对象,再编写一个生成此对象的工具类,以及接受request对象前端传进来的参数,
package com.donlim.fms.common.dto;
import com.alibaba.fastjson.JSON;
import com.donlim.fms.common.dto.util.Util;
import org.apache.commons.lang.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.Map;
/**
* <b>数据传输对象实例化辅助类</b>
* 李安山
*/
public class Dtos {
private static final String PAGE_START = "start";
private static final String PAGE_LENGTH = "length";
/**
* 创建一个常规的Dto对象
*
*/
public static Dto newDto() {
return new HashDto();
}
/**
* 创建一个常规携带Request参数的Dto对象
*
* @param request
* @return
*/
public static Dto newDto(HttpServletRequest request) {
try {
Dto dto = new HashDto();
Map<String, String[]> map = request.getParameterMap();
Iterator<String> keyIterator = (Iterator) map.keySet().iterator();
while (keyIterator.hasNext()) {
String key = (String) keyIterator.next();
String value = map.get(key)[0];
dto.put(key, value);
}
String json = Util.getRequestJsonString(request);
if(StringUtils.isNotBlank(json)&& Util.isJson(json)==true){
Map mapJson = JSON.parseObject(json);
for (Object obj : mapJson.keySet()) {
String key = obj.toString();
String value = null;
if(mapJson.get(obj)!=null){
value=mapJson.get(obj).toString();
}
dto.put(key, value);
}
dto.put("api-json",json);
}else{
// throw new BizException(APIErrorCode.code.code403.getValue(), "参数错误!请使用json格式");
}
return dto;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 在Map的基础上克隆一个常规Dto对象
* @param map
* @return
*/
public static Dto newDto(Map<String, ?> map) {
Dto newDto = new HashDto();
if (map != null) {
newDto.putAll(map);
}
return newDto;
}
/**
* 创建一个常规的Dto对象,并初始化一个键值对。
*
* @param keyString
* @param valueObject
* @return
*/
public static Dto newDto(String keyString, Object valueObject) {
Dto dto = new HashDto();
dto.put(keyString, valueObject);
return dto;
}
/**
* 创建一个分页Dto对象
* <p>这里会对分页参数做一些校验和转换
*
* @return
*/
// public static Dto newPageDto(HttpServletRequest request) {
// Dto newDto = newDto(request);
// if(newDto == null){
// throw new BizException(APIErrorCode.code.code400.getValue(), "获取参数为空。");
// }
//
// if (Util.isEmpty(newDto.get("page")) || Util.isEmpty(newDto.get("limit"))) {
// //如果使用了pageDto,但又缺失分页参数,则抛出异常
// throw new BizException(APIErrorCode.code.code400.getValue(), "列表查询出错。分页参数(page | limit)缺失,请确认。");
// }else {
// int length = newDto.getInteger("limit");
// newDto.put(PAGE_START, (newDto.getInteger("page")-1) * length);
// newDto.put(PAGE_LENGTH, length);
// }
// return newDto;
// }
}
在controller中,直接可以将request对象传给Dtos类,让它自动解析
@PostMapping("list")
public OutVO list(HttpServletRequest request){
return sealService.list(Dtos.newDto(request));
}
此dto可以一直作为查询参数传递到mapper.xml文件中,大大简化了前端很多杂乱参数的收集工作。而且接口之间互调,也可以作为参数传递,无需定义很多无用的参数传递类,大大简化了开发。使代码更简洁