尤其是SpringMVC和Mybatis一起用的时候,用Map大大减少了需要的bean/vo/po之类的东东。
用Map也会遇到一个问题,就是类型转换的代码特别的多。
/** * 得到当前页面的页数 */ private int getCurrentPage(Map<String, Object> params){ return Integer.parseInt(params.get(PageUtils.CURRENT_PAGE).toString()); }
从Map中获得一个Object类型的值,然后toString,再次转换为目标Integer等类型。
不够简洁,即使提取了工具方法,代码也还是有点繁琐。
因此,我对Map进行了一次封装。(有人其实已经这么做了,我也是受到别人的启发,才决定进行封装的。 只不过,我需要简洁的符合自己偏好的而已。)
使用方式
@RequestMapping(value = "/list") @ResponseBody public PageVO list(@RequestParam TypedMap<String, Object> params) { Integer age = params.getInt("age"); String name = params.getString("name"); }
TypedMap代码实现
package cn.fansunion.common.collection; import java.util.Date; import java.util.HashMap; import org.apache.commons.lang3.StringUtils; import cn.fansunion.common.converter.Integers; import cn.fansunion.common.converter.Longs; import cn.fansunion.common.util.TimeUtil; /** * 带类型的Map,在Map的基础上,增加了getInt,getLong等工具方法。 * * @author leiwen * */ public class TypedMap<K, V> extends HashMap<K, V> { private static final long serialVersionUID = 1L; /** * 从集合中获得一个Integer类型的值。 * * @param key * 键 * @param defaultValue * 默认值,如果根据key没有找到对应的值,使用该默认值 * @return 该key对应的值或者为defaultValue */ public Integer getInt(String key, Integer defaultValue) { Object v = get(key); Integer result = Integers.valueOf(v, defaultValue); return result; } /** * 根据key获得Integer类型的值。 * * @param key * 键 * @return 该key对应的值,如果没有找到,则返回null */ public Integer getInt(String key) { return getInt(key, null); } /** * 从集合中获得一个String类型的值。 * * @param key * 键 * @param defaultValue * 默认值,如果根据key没有找到对应的值,使用该默认值 * @return 该key对应的值或者为defaultValue */ public String getString(String key, String defaultValue) { Object value = get(key); if (value == null) { return defaultValue; } String result = defaultValue; if (value instanceof String) { result = (String) value; } else { result = String.valueOf(value); } return result; } /** * 根据key获得String类型的值。 * * @param key * 键 * @return 该key对应的值,如果没有找到,则返回null */ public String getString(String key) { return getString(key, null); } /** * 根据key获得Long类型的值。 * * @param key * 键 * @return 该key对应的值,如果没有找到,则返回null */ public Long getLong(String key) { return getLong(key, null); } /** * 从集合中获得一个Long类型的值。 * * @param key * 键 * @param defaultValue * 默认值,如果根据key没有找到对应的值,使用该默认值 * @return 该key对应的值或者为defaultValue */ public Long getLong(String key, Long defaultValue) { return Longs.valueOf(get(key), defaultValue); } public Boolean getBoolean(String key, Boolean defaultValue) { Object value = get(key); if (value == null) { return defaultValue; } if (Boolean.class.isInstance(value)) { return (Boolean) value; } else { return defaultValue; } } /** * 根据key获得Boolean类型的值 * * @param key * 键 * @return 该键对应的值,如果没有找到,返回null */ public Boolean getBoolean(String key) { return getBoolean(key, null); } /** * 从集合中获得一个Date类型的值。 * * @param key * 键 * @return 该key对应的值,如果没有找到,返回null */ public Date getDate(String key) { return getDate(key, null); } /** * 从集合中获得一个Date类型的值。 * * @param key * 键 * @param defaultValue * 默认值,如果根据key没有找到对应的值,使用该默认值 * @return 该key对应的值或者为defaultValue */ public Date getDate(String key, Date defualtValue) { Object value = get(key); if (value == null) { return null; } if (value instanceof Date) { return (Date) value; } else { String str = value.toString(); if (StringUtils.isNotEmpty(str)) { return TimeUtil.parseYmdHms(key); } } return defualtValue; } /** * 根据key获得目标类型的值 * * @param key * 键 * @param type * 目标类型 * @return 该键对应的值,如果没有找到,返回null */ public <T> T getTypedObject(String key, T type) { Object value = get(key); if (value == null) { return null; } return (T) value; } }
TypedMap单元测试代码
package cn.fansunion.common.collection; import java.util.Date; import junit.framework.TestCase; import org.junit.Test; /** * @author leiwen */ public class TypedMapTest extends TestCase { @Test public static void testGetInt() { TypedMap<String, Object> map = createMap(); Integer value = 198962; map.put("intKey", value); assertEquals(value, map.getInt("intKey")); assertNull(map.getInt("notExistIntKey")); } @Test public static void testGetIntWithDefaultValue() { TypedMap<String, Object> map = createMap(); Integer defaultValue = 0; Integer value = 198962; map.put("intKey", value); assertEquals(value, map.getInt("intKey", defaultValue)); assertEquals(defaultValue, map.getInt("notExistIntKey", defaultValue)); } @Test public static void testGetLong() { TypedMap<String, Object> map = createMap(); Long value = 198962L; map.put("longKey", value); assertEquals(value, map.getLong("longKey")); assertNull(map.getLong("notExistLongKey")); } @Test public static void testGetLongWithDefualtValue() { TypedMap<String, Object> map = createMap(); Long defaultValue = 0L; Long value = 198962L; map.put("longKey", value); assertEquals(value, map.getLong("longKey", defaultValue)); assertEquals(defaultValue, map.getLong("notExistLongKey", defaultValue)); } @Test public static void testGetString() { TypedMap<String, Object> map = createMap(); String value = "http://FansUnion.cn"; map.put("stringKey", value); assertEquals(value, map.getString("stringKey")); assertNull(map.getString("notExistStringKey")); } @Test public static void testGetStringWithDefualtValue() { TypedMap<String, Object> map = createMap(); String defaultValue = "leiwen"; String value = "http://FansUnion.cn"; map.put("stringKey", value); assertEquals(value, map.getString("stringKey", defaultValue)); assertEquals(defaultValue, map.getString("notExistStringKey", defaultValue)); } @Test public static void testGetDate() { TypedMap<String, Object> map = createMap(); Date value = new Date(); map.put("dateKey", value); assertEquals(value, map.getDate("dateKey")); assertNull(map.getDate("notExistDateKey")); } @Test public static void testGetDateWithDefualtValue() { TypedMap<String, Object> map = createMap(); Date defaultValue = null; Date value = new Date(); map.put("dateKey", value); assertEquals(value, map.getDate("dateKey", defaultValue)); assertEquals(defaultValue, map.getDate("notExistDateKey", defaultValue)); } @Test public static void testGetBoolean() { TypedMap<String, Object> map = createMap(); Boolean value = false; map.put("booleanKey", value); assertEquals(value, map.getBoolean("booleanKey")); assertNull(map.getBoolean("notExistBooleanKey")); } @Test public static void testGetBooleanWithDefaultValue() { TypedMap<String, Object> map = createMap(); Boolean defaultValue = false; Boolean value = true; map.put("booleanKey", value); assertEquals(value, map.getBoolean("booleanKey", defaultValue)); assertEquals(defaultValue, map.getBoolean("notExistBooleanKey", defaultValue)); } @Test public static void testGetTypedObject() { TypedMap<String, Object> map = createMap(); Integer value = 198962; map.put("intKey", value); assertEquals(value, map.getTypedObject("intKey", Integer.class)); } public static TypedMap<String, Object> createMap() { return new TypedMap<String, Object>(); } }
原文参见: 提高生产力:SpringMVC中,使用扩展数据类型TypedMap接收Web请求参数
相关阅读: 提高生产力