//前台以form-data格式传递大量数据 name:desk //string type:1 //int isOk:true //boolean doublee:5.87 //double .........
//controller
package com.base.controllerData;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/test")
public class TestController extends BaseController {
@Autowired
private TestService testService;
@RequestMapping(value = "/query", method = RequestMethod.GET)
@ResponseBody
public void query() {
PageData pd = this.getPageData();
String name = pd.getString("name");
int type = pd.getInt("type");
boolean isOk = pd.getBoolean("isOk");
double doublee = pd.getDouble("doublee");
List
}
}
package com.base.controllerData;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
/**
* 对象功能:BaseController基类
*
*/
public class BaseController{
/**
* new PageData对象
*
* @return
*/
public PageData getPageData() {
return new PageData(this.getRequest());
}
/**
* 得到request对象
*
* @return
*/
public HttpServletRequest getRequest() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest();
return request;
}
/**
* 得到response对象
*
* @return
*/
public HttpServletResponse getResponse() {
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getResponse();
return response;
}
}
package com.base.controllerData;
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:用于封装参数和Mysql数据对象
*/
@SuppressWarnings("rawtypes")
public class PageData extends HashMap implements Map {
private static final long serialVersionUID = 1L;
Map map = null;
HttpServletRequest request;
@SuppressWarnings("unchecked")
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();
}
public PageData(Object key,Object value) {
map = new HashMap();
put(key, value);
}
@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) {
Object val = get(key);
if (val != null && !"".equals(val)) {
return val.toString();
} else {
return "";
}
}
public Double getDouble(Object key) {
Object val = get(key);
if (val != null && !"".equals(val)) {
return Double.valueOf(val.toString());
} else {
return 0d;
}
}
public Integer getInt(Object key) {
Object val = get(key);
if (val != null && !"".equals(val)) {
return Integer.valueOf(val.toString());
} else {
return null;
}
}
public Boolean getBoolean(Object key) {
Object val = get(key);
if (val != null && !"".equals(val)) {
return Boolean.parseBoolean(val.toString());
} else {
return false;
}
}
@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) {
}
}
return map.put(key, value);
}
@Override
public Object remove(Object key) {
return map.remove(key);
}
@Override
public void clear() {
map.clear();
}
@Override
public boolean containsKey(Object key) {
return map.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return map.containsValue(value);
}
@Override
public Set entrySet() {
return map.entrySet();
}
@Override
public boolean isEmpty() {
return map.isEmpty();
}
@Override
public Set keySet() {
return map.keySet();
}
@SuppressWarnings("unchecked")
@Override
public void putAll(Map t) {
map.putAll(t);
}
@Override
public int size() {
return map.size();
}
@Override
public Collection values() {
return map.values();
}
}
//service
package com.base.controllerData;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
public class TestService {
@Autowired
private BaseDao dao;
public List
return (List
}
}
//mapper文件 //mybatis配置文件name, type, isOk, doublee
代码地址:https://github.com/1542196732/controllerData