一个超级简单版本的,本来不想发的,结果被文强大哥所逼,只能发了。
首先是vue的混合代码:
export default {
data: {
tableData: {
current: '',
pageSize: '',
total: 0,
},//表格默认的参数,请不要添加额外参数
tableFilter: {},//单表的参数
paramMap: new Map(),// 如果说,你还想有其他的额外参数,请放到这里
},
methods: {
/**
* 查询表格方法,任何引入了该混合的vue,刷新表格都用这个
*/
getTableData(cb) {
const extraData = {}
const filters = {}
let key
for (key in this.tableData)
typeof this.tableData[key] !== "undefined" && (extraData[key] = this.tableData[key])
for (key in this.tableFilter)
typeof this.tableFilter[key] !== "undefined" && (filters[key] = this.tableFilter[key])
for (key in this.paramMap)
typeof this.paramMap[key] !== "undefined"
&& (!extraData.hasOwnProperty(key)) && (extraData[key] = this.paramMap[key])
extraData['filters'] = filters
cb && cb(extraData)
},
}
}
vue引入:
export default {
data: {
tableData: {
current: '',
pageSize: '',
total: 0,
},//表格默认的参数,请不要添加额外参数
tableFilter: {},//单表的参数
paramMap: new Map(),// 如果说,你还想有其他的额外参数,请放到这里
},
methods: {
/**
* 查询表格方法,任何引入了该混合的vue,刷新表格都用这个
*/
getTableData(cb) {
const extraData = {}
const filters = {}
let key
for (key in this.tableData)
typeof this.tableData[key] !== "undefined" && (extraData[key] = this.tableData[key])
for (key in this.tableFilter)
typeof this.tableFilter[key] !== "undefined" && (filters[key] = this.tableFilter[key])
for (key in this.paramMap)
typeof this.paramMap[key] !== "undefined"
&& (!extraData.hasOwnProperty(key)) && (extraData[key] = this.paramMap[key])
extraData['filters'] = filters
cb && cb(extraData)
},
}
}
然后是java的
第一个基本对象,包含分页参数
package com.jianvin.admin.util;
import java.util.List;
import java.util.Map;
public class TableFilter {
private Integer current;
private Integer pageSize;
private Map<String, List<String>> filters;
public Integer getCurrent() {
return current;
}
public void setCurrent(Integer current) {
this.current = current;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Map<String, List<String>> getFilters() {
return filters;
}
public void setFilters(Map<String, List<String>> filters) {
this.filters = filters;
}
}
接着写一个类继承,该类保存额外参数
package com.jianvin.admin.util;
import javax.validation.constraints.NotNull;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* 此类用于前端接收数据
*/
public class TableRequest<K, V> extends TableFilter implements Map<K, V> {
private Map<K, V> map;
private Boolean isSync;
public TableRequest() {
this(false);
}
public TableFilter tableFilter() throws IllegalAccessException {
return this.convertMapToObj(this, TableFilter.class);
}
public TableRequest(Boolean isSync) {
if (isSync == null || !isSync) {
this.map = new HashMap<>();
} else this.map = new ConcurrentHashMap<>();
this.isSync = isSync;
}
@Override
public int size() {
return this.map.size();
}
@Override
public boolean isEmpty() {
return this.map.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return this.map.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return this.map.containsValue(value);
}
@Override
public V get(Object key) {
return this.map.get(key);
}
@Override
public V put(K key, V value) {
return this.map.put(key, value);
}
@Override
public V remove(Object key) {
return this.map.remove(key);
}
@Override
public void putAll(@NotNull Map<? extends K, ? extends V> m) {
this.map.putAll(m);
}
@Override
public void clear() {
this.map.clear();
}
@Override
public Set<K> keySet() {
return this.map.keySet();
}
@Override
public Collection<V> values() {
return this.map.values();
}
@Override
public Set<Entry<K, V>> entrySet() {
return this.map.entrySet();
}
public String gString(String key) {
Object o = this.map.get(key);
if (o instanceof String) return (String) o;
else if (o == null) return "";
else return String.valueOf(o);
}
public Integer gInt(String key) {
Object o = this.map.get(key);
if (o instanceof Integer) return (Integer) o;
else return null;
}
public Map<String, Object> gMap(String key) {
Object o = this.map.get(key);
if (o instanceof Map) return (Map) o;
else return null;
}
public <T> T getT(String key, Class<? extends T> tClass) throws IllegalAccessException {
Object o = this.map.get(key);
if (o instanceof Map) {
Object o1 = this.convertMapToObj((Map) o, tClass);
if (o1 != null)
return (T) o1;
}
return null;
}
public Map<String, Object> convertObjToMap(Object obj) throws IllegalAccessException {
if (obj == null)
throw new IllegalAccessException("{convertObjToMap}参数不能为空");
Map<String, Object> map = new HashMap<>();
Class<?> aClass = obj.getClass();
Field[] fields = aClass.getDeclaredFields();
Method method;
for (Field field : fields) {
try {
method = aClass.getMethod("get" + this.convertToUpper(field.getName()));
map.put(field.getName(), method.invoke(obj));
} catch (NoSuchMethodException e) {
continue;
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
return map;
}
public <T> T convertMapToObj(Map<K, V> map, Class<? extends T> tClass) throws IllegalAccessException {
try {
if (map == null || map.isEmpty() || tClass == null)
throw new IllegalAccessException();
T newT = tClass.getConstructor().newInstance();
Set<Entry<K, V>> entries = map.entrySet();
Method method;
Field field;
for (Entry<K, V> entry : entries) {
field = tClass.getDeclaredField(String.valueOf(entry.getKey()));
method = tClass.getMethod("set" + convertToUpper(String.valueOf(entry.getKey())), field.getType());
method.invoke(newT, entry.getValue());
}
return newT;
} catch (Exception e) {
throw new IllegalAccessException("转换失败");
}
}
public String convertToUpper(String str) throws IllegalAccessException {
if (str == null || str.length() == 0)
throw new IllegalAccessException("{convertToUpper}参数不能为空");
char[] chars = str.toCharArray();
if (65 <= chars[0] && chars[0] <= 81)
chars[0] = String.valueOf(chars[0]).toUpperCase().charAt(0);
return String.valueOf(chars);
}
}