哈喽。。。。我回来了。好久没写文章了,这段时间太忙了。上家公司倒闭了,这段时间找工作,熟悉新的工作,到现在入职了2周,现在终于有时间给大家分享技术了。


他们说我你分享的文章会被人讽刺的,写的太烂了,我不怕,只要我还有梦,我就一直写下去,人不死终会出头。

今天给大家分享一个万能的实体类。
相当于是一个map根据传什么值都可以

    package com.ylxy.util;

    import java.io.BufferedReader;
    import java.io.Reader;
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Set;

    import javax.servlet.http.HttpServletRequest;
    import com.alibaba.druid.proxy.jdbc.ClobProxyImpl;
    /** 
     * 说明:参数封装Map
     * @version
     */
    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();
        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);
    }

    @SuppressWarnings("unchecked")
    @Override
    public Object put(Object key, Object value) {
        if(value instanceof ClobProxyImpl){             //读取oracle Clob类型数据
            try {
                ClobProxyImpl cpi = (ClobProxyImpl)value;
                Reader is = cpi.getCharacterStream();     //获取流
                BufferedReader br = new BufferedReader(is);
                String str = br.readLine();
                StringBuffer sb = new StringBuffer();
                while(str != null){                        //循环读取数据拼接到字符串
                    sb.append(str);
                    sb.append("\n");
                    str = br.readLine();
                }
                value = sb.toString();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        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) {
        return map.containsKey(key);
    }

    public boolean containsValue(Object value){
        return map.containsValue(value);
    }

    public Set entrySet() {
        return map.entrySet();
    }

    public boolean isEmpty() {
        return map.isEmpty();
    }

    public Set keySet() {
        return map.keySet();
    }

    @SuppressWarnings("unchecked")
    public void putAll(Map t) {
        map.putAll(t);
    }

    public int size() {
        return map.size();
    }

    public Collection values() {
        return map.values();
        }
    }

使用方法:

/**
     * 显示文章列表
     * @throws Exception 
     */
    @RequestMapping(value="/findArticleList")
    public ModelAndView findArticleList(Page page) throws Exception{
        ModelAndView mv = this.getModelAndView();
        //直接new一个pageDate
        PageData pd = new PageData();
        //直接接受数据
        pd = this.getPageData();
    //封装sql所需要的数据
        pd.put("pu_uuid",Jurisdiction.getUserId());
        page.setPd(pd);
        List<PageData>    varList = articleService.findAllArticleList(page);            
        mv.addObject("varList", varList);
        mv.addObject("pd", pd);
        mv.setViewName("publics/article/article_list");
        return mv;
    }

这是service

    /**
     * 公众号用户下文章列表
     * @throws Exception 
     */
    @SuppressWarnings("unchecked")
    public List<PageData> findAllArticleList(Page page) throws Exception {
        return (List<PageData>) dao.findForList("ArticleMapper.articlelistPage", page);
    }

这是sql直接入参数是pageDate 和返回类型是pageDate

<select id="articlelistPage" parameterType="page" resultType="pd">
        select 
            aa_uuid,
            aa_title,
            aa_title_img,
            aa_edit_time,
            aa_collect_num,
            aa_comment_num,
            aa_preview_num,
            aa_parent_type,
            aa_sub_type,
            aa_status
        from app_article
        where 1 = 1
        <if test="pd.keywords != null and pd.keywords != ''">
            and (
                aa_title LIKE CONCAT(CONCAT('%', #{pd.keywords}),'%')
                or
                aa_content LIKE CONCAT(CONCAT('%', #{pd.keywords}),'%')
                )
        </if>
        and pu_uuid = #{pd.pu_uuid}
        order by aa_edit_time desc
    </select>

鼓励作者写出更好的技术文档,就请我喝一瓶哇哈哈哈哈哈哈哈。。
微信:

支付宝:

感谢一路支持我的人。。。。。
Love me and hold me
QQ:69673804(16年老号)
EMAIL:69673804@qq.com
友链交换
如果有兴趣和本博客交换友链的话,请按照下面的格式在评论区进行评论,我会尽快添加上你的链接。

网站名称:KingYiFan’S Blog
网站地址:http://blog.cnbuilder.cn
网站描述:年少是你未醒的梦话,风华是燃烬的彼岸花。
网站Logo/头像:http://blog.cnbuilder.cn/upload/2018/7/avatar20180720144536200.jpg