转载自http://www.voidcn.com/article/p-wbxpqlmy-bon.html
问题:
使用标题所述的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 {
@Override
public boolean validate(List<String> warnings) {
return true;
}
@Override
public 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 generatorConfiguration
PUBLIC "-//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哦,不然的话,每次生成的文件都会在文件名最后加个“点数字”—原因从前面贴的第二段代码中可以找到)