牛叉了-arthas 热更新 mybatis mapper xml

2024-08-21 00:08

本文主要是介绍牛叉了-arthas 热更新 mybatis mapper xml,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

测试环境能够热更新class 能否热更新mapper xml? arthas 群有同学提了这样的一个需求,必须满足满足
arthas-idea-plugin 2.8 版本 https://plugins.jetbrains.com/plugin/13581-arthas-idea

1、基本思路

1.1 流程图

在这里插入图片描述

1.2 实现效果

 echo `redis-cli -h '127.0.0.1' -p 6379  get arthasIdeaPluginRedefineCacheKey_e00b5a9e-d926-43c6-8b3f-5790841557ed`  | base64 --decode >arthas-idea-plugin-mybatis-mapper-xml-reload.sh;chmod a+x arthas-idea-plugin-mybatis-mapper-xml-reload.sh;./arthas-idea-plugin-mybatis-mapper-xml-reload.sh;
./arthas-idea-plugin-mybatis-mapper-xml-reload.sh: line 13: /Users/wangji/opt/arthas/mybatis/mapper/UserDoMapper.xml: Permission denied
************************************************************************************************************************
* arthas idea plugin begin;start script path: /Users/wangji/opt/arthas/arthas-idea-plugin-mybatis-mapper-xml-reload.sh *
************************************************************************************************************************decode base64 text to path /Users/wangji/opt/arthas/mybatis/mapper/UserDoMapper.xml[INFO] JAVA_HOME: /Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home
* [1]: 98083 org.jetbrains.idea.maven.server.RemoteMavenServer36[2]: 98051 org.jetbrains.jps.cmdline.Launcher[3]: 98052 com.intellij.idea.Main[4]: 98167 org.jetbrains.idea.maven.server.RemoteMavenServer36[5]: 98567 org.jetbrains.jps.cmdline.Launcher[6]: 98568 com.boot.mybatis.mybatisdemo.MybatisDemoApplication[7]: 96571 org.jetbrains.kotlin.daemon.KotlinCompileDaemon[8]: 98076 org.jetbrains.jps.cmdline.Launcher[9]: 96412请手动选择进程或者idea 预先配置jps -l 工程名称自动执行6
******************************************************************
* first: get spring static spring context class1oader hash value *
******************************************************************arthas start command :/Users/wangji/opt/arthas/as.sh --height 100 --width 200 --select 98568  -c "sc -d com.boot.mybatis.mybatisdemo.common.ApplicationContextProvider"  | tee /Users/wangji/opt/arthas/classLoaderHashValue.out
Arthas script version: 3.5.0
[INFO] JAVA_HOME: /Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home
[INFO] Process 98568 already using port 3658
[INFO] Process 98568 already using port 8563
Arthas home: /Users/wangji/opt/arthas
Calculating attach execution time...
Attaching to 98568 using version /Users/wangji/opt/arthas...real	0m0.236s
user	0m0.419s
sys	0m0.056s
Attach success.,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---./  O  \ |  .--. ''--.  .--'|  '--'  | /  O  \ '   .-'
|  .-.  ||  '--'.'   |  |   |  .--.  ||  .-.  |`.  `-.
|  | |  ||  |\  \    |  |   |  |  |  ||  | |  |.-'    |
`--' `--'`--' '--'   `--'   `--'  `--'`--' `--'`-----'wiki       https://arthas.aliyun.com/doc
tutorials  https://arthas.aliyun.com/doc/arthas-tutorials.html
version    3.5.0
main_class com.boot.mybatis.mybatisdemo.MybatisDemoApplication
pid        98568
time       2021-05-10 22:00:59[arthas@98568]$ sc -d com.boot.mybatis.mybatisdemo.common.ApplicationContextProvider | plaintextclass-info        com.boot.mybatis.mybatisdemo.common.ApplicationContextProvidercode-source       /Users/wangji/Documents/project/mybatis-demo/target/classes/name              com.boot.mybatis.mybatisdemo.common.ApplicationContextProviderisInterface       falseisAnnotation      falseisEnum            falseisAnonymousClass  falseisArray           falseisLocalClass      falseisMemberClass     falseisPrimitive       falseisSynthetic       falsesimple-name       ApplicationContextProvidermodifier          publicannotation        org.springframework.stereotype.Componentinterfaces        org.springframework.context.ApplicationContextAwaresuper-class       +-java.lang.Objectclass-loader      +-sun.misc.Launcher$AppClassLoader@18b4aac2+-sun.misc.Launcher$ExtClassLoader@5762806eclassLoaderHash   18b4aac2Affect(row-cnt:1) cost in 22 ms.
[arthas@98568]$**************************************************************
* last: invoke spring bean to reload mybatis mapper xml file *
**************************************************************arthas start command :/Users/wangji/opt/arthas/as.sh --height 100 --width 200 --select 98568  -c "ognl -x 3 '#springContext=@com.boot.mybatis.mybatisdemo.common.ApplicationContextProvider@context,#springContext.getBean("mybatisMapperXmlFileReloadService").reloadAllSqlSessionFactoryMapper("/Users/wangji/opt/arthas/mybatis/mapper/UserDoMapper.xml")' -c 18b4aac2"  | tee /Users/wangji/opt/arthas/mybatisMapperXmlReloadResult.out
Arthas script version: 3.5.0
[INFO] JAVA_HOME: /Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home
[INFO] Process 98568 already using port 3658
[INFO] Process 98568 already using port 8563
Arthas home: /Users/wangji/opt/arthas
Calculating attach execution time...
Attaching to 98568 using version /Users/wangji/opt/arthas...real	0m0.212s
user	0m0.383s
sys	0m0.051s
Attach success.,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---./  O  \ |  .--. ''--.  .--'|  '--'  | /  O  \ '   .-'
|  .-.  ||  '--'.'   |  |   |  .--.  ||  .-.  |`.  `-.
|  | |  ||  |\  \    |  |   |  |  |  ||  | |  |.-'    |
`--' `--'`--' '--'   `--'   `--'  `--'`--' `--'`-----'wiki       https://arthas.aliyun.com/doc
tutorials  https://arthas.aliyun.com/doc/arthas-tutorials.html
version    3.5.0
main_class com.boot.mybatis.mybatisdemo.MybatisDemoApplication
pid        98568
time       2021-05-10 22:00:59Mapper("/Users/wangji/opt/arthas/mybatis/mapper/UserDoMapper.xml")' -c 18b4aac2 | plaintextontextProvider@context,#springContext.getBean("mybatisMapperXmlFileReloadService").reloadAllSqlSessionFactory
@Boolean[true]
[arthas@98568]$
********************************************************
* arthas idea plugin mybatis mapper xml reload success *
********************************************************

2、解决的问题

  • 基于static spring context 调用bean 方法传递 路径,提供一个 spring bean 去执行热更新
  • 需要全局的自动化,解决classloader的传递问题,需要脚本去解决。

基于这样的一个思路,提供一个bean 一个路径即可
com.github.wangji92.mybatis.reload.core.MybatisMapperXmlFileReloadService#reloadAllSqlSessionFactoryMapper

详细代码地址可以参考:https://github.com/WangJi92/mybatis-mapper-reload-spring-boot-start
简单的实现有bug 欢迎提交pr

public boolean reloadAllSqlSessionFactoryMapper(String mapperFilePath) {if (CollectionUtils.isEmpty(sqlSessionFactoryList)) {log.warn("not find SqlSessionFactory bean");return false;}Path path = Paths.get(mapperFilePath);if (!Files.exists(path)) {log.warn("mybatis reload mapper xml not exist ={}", mapperFilePath);return false;}AtomicBoolean result = new AtomicBoolean(true);sqlSessionFactoryList.parallelStream().forEach(sqlSessionFactory -> {Configuration configuration = sqlSessionFactory.getConfiguration();if (!this.removeMapperCacheAndReloadNewMapperFile(path, configuration)) {log.warn("reload new mapper file fail path={}", path.toString());result.set(false);} else {log.info("reload new mapper file success path={}", path.toString());}});return result.get();}

3、配置

3.1 配置static spring context

https://www.yuque.com/wangji-yunque/ikhsmq/ugrc8n#4XuEK 详情查看这里。
在这里插入图片描述

3.2 配置 reload mapper service bean name method name

在这里插入图片描述

4、测试

demo link: https://github.com/WangJi92/mybatis-log-demo

4.1 old

<select id="selectByExample" parameterType="com.boot.mybatis.mybatisdemo.model.dataobject.UserDoExample" resultMap="BaseResultMap">select<if test="distinct">distinct</if><include refid="Base_Column_List" />from user<if test="_parameter != null"><include refid="Example_Where_Clause" /></if><if test="orderByClause != null">order by ${orderByClause}</if></select>

在这里插入图片描述

4.2 选择修改的xml 文件

4.2.1 构建脚本

在这里插入图片描述

4.2.2 修改的xml
<select id="selectByExample" parameterType="com.boot.mybatis.mybatisdemo.model.dataobject.UserDoExample" resultMap="BaseResultMap">selectnamefrom user<if test="_parameter != null"><include refid="Example_Where_Clause" /></if><if test="orderByClause != null">order by ${orderByClause}</if></select>
4.2.3 执行后的结果

在这里插入图片描述

在这里插入图片描述

这篇关于牛叉了-arthas 热更新 mybatis mapper xml的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1091570

相关文章

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

SQL Server跟踪自动统计信息更新实战指南

《SQLServer跟踪自动统计信息更新实战指南》本文详解SQLServer自动统计信息更新的跟踪方法,推荐使用扩展事件实时捕获更新操作及详细信息,同时结合系统视图快速检查统计信息状态,重点强调修... 目录SQL Server 如何跟踪自动统计信息更新:深入解析与实战指南 核心跟踪方法1️⃣ 利用系统目录

MyBatis-Plus 自动赋值实体字段最佳实践指南

《MyBatis-Plus自动赋值实体字段最佳实践指南》MyBatis-Plus通过@TableField注解与填充策略,实现时间戳、用户信息、逻辑删除等字段的自动填充,减少手动赋值,提升开发效率与... 目录1. MyBATis-Plus 自动赋值概述1.1 适用场景1.2 自动填充的原理1.3 填充策略

mybatis中resultMap的association及collectio的使用详解

《mybatis中resultMap的association及collectio的使用详解》MyBatis的resultMap定义数据库结果到Java对象的映射规则,包含id、type等属性,子元素需... 目录1.reusltmap的说明2.association的使用3.collection的使用4.总

mybatis-plus QueryWrapper中or,and的使用及说明

《mybatis-plusQueryWrapper中or,and的使用及说明》使用MyBatisPlusQueryWrapper时,因同时添加角色权限固定条件和多字段模糊查询导致数据异常展示,排查发... 目录QueryWrapper中or,and使用列表中还要同时模糊查询多个字段经过排查这就导致只要whe

SpringBoot集成MyBatis实现SQL拦截器的实战指南

《SpringBoot集成MyBatis实现SQL拦截器的实战指南》这篇文章主要为大家详细介绍了SpringBoot集成MyBatis实现SQL拦截器的相关知识,文中的示例代码讲解详细,有需要的小伙伴... 目录一、为什么需要SQL拦截器?二、MyBATis拦截器基础2.1 核心接口:Interceptor

SpringBoot中六种批量更新Mysql的方式效率对比分析

《SpringBoot中六种批量更新Mysql的方式效率对比分析》文章比较了MySQL大数据量批量更新的多种方法,指出REPLACEINTO和ONDUPLICATEKEY效率最高但存在数据风险,MyB... 目录效率比较测试结构数据库初始化测试数据批量修改方案第一种 for第二种 case when第三种

MyBatis-Plus通用中等、大量数据分批查询和处理方法

《MyBatis-Plus通用中等、大量数据分批查询和处理方法》文章介绍MyBatis-Plus分页查询处理,通过函数式接口与Lambda表达式实现通用逻辑,方法抽象但功能强大,建议扩展分批处理及流式... 目录函数式接口获取分页数据接口数据处理接口通用逻辑工具类使用方法简单查询自定义查询方法总结函数式接口

MyBatis中$与#的区别解析

《MyBatis中$与#的区别解析》文章浏览阅读314次,点赞4次,收藏6次。MyBatis使用#{}作为参数占位符时,会创建预处理语句(PreparedStatement),并将参数值作为预处理语句... 目录一、介绍二、sql注入风险实例一、介绍#(井号):MyBATis使用#{}作为参数占位符时,会

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推