牛叉了-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

相关文章

mapstruct中的@Mapper注解的基本用法

《mapstruct中的@Mapper注解的基本用法》在MapStruct中,@Mapper注解是核心注解之一,用于标记一个接口或抽象类为MapStruct的映射器(Mapper),本文给大家介绍ma... 目录1. 基本用法2. 常用属性3. 高级用法4. 注意事项5. 总结6. 编译异常处理在MapSt

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

MyBatis ResultMap 的基本用法示例详解

《MyBatisResultMap的基本用法示例详解》在MyBatis中,resultMap用于定义数据库查询结果到Java对象属性的映射关系,本文给大家介绍MyBatisResultMap的基本... 目录MyBATis 中的 resultMap1. resultMap 的基本语法2. 简单的 resul

Mybatis的分页实现方式

《Mybatis的分页实现方式》MyBatis的分页实现方式主要有以下几种,每种方式适用于不同的场景,且在性能、灵活性和代码侵入性上有所差异,对Mybatis的分页实现方式感兴趣的朋友一起看看吧... 目录​1. 原生 SQL 分页(物理分页)​​2. RowBounds 分页(逻辑分页)​​3. Page

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

Mybatis Plus Join使用方法示例详解

《MybatisPlusJoin使用方法示例详解》:本文主要介绍MybatisPlusJoin使用方法示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录1、pom文件2、yaml配置文件3、分页插件4、示例代码:5、测试代码6、和PageHelper结合6

MyBatis设计SQL返回布尔值(Boolean)的常见方法

《MyBatis设计SQL返回布尔值(Boolean)的常见方法》这篇文章主要为大家详细介绍了MyBatis设计SQL返回布尔值(Boolean)的几种常见方法,文中的示例代码讲解详细,感兴趣的小伙伴... 目录方案一:使用COUNT查询存在性(推荐)方案二:条件表达式直接返回布尔方案三:存在性检查(EXI

MyBatis编写嵌套子查询的动态SQL实践详解

《MyBatis编写嵌套子查询的动态SQL实践详解》在Java生态中,MyBatis作为一款优秀的ORM框架,广泛应用于数据库操作,本文将深入探讨如何在MyBatis中编写嵌套子查询的动态SQL,并结... 目录一、Myhttp://www.chinasem.cnBATis动态SQL的核心优势1. 灵活性与可

Mybatis嵌套子查询动态SQL编写实践

《Mybatis嵌套子查询动态SQL编写实践》:本文主要介绍Mybatis嵌套子查询动态SQL编写方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、实体类1、主类2、子类二、Mapper三、XML四、详解总结前言MyBATis的xml文件编写动态SQL

Mybatis Plus JSqlParser解析sql语句及JSqlParser安装步骤

《MybatisPlusJSqlParser解析sql语句及JSqlParser安装步骤》JSqlParser是一个用于解析SQL语句的Java库,它可以将SQL语句解析为一个Java对象树,允许... 目录【一】jsqlParser 是什么【二】JSqlParser 的安装步骤【三】使用场景【1】sql语