<!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> <scope>runtime</scope> </dependency>
/** * Actually execute the prepared statement. This is here so server-side * PreparedStatements can re-use most of the code from this class. * * @param maxRowsToRetrieve the max number of rows to return * @param sendPacket SQL语句packet * @param createStreamingResultSet should a ‘streaming‘ result set be created? * @param queryIsSelectOnly is this query doing a SELECT? */ protected ResultSetInternalMethods executeInternal(int maxRowsToRetrieve, Buffer sendPacket, boolean createStreamingResultSet, boolean queryIsSelectOnly, Field[] metadataFromCache, boolean isBatch) throws SQLException {}
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <sdk.version>7.0.0</sdk.version> </properties> <repositories> <repository> <id>zt-public</id> <url>https://repos.zeroturnaround.com/nexus/content/groups/zt-public</url> </repository> </repositories> <dependencies> <dependency> <groupId>org.zeroturnaround</groupId> <artifactId>jr-sdk</artifactId> <version>${sdk.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.zeroturnaround</groupId> <artifactId>jr-utils</artifactId> <version>${sdk.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> <scope>provided</scope> </dependency> </dependencies>
package com.example.plugin.mysql.cbp; public class MySQLExplainCBP extends JavassistClassBytecodeProcessor { @Override public void process(ClassPool cp, ClassLoader cl, CtClass ctClass) throws Exception { LoggerFactory.getInstance().echo("MySQLExplainCBP..."); // 自定义处理类所在包 cp.importPackage("com.example.plugin.mysql.explain"); // 找到方法:com.mysql.jdbc.PreparedStatement#executeInternal // 在该方法第一行之前插入自定义逻辑 CtMethod m = ctClass.getDeclaredMethod("executeInternal"); m.insertBefore( "MySQLExplain.explainSql(this.connection, sendPacket);" ); } }
package com.example.plugin.mysql.explain; public final class MySQLExplain { private static final Logger logger = LoggerFactory.getInstance(); /** * 调用者:MySQLExplainCBP#process */ public static void explainSql(com.mysql.jdbc.MySQLConnection conn, com.mysql.jdbc.Buffer sendPacket) { byte[] bytes = new byte[sendPacket.getPosition()]; System.arraycopy(sendPacket.getByteBuffer(), 0, bytes, 0, bytes.length); String sql = new String(bytes, 5, bytes.length - 5); // 前置检查,检查是否执行计划 if (!processBefore(sql)) { return; } try { Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("EXPLAIN " + sql); List<ExplainResultVo> explainResultList = new ArrayList<>(); while (rs.next()) { ExplainResultVo explainResultVo = new ExplainResultVo(); explainResultVo.setId(rs.getString("id")); explainResultVo.setSelectType(rs.getString("select_type")); explainResultVo.setTable(rs.getString("table")); explainResultVo.setPartitions(rs.getString("partitions")); explainResultVo.setType(rs.getString("type")); explainResultVo.setPossibleKeys(rs.getString("possible_keys")); explainResultVo.setKey(rs.getString("key")); explainResultVo.setKeyLen(rs.getString("key_len")); explainResultVo.setRef(rs.getString("ref")); explainResultVo.setRows(rs.getString("rows")); explainResultVo.setFiltered(rs.getString("filtered")); explainResultVo.setExtra(rs.getString("Extra")); explainResultList.add(explainResultVo); } rs.close(); // 打印结果 analyzeResult(sql, explainResultList); } catch (Exception se) { logger.errorEcho("EXPLAIN SQL异常", se); } } /** * 前置检查,检查是否执行计划,自定添加逻辑 */ private static boolean processBefore(String sql) { return true; } /** * 分析结果,打印执行计划,可自定添加过滤条件 */ private static void analyzeResult(String sql, List<ExplainResultVo> explainResultList) { ExplainHelper.printExplainResult(sql, explainResultList, showSQL); } }
package com.example.plugin.mysql; /** * 插件注册,需在pom中配置该类 */ public class MySQLExplainPlugin implements Plugin { private static final Logger logger = LoggerFactory.getInstance(); @Override public void preinit() { Integration integration = IntegrationFactory.getInstance(); ClassLoader cl = getClass().getClassLoader(); logger.echo("MySQL执行计划插件启用, 配置详情: \n" + Config.getMySQLConfTable()); integration.addIntegrationProcessor( cl, "com.mysql.jdbc.PreparedStatement", new MySQLExplainCBP()); } // 指定依赖条件,依赖存在时才启用本插件 @Override public boolean checkDependencies(ClassLoader cl, ClassResourceSource crs) { return crs.getClassResource("com.mysql.jdbc.PreparedStatement") != null; } @Override public String getId() { return "mysql-explain-jr-plugin"; } // 省略其余方法,默认返回null即可 }
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> <configuration> <archive> <manifestEntries> <JavaRebel-Plugin>com.example.plugin.mysql.MySQLExplainPlugin</JavaRebel-Plugin> </manifestEntries> </archive> </configuration> </plugin> </plugins> </build>
mvn clean package -Dmaven.test.skip=true
-Drebel.plugins=/your/path/mysql-explain-jr-plugin.jar
原文:https://www.cnblogs.com/flylinran/p/11440035.html