Mybatis code generator1.3.4版本 XML 文件重新生成不会覆盖原文件

本文主要是介绍Mybatis code generator1.3.4版本 XML 文件重新生成不会覆盖原文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

转载

原文 https://my.oschina.net/u/137785/blog/736372

问题:

使用标题所述的generator,在生成xxxMapper.xml文件后,再生成一次,新的内容会以追加的方式加入到原来的xxxMapper.xml文件中。(通常我是希望覆盖的)

寻找到的原因:

在IntrospectedTableMyBatis3Impl.getGeneratedXmlFiles方法中,isMergeable值被写死为true了。
GeneratedXmlFile gxf = new GeneratedXmlFile(document,getMyBatis3XmlMapperFileName(), getMyBatis3XmlMapperPackage(),context.getSqlMapGeneratorConfiguration().getTargetProject(),true, context.getXmlFormatter()); 而MyBatisGenerator.writeGeneratedXmlFile方法中使用到该属性了。代码如下:if (targetFile.exists()) {if (gxf.isMergeable()) {source = XmlFileMergerJaxp.getMergedSource(gxf, targetFile);} else if (shellCallback.isOverwriteEnabled()) {source = gxf.getFormattedContent();warnings.add(getString("Warning.11", targetFile.getAbsolutePath()));} else {source = gxf.getFormattedContent();targetFile = getUniqueFileName(directory, gxf.getFileName());warnings.add(getString("Warning.2", targetFile.getAbsolutePath())); //$NON-NLS-1$} } else {source = gxf.getFormattedContent(); }

关键点就在第2行,结果导致每次重新生成后都是追加。

解决方法:

我认为这是一个小bug,为了不用修改源码,重新打包,造成包不一致,我还是希望在运行时处理它。经过一番折腾,终于找到方法了。使用反射在运行时把isMergeable强制改成false。

具体做法是:

1.编写一个插件

public class OverIsMergeablePlugin extends PluginAdapter {@Overridepublic boolean validate(List<String> warnings) {return true;}@Overridepublic boolean sqlMapGenerated(GeneratedXmlFile sqlMap, IntrospectedTable introspectedTable) {try {Field field = sqlMap.getClass().getDeclaredField("isMergeable");field.setAccessible(true);field.setBoolean(sqlMap, false);} catch (Exception e) {e.printStackTrace();}return true;}
}

2.配置generatorConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfigurationPUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"><!-- 详细文档 http://www.mybatis.org/generator/configreference/xmlconfig.html -->
<generatorConfiguration><properties resource="config.properties" /><context id="generatorContext" targetRuntime="${targetRuntime}"><plugin type="com.wql.customer.OverIsMergeablePlugin" /><commentGenerator type="com.wql.customer.CustomerCommentGenerator"><property name="suppressDate" value="false" /><property name="suppressAllComments" value="false" /><property name="addRemarkComments" value="true" /><property name="dateFormat" value="yyyy-MM-dd HH:mm:ss" /></commentGenerator><jdbcConnection driverClass="${jdbc.driver}" connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"></jdbcConnection><javaTypeResolver><property name="forceBigDecimals" value="false" /></javaTypeResolver><javaModelGenerator targetPackage="${model.package}" targetProject="${target.project}"><property name="enableSubPackages" value="true" /><property name="trimStrings" value="true" /></javaModelGenerator><sqlMapGenerator targetPackage="${xml.package}" targetProject="${target.project.resources}"><property name="enableSubPackages" value="true" /></sqlMapGenerator><javaClientGenerator targetPackage="${mapper.package}" targetProject="${target.project}" type="XMLMAPPER"><property name="enableSubPackages" value="true" /></javaClientGenerator><table tableName="${tableName}" domainObjectName="${domainObjectName}" enableCountByExample="${enableCountByExample}" enableUpdateByExample="${enableUpdateByExample}" enableDeleteByExample="${enableDeleteByExample}" enableSelectByExample="${enableSelectByExample}" selectByExampleQueryId="${selectByExampleQueryId}" /></context>
</generatorConfiguration>

3.运行生成程序

public static void main(String[] args) throws Exception {List<String> warnings = new ArrayList<String>();boolean overwrite = true;ConfigurationParser cp = new ConfigurationParser(warnings);Configuration config = cp.parseConfiguration(Main.class.getClassLoader().getResourceAsStream("generatorConfig.xml"));DefaultShellCallback callback = new DefaultShellCallback(overwrite);MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);myBatisGenerator.generate(null);System.out.println("----ok----");}

大功告成!嘻嘻!(对了,最后那个overwrite一定要设置为true哦,不然的话,每次生成的文件都会在文件名最后加个“点数字”—原因从前面贴的第二段代码中可以找到)

这篇关于Mybatis code generator1.3.4版本 XML 文件重新生成不会覆盖原文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Nginx进行平滑升级的实战指南(不中断服务版本更新)

《Nginx进行平滑升级的实战指南(不中断服务版本更新)》Nginx的平滑升级(也称为热升级)是一种在不停止服务的情况下更新Nginx版本或添加模块的方法,这种升级方式确保了服务的高可用性,避免了因升... 目录一.下载并编译新版Nginx1.下载解压2.编译二.替换可执行文件,并平滑升级1.替换可执行文件

Django HTTPResponse响应体中返回openpyxl生成的文件过程

《DjangoHTTPResponse响应体中返回openpyxl生成的文件过程》Django返回文件流时需通过Content-Disposition头指定编码后的文件名,使用openpyxl的sa... 目录Django返回文件流时使用指定文件名Django HTTPResponse响应体中返回openp

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

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

在macOS上安装jenv管理JDK版本的详细步骤

《在macOS上安装jenv管理JDK版本的详细步骤》jEnv是一个命令行工具,正如它的官网所宣称的那样,它是来让你忘记怎么配置JAVA_HOME环境变量的神队友,:本文主要介绍在macOS上安装... 目录前言安装 jenv添加 JDK 版本到 jenv切换 JDK 版本总结前言China编程在开发 Java

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

JAVA覆盖和重写的区别及说明

《JAVA覆盖和重写的区别及说明》非静态方法的覆盖即重写,具有多态性;静态方法无法被覆盖,但可被重写(仅通过类名调用),二者区别在于绑定时机与引用类型关联性... 目录Java覆盖和重写的区别经常听到两种话认真读完上面两份代码JAVA覆盖和重写的区别经常听到两种话1.覆盖=重写。2.静态方法可andro

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法