一、前言
关于jfinal3.0的自定义HashMap用于业务层传参与返回值的com.jfinal.kit.JMap类,对参数或者返回值封装,常用于业务层传参与返回值,具体应用实例如下
JMap ---> JFinal Map @b@ 参数或者返回值封装,常用于业务层传参与返回值@b@ 1:JMap para = JMap.create("id", 123);@b@ User user = user.findFirst(getSqlPara("find", para));@b@ @b@ 2:return JMap.fail("msg", "用户名或密码错误"); // 登录失败返回@b@ return JMap.ok("loginUser", user); // 登录成功返回@b@ @b@ 3:JMap map = loginService.login(...);@b@ renderJson(map);
二、源码说明
其中Json.getJson().toJson(this)参考其他章节
package com.jfinal.kit;@b@@b@import java.util.HashMap;@b@import java.util.Map;@b@import com.jfinal.json.Json;@b@@b@/**@b@ * JMap ---> JFinal Map @b@ * 参数或者返回值封装,常用于业务层传参与返回值@b@ * @b@ * Example:@b@ * 1:JMap para = JMap.create("id", 123);@b@ * User user = user.findFirst(getSqlPara("find", para));@b@ * @b@ * 2:return JMap.fail("msg", "用户名或密码错误"); // 登录失败返回@b@ * return JMap.ok("loginUser", user); // 登录成功返回@b@ * @b@ * 3:JMap map = loginService.login(...);@b@ * renderJson(map);@b@ */@b@@SuppressWarnings({"serial", "rawtypes", "unchecked"})@b@public class JMap extends HashMap {@b@@b@ private static final String STATUS_OK = "isOk";@b@ private static final String STATUS_FAIL = "isFail";@b@ @b@ public JMap() {@b@ }@b@ @b@ public static JMap ok() {@b@ return new JMap().setOk();@b@ }@b@ @b@ public static JMap ok(Object key, Object value) {@b@ return ok().set(key, value);@b@ }@b@ @b@ public static JMap fail() {@b@ return new JMap().setFail();@b@ }@b@ @b@ public static JMap fail(Object key, Object value) {@b@ return fail().set(key, value);@b@ }@b@ @b@ public static JMap create() {@b@ return new JMap();@b@ }@b@ @b@ public static JMap create(Object key, Object value) {@b@ return new JMap().set(key, value);@b@ }@b@ @b@ public JMap setOk() {@b@ super.put(STATUS_OK, Boolean.TRUE);@b@ super.put(STATUS_FAIL, Boolean.FALSE);@b@ return this;@b@ }@b@ @b@ public JMap setFail() {@b@ super.put(STATUS_OK, Boolean.FALSE);@b@ super.put(STATUS_FAIL, Boolean.TRUE);@b@ return this;@b@ }@b@ @b@ public boolean isOk() {@b@ Boolean isOk = (Boolean)get(STATUS_OK);@b@ return isOk != null && isOk;@b@ }@b@ @b@ public boolean isFail() {@b@ Boolean isFail = (Boolean)get(STATUS_FAIL);@b@ return isFail != null && isFail;@b@ }@b@ @b@ public JMap set(Object key, Object value) {@b@ super.put(key, value);@b@ return this;@b@ }@b@ @b@ public JMap set(Map map) {@b@ super.putAll(map);@b@ return this;@b@ }@b@ @b@ public JMap set(JMap jMap) {@b@ super.putAll(jMap);@b@ return this;@b@ }@b@ @b@ public JMap delete(Object key) {@b@ super.remove(key);@b@ return this;@b@ }@b@ @b@ public <T> T getAs(Object key) {@b@ return (T)get(key);@b@ }@b@ @b@ public String getStr(Object key) {@b@ return (String)get(key);@b@ }@b@@b@ public Integer getInt(Object key) {@b@ return (Integer)get(key);@b@ }@b@@b@ public Long getLong(Object key) {@b@ return (Long)get(key);@b@ }@b@@b@ public Boolean getBoolean(Object key) {@b@ return (Boolean)get(key);@b@ }@b@ @b@ /**@b@ * key 存在,并且 value 不为 null@b@ */@b@ public boolean notNull(Object key) {@b@ return get(key) != null;@b@ }@b@ @b@ /**@b@ * key 不存在,或者 key 存在但 value 为null@b@ */@b@ public boolean isNull(Object key) {@b@ return get(key) == null;@b@ }@b@ @b@ /**@b@ * key 存在,并且 value 为 true,则返回 true@b@ */@b@ public boolean isTrue(Object key) {@b@ Object value = get(key);@b@ return (value instanceof Boolean && ((Boolean)value == true));@b@ }@b@ @b@ /**@b@ * key 存在,并且 value 为 false,则返回 true@b@ */@b@ public boolean isFalse(Object key) {@b@ Object value = get(key);@b@ return (value instanceof Boolean && ((Boolean)value == false));@b@ }@b@ @b@ public String toJson() {@b@ return Json.getJson().toJson(this);@b@ }@b@ @b@ public boolean equals(Object jMap) {@b@ return jMap instanceof JMap && super.equals(jMap);@b@ }@b@}