mybatis-generator擴充套件教程系列 -- 自定義配置引數修改DAO,Mapper檔案字尾
阿新 • • 發佈:2019-02-09
今天主要講解如何解決我們使用mybatis生成器遇到的最常見問題,如何修改生成的dao,mapper檔案字尾,下面我們繼續使用上一篇的用例繼續改造,如果本篇示例看得不太理解的可以翻看下之前的演示,下面就開始直奔主題了
1. 先增加一個引數配置看我們的檔案生成字尾是什麼,我們就新增個mapperExt引數寫在JavaParamConfiguration.java配置物件類
package org.mybatis.generator.config; import static org.mybatis.generator.internal.util.StringUtility.stringHasValue; import static org.mybatis.generator.internal.util.messages.Messages.getString; import java.util.List; import org.mybatis.generator.api.dom.xml.Attribute; import org.mybatis.generator.api.dom.xml.XmlElement; /** * @author shadow */ public class JavaParamConfiguration extends TypedPropertyHolder { private String baseDAO; // baseDAO類路徑 private String baseService; // baseService類路徑 private String buildType; // 1.dao層 2.service層 3.control層 private String mapperExt; // dao檔案字尾配置 public JavaParamConfiguration() { super(); } public String getBaseDAO() { return baseDAO; } public void setBaseDAO(String baseDAO) { this.baseDAO = baseDAO; } public String getBaseService() { return baseService; } public void setBaseService(String baseService) { this.baseService = baseService; } public String getBuildType() { return buildType; } public void setBuildType(String buildType) { this.buildType = buildType; } public String getMapperExt() { return mapperExt; } public void setMapperExt(String mapperExt) { this.mapperExt = mapperExt; } public XmlElement toXmlElement() { XmlElement answer = new XmlElement("javaParam"); //$NON-NLS-1$ if (baseDAO != null) { answer.addAttribute(new Attribute("baseDAO", baseDAO)); //$NON-NLS-1$ } if (baseService != null) { answer.addAttribute(new Attribute("baseService", baseService)); //$NON-NLS-1$ } if (buildType != null) { answer.addAttribute(new Attribute("buildType", buildType)); //$NON-NLS-1$ } if (mapperExt != null) { answer.addAttribute(new Attribute("mapperExt", mapperExt)); //$NON-NLS-1$ } addPropertyXmlElements(answer); return answer; } public void validate(List<String> errors, String contextId) { if (!stringHasValue(baseDAO)) { errors.add(getString("ValidationError.12", //$NON-NLS-1$ "JavaParamConfiguration", contextId)); //$NON-NLS-1$ } if (!stringHasValue(baseService)) { errors.add(getString("ValidationError.13", //$NON-NLS-1$ "JavaParamConfiguration", contextId)); //$NON-NLS-1$ } if (!stringHasValue(buildType)) { errors.add(getString("ValidationError.14", //$NON-NLS-1$ "JavaParamConfiguration", contextId)); //$NON-NLS-1$ } } }
2.修改mybatis-generator-config_1_0.dtd檔案,javaParam節點配置裡面加入一個引數
<!-- 自定義XML節點 -->
<!ELEMENT javaParam (property*)>
<!ATTLIST javaParam
baseDAO CDATA #REQUIRED
baseService CDATA #REQUIRED
buildType CDATA #REQUIRED
mapperExt CDATA #REQUIRED>
3.修改generatorConfig.xml檔案,在javaParam節點加入我們的引數值
<!--自定義XML引數配置 -->
<javaParam baseDAO="com.test.BaseDAO"
baseService="com.test.BaseService"
buildType="1,2" mapperExt="DAO">
</javaParam>
修改填充資料物件程式碼MyBatisGeneratorConfigurationParser.java
private void parseJavaParam(Context context, Node node) { JavaParamConfiguration javaParamConfiguration = new JavaParamConfiguration(); context.setJavaParamConfiguration(javaParamConfiguration); Properties attributes = parseAttributes(node); String baseDAO = attributes.getProperty("baseDAO"); //$NON-NLS-1$ String baseService = attributes.getProperty("baseService"); //$NON-NLS-1$ String buildType = attributes.getProperty("buildType"); //$NON-NLS-1$ String mapperExt = attributes.getProperty("mapperExt"); // 增加自定義引數填充 javaParamConfiguration.setBaseDAO(baseDAO); javaParamConfiguration.setBaseService(baseService); javaParamConfiguration.setBuildType(buildType); javaParamConfiguration.setMapperExt(mapperExt); NodeList nodeList = node.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node childNode = nodeList.item(i); if (childNode.getNodeType() != Node.ELEMENT_NODE) { continue; } if ("property".equals(childNode.getNodeName())) { //$NON-NLS-1$ parseProperty(javaParamConfiguration, childNode); } } }
4.我們來看看生成檔案的主要實現類IntrospectedTableMyBatis3Impl.java下面這段方法,我們可以看到第729行introspectedTable.initialize();初始化配置檔案
/**
* Generate files.
*
* @param callback
* the callback
* @param generatedJavaFiles
* the generated java files
* @param generatedXmlFiles
* the generated xml files
* @param warnings
* the warnings
* @throws InterruptedException
* the interrupted exception
*/
public void generateFiles(ProgressCallback callback,
List<GeneratedJavaFile> generatedJavaFiles,
List<GeneratedXmlFile> generatedXmlFiles, List<String> warnings)
throws InterruptedException {
pluginAggregator = new PluginAggregator();
for (PluginConfiguration pluginConfiguration : pluginConfigurations) {
Plugin plugin = ObjectFactory.createPlugin(this,
pluginConfiguration);
if (plugin.validate(warnings)) {
pluginAggregator.addPlugin(plugin);
} else {
warnings.add(getString("Warning.24", //$NON-NLS-1$
pluginConfiguration.getConfigurationType(), id));
}
}
if (introspectedTables != null) {
for (IntrospectedTable introspectedTable : introspectedTables) {
callback.checkCancel();
introspectedTable.initialize();
introspectedTable.calculateGenerators(warnings, callback);
generatedJavaFiles.addAll(introspectedTable
.getGeneratedJavaFiles());
generatedXmlFiles.addAll(introspectedTable
.getGeneratedXmlFiles());
generatedJavaFiles.addAll(pluginAggregator
.contextGenerateAdditionalJavaFiles(introspectedTable));
generatedXmlFiles.addAll(pluginAggregator
.contextGenerateAdditionalXmlFiles(introspectedTable));
}
}
generatedJavaFiles.addAll(pluginAggregator
.contextGenerateAdditionalJavaFiles());
generatedXmlFiles.addAll(pluginAggregator
.contextGenerateAdditionalXmlFiles());
}
5.我們繼續深入進去看IntrospectedTable.java的initialize()方法實現
public void initialize() {
calculateJavaClientAttributes();
calculateModelAttributes();
calculateXmlAttributes();
if (tableConfiguration.getModelType() == ModelType.HIERARCHICAL) {
rules = new HierarchicalModelRules(this);
} else if (tableConfiguration.getModelType() == ModelType.FLAT) {
rules = new FlatModelRules(this);
} else {
rules = new ConditionalModelRules(this);
}
context.getPlugins().initialized(this);
}
6.可以看到calculateJavaClientAttributes,calculateModelAttributes,calculateXmlAttributes三行程式碼應該就是初始化實現程式碼
我們把calculateJavaClientAttributes方法1345行程式碼,修改成如下
protected void calculateJavaClientAttributes() {
if (context.getJavaClientGeneratorConfiguration() == null) {
return;
}
StringBuilder sb = new StringBuilder();
sb.append(calculateJavaClientImplementationPackage());
sb.append('.');
sb.append(fullyQualifiedTable.getDomainObjectName());
sb.append("DAOImpl"); //$NON-NLS-1$
setDAOImplementationType(sb.toString());
sb.setLength(0);
sb.append(calculateJavaClientInterfacePackage());
sb.append('.');
sb.append(fullyQualifiedTable.getDomainObjectName());
sb.append("DAO"); //$NON-NLS-1$
setDAOInterfaceType(sb.toString());
sb.setLength(0);
sb.append(calculateJavaClientInterfacePackage());
sb.append('.');
if (stringHasValue(tableConfiguration.getMapperName())) {
sb.append(tableConfiguration.getMapperName());
} else {
sb.append(fullyQualifiedTable.getDomainObjectName());
// sb.append("Mapper"); //$NON-NLS-1$
sb.append(context.getJavaParamConfiguration().getMapperExt()); // 修改自定義字尾名稱
}
setMyBatis3JavaMapperType(sb.toString());
sb.setLength(0);
sb.append(calculateJavaClientInterfacePackage());
sb.append('.');
if (stringHasValue(tableConfiguration.getSqlProviderName())) {
sb.append(tableConfiguration.getSqlProviderName());
} else {
sb.append(fullyQualifiedTable.getDomainObjectName());
sb.append("SqlProvider"); //$NON-NLS-1$
}
setMyBatis3SqlProviderType(sb.toString());
}
7.最後看看我們的執行生成結果
最後小結,示例是沿用之前的講解用例,所以如果有的地方沒看明白可以翻看下我之前的講解教程,謝謝大家的支援