万能实体类(pageDate)

哈喽。。。。我回来了。好久没写文章了,这段时间太忙了。上家公司倒闭了,这段时间找工作,熟悉新的工作,到现在入职了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() throws Exception{
    ModelAndView mv = this.getModelAndView();
    //直接new一个pageDate
    PageData pd = new PageData();
//封装sql所需要的数据
    pd.put("pu_uuid","123");
    page.setPd(pd);
    List<PageData>    varList = articleService.findAllArticleList(pd);            
    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(PageData pd) throws Exception {
    return (List<PageData>) dao.findForList("ArticleMapper.articlelistPage", pd);
}

这是sql直接入参数是pageDate 和返回类型是pageDate pd是别名是PageDate的别名

<select id="articlelistPage" parameterType="pd" 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

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
package com.model; import javax.servlet.http.HttpServletRequest; import java.util.*; /** * 说明:参数封装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(); Entry entry; String name = ""; String value = ""; while (entries.hasNext()) { entry = (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 static boolean hasVal(PageData pd,String key){ return pd!=null&&pd;.containsKey(key)&&!pd.getString(key).isEmpty(); } public String getString(Object key) { return (String)get(key); } public Integer getInt(Object key) { return Integer.parseInt(getString(key)); } @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
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值