SSM框架使用Map来替代实体Entity

Posted tandi19960505

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM框架使用Map来替代实体Entity相关的知识,希望对你有一定的参考价值。

基础控制器

public class BaseController {

    /**
     * 根据当前Request得到PageData
     *
     * @return the page data
     */
    public PageData getPageData() {
        return new PageData(this.getRequest());
    }

    /**
     * 得到ModelAndView
     *
     * @return the model and view
     */
    public ModelAndView getModelAndView() {
        return new ModelAndView();
    }

    /**
     * 得到request对象
     * @return the request
     */
    public HttpServletRequest getRequest() {
        // 利用spring api获取当前request对象
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request;
    }
}

封装来用户代替实体的map对象

/**
 * 页面数据对象(相当于实体),注意:PageData实质就是HashMap
 * 获取页面数据封装到Map中作为实体对象
 */
public class PageData extends HashMap implements Map {

    private static final long serialVersionUID = 1L;

    Map map = null;
    HttpServletRequest request;

    public PageData(HttpServletRequest request) {
        this.request = request;
        Map properties = request.getParameterMap(); // 获取所有参数 
        Map returnMap = new HashMap(); // String, String
        Iterator entries = properties.entrySet().iterator(); // 迭代参数
        Map.Entry entry;
        String name = "";
        String value = "";
        
        // 循环迭代
        while (entries.hasNext()) {
            
            entry = (Map.Entry) entries.next();
            name = (String) entry.getKey(); // 参数名称
            Object valueObj = entry.getValue(); // 参数值
            
            if (null == valueObj) {
                value = "";
            } else if (valueObj instanceof String[]) {
                String[] values = (String[]) valueObj;
                for (int i = 0; i < values.length; i++) {
                    value = values[i] + ",";
                }
                value = value.substring(0, value.length() - 1);
            } else {
                value = valueObj.toString();
            }
            
            returnMap.put(name, value);
        }
        
        
        map = returnMap;
    }

    public PageData() {
        map = new HashMap();
    }

    @Override
    public Object get(Object key) {
        Object obj = null;
        if (map.get(key) instanceof Object[]) {     // 获取的是对象数组
            Object[] arr = (Object[]) map.get(key);
            obj = request == null ? arr : (request.getParameter((String) key) == null ? arr : arr[0]);
        } else { 
            obj = map.get(key); // 获取的是对象
        }
        return obj;
    }

    public String getString(Object key) {
        return (String) get(key);
    }
    public double getDouble(Object key) {
        return (double) get(key);
    }
    public String getNoneNULLString(Object key) {
        String s = (String) get(key);
        return s==null?"":s;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Object put(Object key, Object value) {
        return map.put(key, value);
    }

    @Override
    public Object remove(Object key) {
        return map.remove(key);
    }

    public void clear() {
        map.clear();
    }

    public boolean containsKey(Object key) {
        // TODO Auto-generated method stub
        return map.containsKey(key);
    }

    public boolean containsValue(Object value) {
        // TODO Auto-generated method stub
        return map.containsValue(value);
    }

    public Set entrySet() {
        // TODO Auto-generated method stub
        return map.entrySet();
    }

    public boolean isEmpty() {
        // TODO Auto-generated method stub
        return map.isEmpty();
    }

    public Set keySet() {
        // TODO Auto-generated method stub
        return map.keySet();
    }

    @SuppressWarnings("unchecked")
    public void putAll(Map t) {
        // TODO Auto-generated method stub
        map.putAll(t);
    }

    public int size() {
        // TODO Auto-generated method stub
        return map.size();
    }

    public Collection values() {
        // TODO Auto-generated method stub
        return map.values();
    }

    @Override
    public String toString() {
        return "PageData [map=" + map + ", request=" + request + "]";
    }

}

以上是关于SSM框架使用Map来替代实体Entity的主要内容,如果未能解决你的问题,请参考以下文章

深入理解--SSM框架中Dao层,Mapper层,controller层,service层,model层,entity层都有什么作用

如何将嵌入式数据库与 Entity 框架一起使用?

JDK注解替代Hibernate的Entity映射

SSM框架MyBatis的各种查询功能

Entity Framework实体框架使用TrackerEnabledDbContext进行操作日志跟踪

什么是ssm框架