一种高效的数据对象转换方式(1)

04/12 13:40
阅读数 21

一:背景介绍

    在开发的过程当中,后端数据对象需要处理再传给前端,前端数据对象也需要处理再传给后端,这里往往需要建立不同数据对象来进行数据的转换处理。正是因为这种情况的存在,本文介绍一种高效的数据对象转换方式。

二:高效数据对象转换方式讲解

    怎么使用? 下面就是具体使用方法    

    public Object buildObj(Object obj, String gridName) {
        Map<String, Object> mapData = HubDataToMapParser.getInstance().toMapByObj(this.getClass(), gridName, obj);
        return buildResult(SUCCESS, mapData, null, null, gridName);
    }
    
    
    public Object buildList(List<?> list, String gridName) {
        List<Map<String, Object>> mapList = HubDataToMapParser.getInstance().toMapListByObjList(this.getClass(), gridName, list);
        Map<String, List<?>> mapData = getMapData(mapList);
        return buildResult(SUCCESS, mapData, null, null, gridName);
    }    
    
    public Object buildPage(Page<?> page, String gridName) {
        List<Map<String, Object>> list = HubDataToMapParser.getInstance().toMapListByObjList(this.getClass(), gridName, page.getResult());
        Map<String, List<?>> mapData = getMapData(list);
        return buildResult(SUCCESS, mapData, page, null, gridName);
    }
    
    public Object buildResult(String status, Object data, Page<?> page, String msg, String gridName) {
        Map<String, Object> root = new LinkedHashMap<String, Object>();
        root.put("status", status);
        if (null != msg) {
            root.put("message", msg);
        }
        if (null != gridName && isOpenHeader) {
            root.put("header", HubXmlCache.getInstance().toGridKvMap(this.getClass(), gridName));
        }
        if (null != data) {
            root.put("data", data);
        }
        if (null != page) {
            Map<String, Object> pagination = new HashMap<String, Object>();
            pagination.put("total", page.getTotal());
            pagination.put("pageSize", page.getPageSize());
            pagination.put("pageNum", page.getPageNum());
            root.put("page", pagination);
        }
        return root;
    }
    
    private Map<String, List<?>> getMapData(List<?> list) {
        Map<String, List<?>> map = new HashMap<>();
        map.put(ITEMS, list);
        return map;
    }

    本文通过xml的形式对数据对象进行处理,下面将以系统用户表信息为例。

    例如:SysUserController.java 对应的创建一个SysUserController.java

package yui.system.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import yui.bss.mgr.base.sys.ISysUserMgr;
import yui.bss.model.dto.ext.sys.SysUserDtox;
import yui.bss.model.vo.sys.SysUserVo;
import yui.comn.utils.CollUtils;
import yui.comn.utils.MD5Util;
import yui.comn.utils.Page;
import yui.comn.utils.orm.CatSysCode;
import yui.comn.utils.orm.MapperSqlClause;
import yui.comn.web.aop.Log;
import yui.comn.web.controller.BaseController;
import yui.system.web.ro.SysUserRo;

/**
 * 系统用户
 * @author yuyi   1060771195@qq.com
 */
@Api(value="系统用户")
@RestController
@RequestMapping("sys/user")
public class SysUserController extends BaseController {
    @Reference
    private ISysUserMgr sysUserMgr;

    @Log
    @ApiOperation(value = "系统用户分页列表") 
    @GetMapping("page")  
    public Object page(String query) { 
        Page<SysUserDtox> page = sysUserMgr.pageSysUserBySql(getMapperSqlClause(query)); 
        return buildPage(page, LIST); 
    } 
     
    @Log
    @ApiOperation(value = "系统用户列表") 
    @GetMapping("list")  
    public Object list(String query) { 
        List<SysUserDtox> list = sysUserMgr.listSysUserBySql(getMapperSqlClause(query)); 
        return buildList(list, LIST); 
    } 
     
    @Log
    @ApiOperation(value = "系统用户详情") 
    @GetMapping("info/{pk}")  
    public Object info(@PathVariable("pk") Long pk) { 
        SysUserDtox dtox = sysUserMgr.getSysUserByPKLong(pk); 
        return buildObj(dtox, LIST); 
    }
    
    @Log
    @ApiOperation(value = "系统用户自身详情") 
    @GetMapping("infoSelf")  
    public Object infoSelf() { 
        SysUserDtox dtox = sysUserMgr.getSysUserByPKLong(getUserInfo().getSysUserPk()); 
        return buildObj(dtox, LIST); 
    } 
     
    @Log 
    @ApiOperation(value = "系统用户编辑")
    @PostMapping("edit")  
    public Object edit(@RequestBody @Validated SysUserRo sysUserRo) { 
        SysUserVo sysUserVo = new SysUserVo(); 
        BeanUtils.copyProperties(sysUserRo, sysUserVo); 
        sysUserVo.setCatCd(CatSysCode.getByCd(sysUserRo.getCatCd())); 
        sysUserVo.setStatCd(CatSysCode.getByCd(sysUserRo.getStatCd()));
        
        if (StringUtils.length(sysUserVo.getPwd()) != 32) {
            sysUserVo.setPwd(MD5Util.encode(sysUserVo.getPwd()));
        }
        if (null == sysUserVo.getSysUserPk()) { 
            sysUserMgr.addSysUserSelective(sysUserVo, getUserInfo()); 
        } else { 
            sysUserMgr.updSysUserSelectiveByPK(sysUserVo, getUserInfo()); 
        } 
        return buildSuccess(); 
    } 
     
    @Log
    @ApiOperation(value = "系统用户删除") 
    @GetMapping("del")  
    public Object del(String pks) { 
        sysUserMgr.defunctSysUserByPKBatch(CollUtils.toLongList(pks), getUserInfo()); 
        return buildSuccess(); 
    } 
    
    @ApiOperation(value = "系统用户excel导出") 
    @GetMapping("expt")
    public void expt(HttpServletResponse response, String query) {
        MapperSqlClause msc = getMapperSqlClause(query); 
        msc.setDfltMaxRow(EXPORT_MAX_ROW);
        List<SysUserDtox> list = sysUserMgr.listSysUserBySql(msc); 
        exportExcel(response, list, EXPT);
    }
    
    @ApiOperation(value = "系统用户excel导入模板导出") 
    @GetMapping("imptCase")
    public void imptCase(HttpServletResponse response) {
        exportExcelDemo(response, null, IMPT);
    }
    
    @ApiOperation(value = "系统用户excel导入") 
    @PostMapping("impt")  
    public Object impt(HttpServletRequest request) { 
        List<SysUserDtox> list = importExcel(request, SysUserDtox.class, IMPT); 
        List<SysUserDtox> errList = new ArrayList<>(); 
        for (SysUserDtox SysUserDtox : list) { 
            try { 
                sysUserMgr.addSysUserSelective(SysUserDtox.getSysUserVo(), getUserInfo()); 
            } catch (Throwable t) {  
                errList.add(SysUserDtox); 
            } 
        } 
        return buildList(errList, IMPT); 
    } 
     
    
}
<?xml version="1.0" encoding="UTF-8"?>

<controller name="SysUserController">

    <grid name="LIST">
        <col type="yui.bss.model.vo.sys.SysUserVo" />
    </grid>

    <grid name="INFO">
        <col type="yui.bss.model.vo.sys.SysUserVo" />
    </grid>

    <grid name="EXPT">
        <col type="yui.bss.model.vo.sys.SysUserVo" />
    </grid>

    <grid name="IMPT">
        <col type="yui.bss.model.vo.sys.SysUserVo" />
    </grid>


    <search name="QUERY">
        <col type="yui.bss.model.vo.sys.SysUserVo" />
    </search>


    <grid name="GRID">
        <col name="sysUserPk" mapper="sysUserVo.sysUserPk" descr="主键" />
        <col name="subPk" mapper="sysUserVo.subPk" descr="实体主键" />
        <col name="usNm" mapper="sysUserVo.usNm" descr="登陆名" />
        <col name="nkNm" mapper="sysUserVo.nkNm" descr="昵称" />
        <col name="salt" mapper="sysUserVo.salt" descr="盐值" />
        <col name="pwd" mapper="sysUserVo.pwd" descr="密码" />
        <col name="card" mapper="sysUserVo.card" descr="证件号" />
        <col name="phone" mapper="sysUserVo.phone" descr="电话" />
        <col name="mob" mapper="sysUserVo.mob" descr="手机" />
        <col name="email" mapper="sysUserVo.email" descr="邮件" />
        <col name="sex" mapper="sysUserVo.sex" descr="性别" />
        <col name="imgUrl" mapper="sysUserVo.imgUrl" descr="头像" />
        <col name="qqOpenId" mapper="sysUserVo.qqOpenId" descr="QQopenid" />
        <col name="openId" mapper="sysUserVo.openId" descr="微信openid" />
        <col name="unionId" mapper="sysUserVo.unionId" descr="微信unionid" />
        <col name="rmks" mapper="sysUserVo.rmks" descr="备注" />
        <col name="arg1" mapper="sysUserVo.arg1" descr="参数1" />
        <col name="arg2" mapper="sysUserVo.arg2" descr="参数2" />
        <col name="arg3" mapper="sysUserVo.arg3" descr="参数3" />
        <col name="arg4" mapper="sysUserVo.arg4" descr="参数4" />
        <col name="arg5" mapper="sysUserVo.arg5" descr="参数5" />
        <col name="arg6" mapper="sysUserVo.arg6" descr="参数6" />
        <col name="arg7" mapper="sysUserVo.arg7" descr="参数7" />
        <col name="arg8" mapper="sysUserVo.arg8" descr="参数8" />
        <col name="arg9" mapper="sysUserVo.arg9" descr="参数9" />
        <col name="vldFrTm" mapper="sysUserVo.vldFrTm" descr="有效开始时间" />
        <col name="vldToTm" mapper="sysUserVo.vldToTm" descr="有效结束时间" />
        <col name="catCd" mapper="sysUserVo.catCd" descr="分类编码" />
        <col name="catNm" mapper="sysUserVo.catNm" descr="分类" />
        <col name="statCd" mapper="sysUserVo.statCd" descr="状态编码" />
        <col name="statNm" mapper="sysUserVo.statNm" descr="状态" />
        <col name="crtTm" mapper="sysUserVo.crtTm" descr="创建时间" />
        <col name="crtBy" mapper="sysUserVo.crtBy" descr="创建人" />
        <col name="updTm" mapper="sysUserVo.updTm" descr="修改时间" />
        <col name="updBy" mapper="sysUserVo.updBy" descr="修改人" />
        <col name="editFlag" mapper="sysUserVo.editFlag" descr="修订状态" />
    </grid>

    <search name="SEARCH">
        <col name="sysUserPk" mapper="T_SYS_USER.SYS_USER_PK" />
        <col name="subPk" mapper="T_SYS_USER.SUB_PK" />
        <col name="usNm" mapper="T_SYS_USER.US_NM" />
        <col name="nkNm" mapper="T_SYS_USER.NK_NM" />
        <col name="salt" mapper="T_SYS_USER.SALT" />
        <col name="pwd" mapper="T_SYS_USER.PWD" />
        <col name="card" mapper="T_SYS_USER.CARD" />
        <col name="phone" mapper="T_SYS_USER.PHONE" />
        <col name="mob" mapper="T_SYS_USER.MOB" />
        <col name="email" mapper="T_SYS_USER.EMAIL" />
        <col name="sex" mapper="T_SYS_USER.SEX" />
        <col name="imgUrl" mapper="T_SYS_USER.IMG_URL" />
        <col name="qqOpenId" mapper="T_SYS_USER.QQ_OPEN_ID" />
        <col name="openId" mapper="T_SYS_USER.OPEN_ID" />
        <col name="unionId" mapper="T_SYS_USER.UNION_ID" />
        <col name="rmks" mapper="T_SYS_USER.RMKS" />
        <col name="arg1" mapper="T_SYS_USER.ARG1" />
        <col name="arg2" mapper="T_SYS_USER.ARG2" />
        <col name="arg3" mapper="T_SYS_USER.ARG3" />
        <col name="arg4" mapper="T_SYS_USER.ARG4" />
        <col name="arg5" mapper="T_SYS_USER.ARG5" />
        <col name="arg6" mapper="T_SYS_USER.ARG6" />
        <col name="arg7" mapper="T_SYS_USER.ARG7" />
        <col name="arg8" mapper="T_SYS_USER.ARG8" />
        <col name="arg9" mapper="T_SYS_USER.ARG9" />
        <col name="vldFrTm" mapper="T_SYS_USER.VLD_FR_TM" />
        <col name="vldToTm" mapper="T_SYS_USER.VLD_TO_TM" />
        <col name="catCd" mapper="T_SYS_USER.CAT_CD" />
        <col name="catNm" mapper="T_SYS_USER.CAT_NM" />
        <col name="statCd" mapper="T_SYS_USER.STAT_CD" />
        <col name="statNm" mapper="T_SYS_USER.STAT_NM" />
        <col name="crtTm" mapper="T_SYS_USER.CRT_TM" />
        <col name="crtBy" mapper="T_SYS_USER.CRT_BY" />
        <col name="updTm" mapper="T_SYS_USER.UPD_TM" />
        <col name="updBy" mapper="T_SYS_USER.UPD_BY" />
        <col name="editFlag" mapper="T_SYS_USER.EDIT_FLAG" />
    </search>

</controller>
import yui.bss.model.vo.sys.SysUserVo;
import yui.comn.utils.orm.BaseDto;

public class SysUserDto extends BaseDto {

    private static final long serialVersionUID = 1L;

    protected SysUserVo sysUserVo = null;
    

    public void setSysUserVo(SysUserVo sysUserVo) {
        this.sysUserVo  =  sysUserVo;
    }

    public SysUserVo getSysUserVo() {
        return sysUserVo;
    }

}
package yui.bss.model.vo.sys;

import java.sql.Timestamp;
import java.io.Serializable;

import yui.comn.utils.orm.CatSysCode;
import yui.comn.utils.annotation.Note;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SysUserVo implements Cloneable, Serializable {

    private static final long serialVersionUID = 1L;
    private static final Logger logger = LoggerFactory.getLogger(SysUserVo.class);

    @Note("主键")
    private Long sysUserPk = null;

    @Note("实体主键")
    private Long subPk = null;

    @Note("登陆名")
    private String usNm = null;

    @Note("昵称")
    private String nkNm = null;

    @Note("盐值")
    private String salt = null;

    @Note("密码")
    private String pwd = null;

    @Note("证件号")
    private String card = null;

    @Note("电话")
    private String phone = null;

    @Note("手机")
    private String mob = null;

    @Note("邮件")
    private String email = null;

    @Note("性别")
    private String sex = null;

    @Note("头像")
    private String imgUrl = null;

    @Note("QQopenid")
    private String qqOpenId = null;

    @Note("微信openid")
    private String openId = null;

    @Note("微信unionid")
    private String unionId = null;

    @Note("备注")
    private String rmks = null;

    @Note("参数1")
    private String arg1 = null;

    @Note("参数2")
    private String arg2 = null;

    @Note("参数3")
    private String arg3 = null;

    @Note("参数4")
    private String arg4 = null;

    @Note("参数5")
    private String arg5 = null;

    @Note("参数6")
    private String arg6 = null;

    @Note("参数7")
    private String arg7 = null;

    @Note("参数8")
    private String arg8 = null;

    @Note("参数9")
    private String arg9 = null;

    @Note("有效开始时间")
    private Timestamp vldFrTm = null;

    @Note("有效结束时间")
    private Timestamp vldToTm = null;

    @Note("分类编码")
    private String catCd = null;

    @Note("分类")
    private String catNm = null;

    @Note("状态编码")
    private String statCd = null;

    @Note("状态")
    private String statNm = null;

    @Note("创建时间")
    private Timestamp crtTm = null;

    @Note("创建人")
    private String crtBy = null;

    @Note("修改时间")
    private Timestamp updTm = null;

    @Note("修改人")
    private String updBy = null;

    @Note("修订状态")
    private Integer editFlag = null;
  

}

 

    xml支持两种方式的配置方式:

    第一种: 通过类配置方式,对数据对象进行数据转换,这种方式主要简化配置,特别在开发阶段,数据库表字段的变动时,这种配置不需要修改xml。由于需要输出每个字段含义,所以需要在数据对象中添加@Note注释,当处理数据对象类时,可以获取字段含义。

<grid name="LIST">
    <col type="yui.bss.model.vo.sys.SysUserVo" />
</grid>

第二种:通过属性配置方式,对数据对象进行数据转换,这种方式配置方式应用面广且配置灵活,例如查询出10个字段,前端只需要展示5个,那么只要在xml配置5个对应col。

    <grid name="GRID">
        <col name="sysUserPk" mapper="sysUserVo.sysUserPk" descr="主键" />
        <col name="subPk" mapper="sysUserVo.subPk" descr="实体主键" />
        <col name="usNm" mapper="sysUserVo.usNm" descr="登陆名" />
        <col name="nkNm" mapper="sysUserVo.nkNm" descr="昵称" />
        <col name="salt" mapper="sysUserVo.salt" descr="盐值" />
        <col name="pwd" mapper="sysUserVo.pwd" descr="密码" />
        <col name="card" mapper="sysUserVo.card" descr="证件号" />
        <col name="phone" mapper="sysUserVo.phone" descr="电话" />
        <col name="mob" mapper="sysUserVo.mob" descr="手机" />
        <col name="email" mapper="sysUserVo.email" descr="邮件" />
        <col name="sex" mapper="sysUserVo.sex" descr="性别" />
        <col name="imgUrl" mapper="sysUserVo.imgUrl" descr="头像" />
        <col name="qqOpenId" mapper="sysUserVo.qqOpenId" descr="QQopenid" />
        <col name="openId" mapper="sysUserVo.openId" descr="微信openid" />
        <col name="unionId" mapper="sysUserVo.unionId" descr="微信unionid" />
        <col name="rmks" mapper="sysUserVo.rmks" descr="备注" />
        <col name="arg1" mapper="sysUserVo.arg1" descr="参数1" />
        <col name="arg2" mapper="sysUserVo.arg2" descr="参数2" />
        <col name="arg3" mapper="sysUserVo.arg3" descr="参数3" />
        <col name="arg4" mapper="sysUserVo.arg4" descr="参数4" />
        <col name="arg5" mapper="sysUserVo.arg5" descr="参数5" />
        <col name="arg6" mapper="sysUserVo.arg6" descr="参数6" />
        <col name="arg7" mapper="sysUserVo.arg7" descr="参数7" />
        <col name="arg8" mapper="sysUserVo.arg8" descr="参数8" />
        <col name="arg9" mapper="sysUserVo.arg9" descr="参数9" />
        <col name="vldFrTm" mapper="sysUserVo.vldFrTm" descr="有效开始时间" />
        <col name="vldToTm" mapper="sysUserVo.vldToTm" descr="有效结束时间" />
        <col name="catCd" mapper="sysUserVo.catCd" descr="分类编码" />
        <col name="catNm" mapper="sysUserVo.catNm" descr="分类" />
        <col name="statCd" mapper="sysUserVo.statCd" descr="状态编码" />
        <col name="statNm" mapper="sysUserVo.statNm" descr="状态" />
        <col name="crtTm" mapper="sysUserVo.crtTm" descr="创建时间" />
        <col name="crtBy" mapper="sysUserVo.crtBy" descr="创建人" />
        <col name="updTm" mapper="sysUserVo.updTm" descr="修改时间" />
        <col name="updBy" mapper="sysUserVo.updBy" descr="修改人" />
        <col name="editFlag" mapper="sysUserVo.editFlag" descr="修订状态" />
    </grid>

    xml有两种类型元素类型:

    第一种:search,顾名思义,这个配置用于查询字段转换为数据库表名+字段,默认通过类配置方式。如果采用属性配置方式,当有些字段不能被用于查询,可以删除相应的col,增加数据的安全性;当需要同时查询两张表时,可以添加相应的col,例如加入<col name="roleNm" mapper="T_SYS_ROLE.NM" />,这样可以前端可以对两张表数据进行查询,扩散一下思维,也可以对三表,乃至N表进行数据查询。

    第二种:grid,这个元素主要用于数据对象数据转换,sysUserController.xml有LIST,INFO,IMPT,EXPT四种名称,下面来讲解四中名称对应的四种情况,

    1)LIST: 后端查询出列表时,对数据对象List<SysUserVo>进行转换为List<Map<String, Object>>

    2)INFO:后端查询出对象时,对数据对象SysUserVo进行转换为Map<String, Object>

    3)EXPT:后端查询出列表时,进行EXCEL数据文件导入

    4)IMPT:这里对这个IMPT会有三种情况的使用。 ①当前端上传EXCEL数据文件时,通过XML把流数据转换为数据对象,然后后端直接可以对数据对象进行处理。②当EXCEL数据文件部分数据出错时,通过该XML把错误数据返回给前端。③EXCEL上传模板下载,为了保持EXCEL标题的一致性,通过XML导出一个只有标题EXCEL模板文件。

通过有效的数据对象转换,还能给每个查询结果的数据属性进行解释。

{
  "status": "200",
  "header": {
    "sysUserPk": "主键",
    "subPk": "实体主键",
    "usNm": "登陆名",
    "nkNm": "昵称"
  },
  "data": {
    "items": [
      {
        "sysUserPk": 4003665987933184,
        "subPk": null,
        "usNm": "test",
        "nkNm": "测试账号"
      }
    ]
  }
}

三:高效数据对象转换方式总结

    通过这种高效数据对象转换方式,查询的时候,增加数据的安全性,且支持多表查询,返回结果的时候。可以灵活的配置返回结果,灵活的返回数据对象,且返回给前端数据属性的解释。其他方式的应用有待开发。

    一种高效的数据对象转换方式(2),下一章将讲解数据对象转换的具体处理方式。

   

 

 

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部