問題:html
使用標題所述的generator,在生成xxxMapper.xml文件後,再生成一次,新的內容會以追加的方式加入到原來的xxxMapper.xml文件中。(一般我是但願覆蓋的)java
尋找到的緣由:sql
在IntrospectedTableMyBatis3Impl.getGeneratedXmlFiles方法中,isMergeable值被寫死爲true了。shell
GeneratedXmlFile gxf = new GeneratedXmlFile(document, getMyBatis3XmlMapperFileName(), getMyBatis3XmlMapperPackage(), context.getSqlMapGeneratorConfiguration().getTargetProject(), true, context.getXmlFormatter());
而MyBatisGenerator.writeGeneratedXmlFile方法中使用到該屬性了。代碼以下:mybatis
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行,結果致使每次從新生成後都是追加。app
解決方法:dom
我認爲這是一個小bug,爲了避免用修改源碼,從新打包,形成包不一致,我仍是但願在運行時處理它。通過一番折騰,終於找到方法了。使用反射在運行時把isMergeable強制改爲false。ide
具體作法是:url
1.編寫一個插件spa
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哦,否則的話,每次生成的文件都會在文件名最後加個「點數字」---緣由從前面貼的第二段代碼中能夠找到)