当前位置: 首页 > news >正文

查询出跨4个表以上的查询

不过不考虑子查询等, 可以粗略认为是语句中含有4个以上的join
不想一个一个找.
于是想:
方法一: spring中直接取出所有的mapper bean的 解析后的sql 语句, 然后按条件过滤, 再导出excel. 可惜的是, 这儿的sql语句 已经被mybatis解析成了嵌套的各类节点, 没法直接看原语句了. 所以不行.
public static void main(String[] args) {

    SpringApplication application = new SpringApplication(xxxApplication.class);
    application.addInitializers(new SpringUtils());
    application.setRegisterShutdownHook(false);
    application.run(args);
    outputSql();
}

private static void outputSql() {
    final Collection<MappedStatement> mappedStatements = SpringUtils.getApplicationContext().getBean(SqlSessionFactory.class).getConfiguration().getMappedStatements();
    int ignoreCount = 0;
    int errorCount = 0;
    int predictOutPutCount = 0;
    final String replacer = "JOIN";
    final int maxFrequency = 0; //join出现的次数下限(不含)
    final int maxLengthGap = replacer.length() * maxFrequency;
    for (final Object obj : mappedStatements) {
        if (obj instanceof MappedStatement) {
            MappedStatement statement = (MappedStatement) obj;
            if (!statement.getId().contains("Mapper") && !statement.getId().contains("mapper")) {
                ignoreCount++;
                continue;
            }
            final String rawSql = getStaticSql(statement);
            if (StringUtils.isBlank(rawSql)) {
                errorCount++;
                continue;
            }
            final String upperCaseSql = rawSql.toUpperCase();
            System.out.println("upperCaseSql = " + upperCaseSql);
            if (upperCaseSql.length() - (upperCaseSql.replaceAll(replacer, "")).length() > maxLengthGap) { //Pattern.compile.matcher计算效率应该更高
                System.out.println("rawSql = " + rawSql);
            }
        }
        ignoreCount++;
    }
    System.out.println("处理sql总数 ===  " + mappedStatements.size());
    System.out.println("预计导出sql总数 ===  " + predictOutPutCount);
    System.out.println("出错sql总数 ===  " + errorCount);
    System.out.println("极简sql总数 ===  " + ignoreCount);
}

private static String getStaticSql(final MappedStatement statement) {
    SqlSource sqlSource = statement.getSqlSource();
    if (sqlSource instanceof StaticSqlSource) {
        final Field field = ReflectionUtils.findField(StaticSqlSource.class, "sql");
        field.setAccessible(true);
        return ReflectionUtils.getField(field, sqlSource).toString();
    }

// if (sqlSource instanceof RawSqlSource) {
// sqlSource = JSON.toJavaObject((JSONObject) (((JSONObject) JSONObject.toJSON(sqlSource)).get(“sqlSource”)), StaticSqlSource.class);
// return ((JSONObject) JSONObject.toJSON(sqlSource)).getString(“sql”);
// }

    if (sqlSource instanceof RawSqlSource) {
        final Field field = ReflectionUtils.findField(RawSqlSource.class, "sqlSource");
        field.setAccessible(true);
        final Object sqlSource1 = ReflectionUtils.getField(field, sqlSource);
        final Field field2 = ReflectionUtils.findField(StaticSqlSource.class, "sql");
        field2.setAccessible(true);
        return ReflectionUtils.getField(field2, sqlSource1).toString();

//
// sqlSource = JSON.toJavaObject((JSONObject) (((JSONObject) JSONObject.toJSON(sqlSource)).get(“sqlSource”)), StaticSqlSource.class);
// return ((JSONObject) JSONObject.toJSON(sqlSource)).getString(“sql”);
}
if (sqlSource instanceof DynamicSqlSource) {
final Field field2 = ReflectionUtils.findField(DynamicSqlSource.class, “rootSqlNode”);
field2.setAccessible(true);
final Object sqlNode = ReflectionUtils.getField(field2, sqlSource);
return JSON.toJSONString(sqlNode);
}
return null;
}
方法二: 读取文件, 按需导出
方法三: 按正则查询, 一个个复制到excel
方法四: idea规范插件可以直接找出来吗

相关文章:

  • 罗马数字转整数(13)题解
  • STC单片机RAM在KEIL编程使用
  • Git的版本控制(查看文件内容和文件的变化状态、版本日志、版本回退以及找回覆盖的版本等)
  • ijkplayer源码分析 —— 事件消息设计
  • 水文科技新仪器——地埋式积水探测器
  • Jackson ImmunoResearch通过 SDS-PAGE 进行蛋白质分离
  • FLUME 安装配置及使用示例
  • 高等教育心理学:学生的个性与社会性的发展
  • Web前端:Angular的优缺点以及何时使用Angular?
  • Redis基础与高可用集群架构进阶详解
  • 如何翻译图片上的英文?建议收藏这三个方法
  • 微信网页开发——JS-SDK接入以及微信二次分享图标和标题丢失
  • 外汇监管机构哪个好?怎么选择?
  • 基于Springboot+vue的电影票预定管理系统 elementui
  • 标签类目体系(面向业务的数据资产设计方法论)-读书笔记5
  • [rust! #004] [译] Rust 的内置 Traits, 使用场景, 方式, 和原因
  • 【JavaScript】通过闭包创建具有私有属性的实例对象
  • 2019.2.20 c++ 知识梳理
  • Bootstrap JS插件Alert源码分析
  • CentOS 7 修改主机名
  • Elasticsearch 参考指南(升级前重新索引)
  • GraphQL学习过程应该是这样的
  • HTML5新特性总结
  • interface和setter,getter
  • Javascript设计模式学习之Observer(观察者)模式
  • Linux学习笔记6-使用fdisk进行磁盘管理
  • miaov-React 最佳入门
  • Selenium实战教程系列(二)---元素定位
  • Webpack 4x 之路 ( 四 )
  • 不上全站https的网站你们就等着被恶心死吧
  • 从 Android Sample ApiDemos 中学习 android.animation API 的用法
  • 回顾2016
  • 经典排序算法及其 Java 实现
  • 聊聊flink的TableFactory
  • 判断客户端类型,Android,iOS,PC
  • 实战|智能家居行业移动应用性能分析
  • 使用putty远程连接linux
  • 完善智慧办公建设,小熊U租获京东数千万元A+轮融资 ...
  • 选择阿里云数据库HBase版十大理由
  • ​学习一下,什么是预包装食品?​
  • #Spring-boot高级
  • #免费 苹果M系芯片Macbook电脑MacOS使用Bash脚本写入(读写)NTFS硬盘教程
  • (1)常见O(n^2)排序算法解析
  • (附源码)spring boot球鞋文化交流论坛 毕业设计 141436
  • (附源码)spring boot校园健康监测管理系统 毕业设计 151047
  • (力扣)循环队列的实现与详解(C语言)
  • (一)pytest自动化测试框架之生成测试报告(mac系统)
  • (转) 深度模型优化性能 调参
  • .NET 同步与异步 之 原子操作和自旋锁(Interlocked、SpinLock)(九)
  • .NET 应用架构指导 V2 学习笔记(一) 软件架构的关键原则
  • .NET成年了,然后呢?
  • @ConditionalOnProperty注解使用说明
  • @JsonFormat与@DateTimeFormat注解的使用
  • @WebService和@WebMethod注解的用法
  • [<死锁专题>]