Mapper.java IPage getDyData(@Param("page") Page page, @Param("columns")String columns, @Param("tableName")String tableNa
Mapper.java
IPage
Mapper.xml
select ${columns}from ${tableName} dy
使用场景:
我们必须知道查询的表名,可以通过表名查询字段及注释,然后把表名和字段传进mapper
如果知道具体查询哪些表,可以把这些表及表对应的字段配置进一张表和字段对应的表中,通过表名查询该表取出对应的字段及注释,此处的字段注释可以自己写相对比较灵活。
DROP TABLE IF EXISTS `table_colume_mapping`;CREATE TABLE `table_colume_mapping` ( `table_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '表名', `column_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '字段名', `column_comment` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '字段汉语名', `table_comment` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '表名注释', `sort` int(10) NULL DEFAULT NULL COMMENT '字段排序', `show_column` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '是' COMMENT '是否展示在列', `del_flag` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '0' COMMENT '0存在1删除') ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '表字段注释映射' ROW_FORMAT = Dynamic;SET FOREIGN_KEY_CHECKS = 1;
调用
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();wrapper.eq(TableColumeMapping::getTableName, "查询的表名");List list = springContextHolder.getBean(TableColumeMapping.class).list(wrapper);Map tableColumeMap = list.stream().collect(Collectors.toMap(TableColumMapperPerson::getColumnName, account -> account));String columnNames = list.stream().map(item -> "dy."+item.getColumnName()).collect(Collectors.joining(","));IPage
来源地址:https://blog.csdn.net/qq_37931185/article/details/130066498
--结束END--
本文标题: 运用Mybatis动态查询字段及表名
本文链接: https://lsjlt.com/news/396623.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0