写了一天的代码了,休息一下总结一下。
这段代码的背景是 通过前端配置表于表之间的关系,后端根据配置信息进行 表于表之间的数据互动
1、通过前端的配置信息封装一个 list<dto> dto里面存着 对应的表名,对应表的实体类属性关系
2、根据前端传来的原始VO,通过反射 以实体属性作为key 属性value作为value 存储一个voMap
3、创建一个paramMap,根据封装好的 list<dto> 和 voMap进行 封装paramMap
4、最后就可以拿着paramMap去插入数据库了
getInsertParam()直接调用这个方法就可以获得paramMap
package nbpt.ts.zhaf.util;
import nbpt.ts.zhaf.domain.dto.StoreGoodsActionDTO;
import nbpt.ts.zhaf.util.enums.TableEnum;
import org.springframework.util.StringUtils;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 表之间的数据互通说明(主表-子表;子表-主表 都可互相转换对应):
* 一、后台根据配置添加对应表的思路(子表转换主表):
* 1、调用ActionUtil.storeGoodsActionAllTable(配置,表名)方法返回 List<List<StoreGoodsActionDTO>> actionList
* a)拿到封装好的配置
* a. sgaProperty;//主表字段属性
* b. otherProperty;//其他表的字段属性
* c. ortherTableName;//其他表的名称
*
* 2、调用ActionUtil.getParamList(空map,类)通过反射获取类中的属性,属性不为空的 以属性名称作为key,属性值作为value存储一个map(在此取名params)
* 3、创建一个map作为insert的param
* 4、循环actionList 根据 sgaProperty;//主表字段属性 对应的 otherProperty;//其他表的字段属性进行
* 组装map(在此取名paramMap) 以sgaProperty;//主表字段属性作为key 通过对应的 otherProperty;//其他表的字段属性 获取 反射组装的map(params)中的值
* 5、将组装好的paramMap传给 mapper层进行数据库操作
*
* 二、前端根据配置回显form表单的操作(主表转换子表)
* 1、调用ActionUtil.storeGoodsActionAllTable(配置,表名)方法返回 List<List<StoreGoodsActionDTO>> actionList 通过ModelMap传输给前台
* 2、前端代码示例根据 表之间属性对应的list 进行 根据jquery组装回显表单
* var storeGoodsAction = ${t_ordergoods_peijian};
* for (var i = 0; i < storeGoodsAction.length; i++) {
* var itemProperty = storeGoodsAction[i].sgaProperty//主表字段属性
* if(item[itemProperty]==""){
* $('#' + storeGoodsAction[i].otherProperty + '').val("暂无");
* }else{
* $('#' + storeGoodsAction[i].otherProperty + '').val(item[itemProperty]);
* }
* }
*/
public class ActionUtil {
/**
*子表insert主表
* @param action 配置的对应关系
* @param tableKey 对应的表名称
* @param paramClass 参数的class
* @param companyid 公司id
* @return
* @throws Exception
*/
public static Map<String,Object> getMainInsertParam(String action,String tableKey,Object paramClass,Integer companyid) throws Exception {
List<List<StoreGoodsActionDTO>> actionList = ActionUtil.storeGoodsActionAllTable(action, tableKey);
Map params = new HashMap<String, Object>();
params = ActionUtil.getParamList(params, paramClass);
Map<String, Object> paramMap = new HashMap<>();
for (StoreGoodsActionDTO storeGoodsActionDTO : actionList.get(0)) {
//封装map
paramMap.put(storeGoodsActionDTO.getOtherProperty(), params.get(storeGoodsActionDTO.getSgaProperty()));
}
paramMap.put("companyid", companyid);
return paramMap;
}
/**
*主表insert子表
* @param action 配置的对应关系
* @param tableKey 对应的表名称
* @param paramClass 参数的class
* @param companyid 公司id
* @return
* @throws Exception
*/
public static Map<String,Object> getChildInsertParam(String action,String tableKey,Object paramClass,Integer companyid) throws Exception {
List<List<StoreGoodsActionDTO>> actionList = ActionUtil.storeGoodsActionAllTable(action, tableKey);
Map params = new HashMap<String, Object>();
params = ActionUtil.getParamList(params, paramClass);
Map<String, Object> paramMap = new HashMap<>();
for (StoreGoodsActionDTO storeGoodsActionDTO : actionList.get(0)) {
//封装map
paramMap.put(storeGoodsActionDTO.getSgaProperty(), params.get(storeGoodsActionDTO.getOtherProperty()));
}
paramMap.put("companyid", companyid);
return paramMap;
}
/**
*
* @param action
* @param ortherTableNameVO 对应其他表的表名(不为空时查询指定的 封装成List<List<StoreGoodsActionDTO>>
* 为null/""时查询所有表配置 封装成List<List<StoreGoodsActionDTO>>)
* @return
* @throws Exception
*/
public static List<List<StoreGoodsActionDTO>> storeGoodsActionAllTable(String action, String ortherTableNameVO) throws Exception {
List<List<StoreGoodsActionDTO>> allTableAction = new ArrayList<>();
String[] tableAction = action.split(";");
for (int a = 0; a < tableAction.length; a++) {
List<StoreGoodsActionDTO> tableActionList = new ArrayList<>();
String[] tablePropertys = tableAction[a].split("\\|");
if(StringUtils.isEmpty(ortherTableNameVO)){
allTableAction = get(tablePropertys,tableAction,tableActionList,allTableAction);
}else if (ortherTableNameVO.equals(tablePropertys[0])) {
allTableAction = get(tablePropertys,tableAction,tableActionList,allTableAction);
}
}
return allTableAction;
}
public static List<List<StoreGoodsActionDTO>> get(String[] tablePropertys, String[] tableAction, List<StoreGoodsActionDTO> tableActionList, List<List<StoreGoodsActionDTO>> allTableAction) throws Exception {
if (tablePropertys.length != 3) {
throw new Exception("第" + tableAction[0] + "个表关联 配置格式有问题!");
}
String[] tableOtherPropertys = tablePropertys[1].split(",");
String[] tableStoreGoodsPropertys = tablePropertys[2].split(",");
if (tableOtherPropertys.length != tableStoreGoodsPropertys.length) {
throw new Exception(tablePropertys[0] + "表配置字段无法对应,请配置相对应的字段!");
}
String ortherTableName = tablePropertys[0];
for (int i = 0; i < tableOtherPropertys.length; i++) {
StoreGoodsActionDTO correspondingDTO = new StoreGoodsActionDTO();
correspondingDTO.setOrtherTableName(ortherTableName);
correspondingDTO.setOtherProperty(tableOtherPropertys[i]);
correspondingDTO.setSgaProperty(tableStoreGoodsPropertys[i]);
tableActionList.add(correspondingDTO);
}
allTableAction.add(tableActionList);
return allTableAction;
}
/**
*
* @param params 空map用于返回 和 以后的扩展
* @param vo 带有参数的vo
* @return
*/
public static Map<String, Object> getParamList(Map<String, Object> params, Object vo) {
Field[] vos = vo.getClass().getDeclaredFields();
for (Field field : vos) {
field.setAccessible(true);
try {
if (!StringUtils.isEmpty(field.get(vo))) {
params.put(field.getName(),field.get(vo));
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return params;
}
}