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

相关文章

关于C++中的虚拟继承的一些总结(虚拟继承,覆盖,派生,隐藏)

1.为什么要引入虚拟继承 虚拟继承是多重继承中特有的概念。虚拟基类是为解决多重继承而出现的。如:类D继承自类B1、B2,而类B1、B2都继承自类A,因此在类D中两次出现类A中的变量和函数。为了节省内存空间,可以将B1、B2对A的继承定义为虚拟继承,而A就成了虚拟基类。实现的代码如下: class A class B1:public virtual A; class B2:pu

解析 XML 和 INI

XML 1.TinyXML库 TinyXML是一个C++的XML解析库  使用介绍: https://www.cnblogs.com/mythou/archive/2011/11/27/2265169.html    使用的时候,只要把 tinyxml.h、tinystr.h、tinystr.cpp、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.

持久层 技术选型如何决策?JPA,Hibernate,ibatis(mybatis)

转自:http://t.51jdy.cn/thread-259-1-1.html 持久层 是一个项目 后台 最重要的部分。他直接 决定了 数据读写的性能,业务编写的复杂度,数据结构(对象结构)等问题。 因此 架构师在考虑 使用那个持久层框架的时候 要考虑清楚。 选择的 标准: 1,项目的场景。 2,团队的技能掌握情况。 3,开发周期(开发效率)。 传统的 业务系统,通常业

ONLYOFFICE 8.1 版本桌面编辑器测评

在现代办公环境中,办公软件的重要性不言而喻。从文档处理到电子表格分析,再到演示文稿制作,强大且高效的办公软件工具能够极大提升工作效率。ONLYOFFICE 作为一个功能全面且开源的办公软件套件,一直以来都受到广大用户的关注与喜爱。而其最新发布的 ONLYOFFICE 8.1 版本桌面编辑器,更是带来了诸多改进和新特性。本文将详细评测 ONLYOFFICE 8.1 版本桌面编辑器,探讨其在功能、用户

17.用300行代码手写初体验Spring V1.0版本

1.1.课程目标 1、了解看源码最有效的方式,先猜测后验证,不要一开始就去调试代码。 2、浓缩就是精华,用 300行最简洁的代码 提炼Spring的基本设计思想。 3、掌握Spring框架的基本脉络。 1.2.内容定位 1、 具有1年以上的SpringMVC使用经验。 2、 希望深入了解Spring源码的人群,对 Spring有一个整体的宏观感受。 3、 全程手写实现SpringM

android 带与不带logo的二维码生成

该代码基于ZXing项目,这个网上能下载得到。 定义的控件以及属性: public static final int SCAN_CODE = 1;private ImageView iv;private EditText et;private Button qr_btn,add_logo;private Bitmap logo,bitmap,bmp; //logo图标private st

Visual Studio中,MSBUild版本问题

假如项目规定了MSBUild版本,那么在安装完Visual Studio后,假如带的MSBUild版本与项目要求的版本不符合要求,那么可以把需要的MSBUild添加到系统中,然后即可使用。步骤如下:            假如项目需要使用V12的MSBUild,而安装的Visual Studio带的MSBUild版本为V14。 ①到MSDN下载V12 MSBUild包,把V12包解压到目录(

Pycharm配置conda环境(解决新版本无法识别可执行文件问题)

引言: 很多小伙伴在下载最新版本的pycharm或者更新到最新版本后为项目配置conda环境的时候,发现文件夹目录中无法显示可执行文件(一般为python.exe),以下就是本人遇到该问题后试验和解决该问题的一些方法和思路。 一般遇到该问题的人群有两种,一种是刚入门对pycharm进行conda环境配置的小白(例如我),不熟悉相关环境配置的操作和过程,还有一种是入坑pycharm有段时间的老手

FastAdmin/bootstrapTable 表格中生成的按钮设置成文字

公司有个系统后台框架用的是FastAdmin,后台表格的操作栏按钮只有图标,想要设置成文字。 查资料后发现其实很简单,主需要新增“text”属性即可,如下 buttons: [{name: 'acceptcompany',title: '复核企业',text:'复核企业',classname: 'btn btn-xs btn-primary btn-dialog',icon: 'fa fa-pe

下载Xcode的历史版本

1.打开链接:https://developer.apple.com/download/more 进入页面 2.在搜索框输入Xcode,回车搜索.如图,找到各种版本Xcode 搜索Xcode 3.双击要下载的Xcode,或者点击前面的+,打开详细.点击下载 下载Xcode 4.接下来就耐心等待下载吧!