他们说我你分享的文章会被人讽刺的,写的太烂了,我不怕,只要我还有梦,我就一直写下去,人不死终会出头。
今天给大家分享一个万能的实体类。 相当于是一个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>