一. 介绍
第一节简单介绍怎么使用数据对象转换:一种高效的数据对象转换方式(1)
https://blog.csdn.net/ssyujay/article/details/81638984
下面将介绍xml中属性的详细使用方法
二. xml中col元素中包含9中属性
prefix, //name的前缀, 比如prefix=“sysUser”, name=“nm”, 那么name输出就是 sysUserNm。
name, //grid: map中的key; search: 查询的key。
mapper, //grid: 对象.属性; search: 表名+字段 。
type, //通过解析对象属性,反射生成列。
descr, //列描述 。
viewOrder, //列排序,如果写,默认按顺序排序,如果解析类对象,按照英文首字母排序。
handler, //处理器,多个处理器用分号(";")隔开。
toName, //如果通过处理器想生成另一个map属性。
toDescr, //如果通过处理器想生成另一个map属性描述。
三. 下面将详细介绍,在grid中这些属性特性和使用
①. prefix:前缀,顾名思义就是在name参数值前加上前缀。
配置如下
<grid name="LIST">
<col prefix="sysUser" name="usNm" mapper="sysUserVo.usNm" descr="登陆名" />
<col prefix="sysUser" name="nkNm" mapper="sysUserVo.nkNm" descr="昵称" />
</grid>
输出如下
{
"status": "200",
"header": {
"sysUserUsNm": "登陆名",
"sysUserNkNm": "昵称"
},
"data": {
"items": [
{
"sysUserUsNm": "admin",
"sysUserNkNm": "yui"
}
]
}
}
②. name:json输出中的key,包括data数据中的key和header数据中的key。
③. mapper:映射到对象实体中的属性。比如sysUserVo.usNm,可以获取对象实体属性的值,然后输出。
④. type:类全名称,通过反射类字段,解析有注释@Note的字段,并且生成col。
@Note包含col属性类型
@Target({ElementType.FIELD, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Note {
/**
* 默认注释
* @return
*/
@AliasFor("descr")
String value() default "";
/**
* 默认注释
* @return
*/
@AliasFor("value")
String descr() default "";
/**
* 属性排序
* @return
*/
int viewOrder() default 0;
/**
* 处理方式
* @return
*/
String handler() default "";
/**
* 生成一个新的元素(不覆盖原来), 该属性只有在handler存在时才有效
* @return
*/
String toName() default "";
/**
* 生成一个新的元素的描述(不覆盖原来), 该属性只有在handler存在时才有效
* @return
*/
String toDescr() default "";
}
配置如下:
<grid name="LIST">
<col type="yui.bss.model.vo.sys.SysUserVo" />
</grid>
输出如下:
{
"status": "200",
"header": {
"sysUserPk": "主键",
"subPk": "实体主键",
"usNm": "登陆名",
"nkNm": "昵称",
"salt": "盐值",
"pwd": "密码",
"card": "证件号",
"phone": "电话",
"mob": "手机",
"email": "邮件",
"sex": "性别",
"imgUrl": "头像",
"qqOpenId": "QQopenid",
"openId": "微信openid",
"unionId": "微信unionid",
"rmks": "备注",
"arg1": "参数1",
"arg2": "参数2",
"arg3": "参数3",
"arg4": "参数4",
"arg5": "参数5",
"arg6": "参数6",
"arg7": "参数7",
"arg8": "参数8",
"arg9": "参数9",
"vldFrTm": "有效开始时间",
"vldToTm": "有效结束时间",
"catCd": "分类编码",
"catNm": "分类",
"statCd": "状态编码",
"statNm": "状态",
"crtTm": "创建时间",
"crtBy": "创建人",
"updTm": "修改时间",
"updBy": "修改人",
"editFlag": "修订状态"
},
"data": {
"items": [
{
"sysUserPk": 1,
"subPk": 3664372564677632,
"usNm": "admin",
"nkNm": "yui",
"salt": null,
"pwd": "c7635bfd99248a2cdef8249ef7bfbef4",
"card": null,
"phone": "123456789",
"mob": "12345678912",
"email": "[email protected]",
"sex": null,
"imgUrl": null,
"qqOpenId": null,
"openId": null,
"unionId": null,
"rmks": null,
"arg1": null,
"arg2": null,
"arg3": null,
"arg4": null,
"arg5": null,
"arg6": null,
"arg7": null,
"arg8": null,
"arg9": null,
"vldFrTm": null,
"vldToTm": null,
"catCd": "20000.160",
"catNm": "人员",
"statCd": "10000.150",
"statNm": "是",
"crtTm": "2017-11-07 15:17:11",
"crtBy": null,
"updTm": "2018-01-18 14:00:36",
"updBy": "admin",
"editFlag": 0
}
]
}
}
⑤. descr:列描述,也是json输出中header中的值。
⑥. viewOrder:排序,如果不填写该属性,按照顺序排序,如果加了这个配置就是按照该配置值排序。
配置如下:
<grid name="LIST">
<col name="usNm" mapper="sysUserVo.usNm" descr="登陆名" viewOrder="2"/>
<col name="nkNm" mapper="sysUserVo.nkNm" descr="昵称" viewOrder="1"/>
</grid>
输出如下:
{
"status": "200",
"header": {
"nkNm": "昵称",
"usNm": "登陆名"
},
"data": {
"items": [
{
"nkNm": "yui",
"usNm": "admin"
}
]
}
}
⑦. handler:处理器,多个处理器用分号(";")隔开,处理器支持spring注入,handler="test" 和 handler="test()" 配置方式都行。不管方法带不带参数,默认带有该col中name参数值。
比如<col name="usNm" mapper="sysUserVo.usNm" descr="登陆名" handler="test"/>, 是带有usNm一个参数。
比如<col name="usNm" mapper="sysUserVo.usNm" descr="登陆名" handler="test(nkNm)"/>, 是带有usNm, nkNm两个参数。
如果配置了处理器,每个值输出前都会执行处理器。使用场景,比如配置身份证,电话号码部分打马输出,比如进行数据字典翻译后输出。
配置如下:
<grid name="LIST">
<col name="usNm" mapper="sysUserVo.usNm" descr="登陆名" handler="test"/>
<col name="nkNm" mapper="sysUserVo.nkNm" descr="昵称" handler="test()"/>
</grid>
public class TestHubDataHandler extends AbstractHubDataHandler {
@Override
protected Object doHandle(List<String> params, Map<String, Object> dataMap) {
System.out.println("TestHubDataHandler");
return dataMap.get(params.get(0)).toString() + "----test";
}
}
输出如下:
{
"status": "200",
"header": {
"usNm": "登陆名",
"nkNm": "昵称"
},
"data": {
"items": [
{
"usNm": "admin----test",
"nkNm": "yui----test"
}
]
}
}
⑧. toName: 配置了这个属性的前提是需要配置handler,当进行handler处理后,生成一个新的属性输出,使用场景,数据库存储编码,通过该配置也可以同时输出编码含义。
⑨. toDescr: 配置了这个属性的前提是需要配置handler,当进行handler处理后,生成一个新的key属性描述
<grid name="LIST">
<col name="usNm" mapper="sysUserVo.usNm" descr="登陆名" handler="test"/>
<col name="nkNm" mapper="sysUserVo.nkNm" descr="昵称"/>
<col name="catCd" mapper="sysUserVo.catCd" descr="分类编码" handler="dist(catCd)" toName="catNm2" toDescr="分类名称2"/>
</grid>
@Component
public class TestDistDataHandler extends AbstractHubDataHandler {
@Reference
private ISysCatMgrx sysCatMgrx;
@Override
protected Object doHandle(List<String> params, Map<String, Object> dataMap) {
String str = (String) dataMap.get(params.get(1));
if (StringUtils.isNotBlank(str)) {
String nm = sysCatMgrx.getNmByCd(str);
return nm;
}
return null;
}
@Override
protected String getRegisterName() {
return "dist";
}
}
输出如下:
{
"status": "200",
"header": {
"usNm": "登陆名",
"nkNm": "昵称",
"catCd": "分类编码",
"catNm2": "分类名称2"
},
"data": {
"items": [
{
"usNm": "admin----test",
"nkNm": "yui",
"catCd": "20000.160",
"catNm2": "人员"
}
]
}
}
四. 如果两个表联查输出
配置如下:
<grid name="LIST">
<col name="usNm" mapper="sysUserVo.usNm" descr="登陆名" />
<col name="nkNm" mapper="sysUserVo.nkNm" descr="昵称" />
<col name="nm" mapper="sysRoleVo.nm" descr="名称" />
<col name="rmks" mapper="sysRoleVo.rmks" descr="备注" />
</grid>
输出如下:
{
"status": "200",
"header": {
"usNm": "登陆名",
"nkNm": "昵称",
"nm": "名称",
"rmks": "备注"
},
"data": {
"usNm": "admin",
"nkNm": "yui",
"nm": "role1",
"rmks": "222"
}
}
如果使用类的配置方式:
配置如下:
<grid name="LIST">
<col prefix="sysUser" type="yui.bss.model.vo.sys.SysUserVo" />
<col prefix="sysRole" type="yui.bss.model.vo.sys.SysRoleVo" />
</grid>
输出如下:
{
"status": "200",
"header": {
"sysUserSysUserPk": "主键",
"sysUserSubPk": "实体主键",
"sysUserUsNm": "登陆名",
"sysUserNkNm": "昵称",
"sysUserSalt": "盐值",
"sysUserPwd": "密码",
"sysUserCard": "证件号",
"sysUserPhone": "电话",
"sysUserMob": "手机",
"sysUserEmail": "邮件",
"sysUserSex": "性别",
"sysUserImgUrl": "头像",
"sysUserQqOpenId": "QQopenid",
"sysUserOpenId": "微信openid",
"sysUserUnionId": "微信unionid",
"sysUserRmks": "备注",
"sysUserArg1": "参数1",
"sysUserArg2": "参数2",
"sysUserArg3": "参数3",
"sysUserArg4": "参数4",
"sysUserArg5": "参数5",
"sysUserArg6": "参数6",
"sysUserArg7": "参数7",
"sysUserArg8": "参数8",
"sysUserArg9": "参数9",
"sysUserVldFrTm": "有效开始时间",
"sysUserVldToTm": "有效结束时间",
"sysUserCatCd": "分类编码",
"sysUserCatNm": "分类",
"sysUserStatCd": "状态编码",
"sysUserStatNm": "状态",
"sysUserCrtTm": "创建时间",
"sysUserCrtBy": "创建人",
"sysUserUpdTm": "修改时间",
"sysUserUpdBy": "修改人",
"sysUserEditFlag": "修订状态",
"sysRoleSysRolePk": "主键",
"sysRolePrntPk": "父主键",
"sysRolePrntNm": "父名称",
"sysRoleNm": "名称",
"sysRoleRmks": "备注",
"sysRoleCatCd": "分类编码",
"sysRoleCatNm": "分类",
"sysRoleStatCd": "状态编码",
"sysRoleStatNm": "状态",
"sysRoleCrtTm": "创建时间",
"sysRoleCrtBy": "创建人",
"sysRoleUpdTm": "修改时间",
"sysRoleUpdBy": "修改人",
"sysRoleEditFlag": "修订状态"
},
"data": {
"sysUserSysUserPk": 1,
"sysUserSubPk": 3664372564677632,
"sysUserUsNm": "admin",
"sysUserNkNm": "yui",
"sysUserSalt": null,
"sysUserPwd": "c7635bfd99248a2cdef8249ef7bfbef4",
"sysUserCard": null,
"sysUserPhone": "123456789",
"sysUserMob": "12345678912",
"sysUserEmail": "[email protected]",
"sysUserSex": null,
"sysUserImgUrl": null,
"sysUserQqOpenId": null,
"sysUserOpenId": null,
"sysUserUnionId": null,
"sysUserRmks": null,
"sysUserArg1": null,
"sysUserArg2": null,
"sysUserArg3": null,
"sysUserArg4": null,
"sysUserArg5": null,
"sysUserArg6": null,
"sysUserArg7": null,
"sysUserArg8": null,
"sysUserArg9": null,
"sysUserVldFrTm": null,
"sysUserVldToTm": null,
"sysUserCatCd": "20000.160",
"sysUserCatNm": "人员",
"sysUserStatCd": "10000.150",
"sysUserStatNm": "是",
"sysUserCrtTm": "2017-11-07 15:17:11",
"sysUserCrtBy": null,
"sysUserUpdTm": "2018-01-18 14:00:36",
"sysUserUpdBy": "admin",
"sysUserEditFlag": 0,
"sysRoleSysRolePk": null,
"sysRolePrntPk": null,
"sysRolePrntNm": null,
"sysRoleNm": "role1",
"sysRoleRmks": "222",
"sysRoleCatCd": null,
"sysRoleCatNm": null,
"sysRoleStatCd": null,
"sysRoleStatNm": null,
"sysRoleCrtTm": null,
"sysRoleCrtBy": null,
"sysRoleUpdTm": null,
"sysRoleUpdBy": null,
"sysRoleEditFlag": null
}
}
五. 下面将详细介绍,在search中这些属性特性和使用
①. prefix:前缀,顾名思义就是在name参数值前加上前缀。
②. name:前端查询字段
③. mapper:表明+字段
比如:<col name="usNm" mapper="T_SYS_USER.US_NM" />,前端查询时usNm,映射到后端时T_SYS_USER.US_NM
比如:<col prefix=“sysUser” name="usNm" mapper="T_SYS_USER.US_NM" />,前端查询时sysUserUsNm,映射到后端时T_SYS_USER.US_NM
六. 通过xml方式,将excel上传数据转化为对象
上面有介绍将对象拍平转化为map对象,这里介绍刚刚相反,将excel对象数据转化为对象列表。
@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);
}
for (int i = sheet.getFirstRowNum() + 1; i < sheet.getPhysicalNumberOfRows(); i++) {
Row row = sheet.getRow(i);
//实例化最外层对象
@SuppressWarnings("unchecked")
T obj = (T) HubDataToDtoParser.getInstance().toObjByClass(clazz);
for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
//对象赋值
HubXmlColumn column = columns.get(j);
mapper = column.getMapper();
Object cellValue = getCellValue(row.getCell(j));
if (null != cellValue) {
HubDataToDtoParser.getInstance().toObjSetVal(obj, mapper, cellValue);
}
}
dataList.add(obj);
}
六. 思想扩展-列级别权限控制
比如在grid添加ROLE属性,通过该属性决定该登录用户输出哪些列
<grid name="LIST" ROLE="ROLE_ADMIN, ROLE_TEST">
<col type="yui.bss.model.vo.sys.SysUserVo" />
</grid>