关于java:mybatis解决动态表动态字段foreach处理

8次阅读

共计 1917 个字符,预计需要花费 5 分钟才能阅读完成。

需要形容:不同零碎领有不同的用户表,如果新增一个零碎的话,则须要新增用户表,此时如果在页面展现的话,则会呈现问题,显得写的很死,不能灵便调用。
现想针对新增零碎用户表的时候能做动静解决,不论新增任何表,都能够动静进行查问,新增,批改,删除操作,不须要后端新增接口,进行增删改查操作。
表构造:

代码:查问,展现详情
controller:

@ApiOperation("查问啊")
@RequestMapping(value = "/personManagementDetail", method = RequestMethod.POST)
public MapOutput personManagementDetail(String userTbname, String accountNumber, Integer pageNum, Integer pageSize) {
    // 查问零碎表中是否存在新增的用户表
 List<EditAndUserOutputDto> editAndUserEntity = pactera_SystemService.getUserTbNameAndEdit(userTbname);
 Map maps = new HashMap();
 List<String> stringList = new ArrayList<>();
 // 查问须要展现的用户表的表名,字段名,字段形容,用来动静展现 title 和 body
 List<TitleOutput> titleOutputList = pactera_SystemService.getFieldInformation(userTbname);
 List list = new ArrayList();
 for (int i = 0; i < titleOutputList.size(); i++) {Map map = new HashMap<>();
 map.put("key", titleOutputList.get(i).getFieldName());
 map.put("name", titleOutputList.get(i).getFieldDescription());
 list.add(map);
 String fieldName = titleOutputList.get(i).getFieldName();
 stringList.add(fieldName);
 }
    if (editAndUserEntity.size() == 0) {maps.put("header", list);
 maps.put("body", 0);
 } else {
        // 当零碎表中领有此用户表时,进行查问,用来展现数据
 PageInfo<Map<String, Object>> lists = userService.getBodyResult(userTbname, accountNumber, pageNum, pageSize, stringList);
 List<Map<String, Object>> body = lists.getList();
 maps.put("header", list);
 maps.put("body", body);
 }
    // 0 是可编辑状态,1 是不可编辑状态
 if ("User".equals(userTbname)) {maps.put("edit", 1);
 } else {maps.put("edit", 0);
 }
    return new MapOutput(200, "胜利", null, maps);
}

对应 mapper.xml

SELECT
    表名 =
CASE
        WHEN a.colorder= 1 THEN
        d.name ELSE '' 
    END, 字段序号 = a.colorder, 字段名 = a.name,
    字段阐明 = ISNULL(g.[value], '' ) 
FROM
    syscolumns a
    LEFT JOIN systypes b ON a.xusertype= b.xusertype
    INNER JOIN sysobjects d ON a.id= d.id 
    AND d.xtype= 'U' 
    AND d.name<> 'dtproperties'
    LEFT JOIN syscomments e ON a.cdefault= e.id
    LEFT JOIN sys.extended_properties g ON a.id= g.major_id 
    AND a.colid= g.minor_id
    LEFT JOIN sys.extended_properties f ON d.id= f.major_id 
    AND f.minor_id= 0 
WHERE
    d.name LIKE '%User' -- 如果查问所有表, 加上此条件
    --d.name = 某用户表  如果只查问指定表, 加上此条件
ORDER BY
    a.id,
    a.colorder;

查问后果:

正文完
 0