MyBatis Generator系列(八)----MyBatis Generator自定義外掛實現自定義Mapper
阿新 • • 發佈:2019-02-09
一、建立基類
有時候會通過建立實體的基類,用來複用一些程式碼,然後讓其他的實體類整合這個類:
package com.fendo.bean; /** * @Title: BaseModel.java * @Package com.fendo.bean * @Description: 實體基類 * @author fendo * @date 2017年12月2日 下午5:32:40 * @version V1.0 */ public class BaseModel { private String createDate; private String createName; private String updateDate; private String updateName; public String getCreateDate() { return createDate; } public void setCreateDate(String createDate) { this.createDate = createDate; } public String getCreateName() { return createName; } public void setCreateName(String createName) { this.createName = createName; } public String getUpdateDate() { return updateDate; } public void setUpdateDate(String updateDate) { this.updateDate = updateDate; } public String getUpdateName() { return updateName; } public void setUpdateName(String updateName) { this.updateName = updateName; } }
然後建立一個Mapper基類
/** * @Title: BaseMapper.java * @Package com.fendo.mapper * @Description: Mapper基類 * @author fendo * @date 2017年12月2日 下午5:33:05 * @version V1.0 */ public interface BaseMapper<T extends BaseModel> { List<T> selectAll(); int deleteByPrimaryKey(String id); int insert(T record); int insertSelective(T record); T selectByPrimaryKey(String id); int updateByPrimaryKey(T record); int updateByPrimaryKeySelective(T record); }
二、配置generatorConfig.xml
引入外掛:
<!-- 自動生成tostring方法 --> <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/> <!-- 自動生成equals方法和hashcode方法 --> <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/> <!-- 自定義方法 --> <plugin type="org.mybatis.generator.plugins.MapperPlugin"> <property name="targetProject" value="../mybatis-generator-oracle/src/main/java"/> <property name="targetPackage" value="com.fendo.mapper"/> </plugin> <commentGenerator> <!-- 設定編碼為UTF-8 --> <property name="javaFileEncoding" value="UTF-8"/> <!--配置生成註釋,預設註釋已經修改 --> <property name="suppressAllComments" value="false"/> </commentGenerator>
然後給mapper和bean新增基類
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="com.fendo.bean" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
<property name="rootClass" value="com.fendo.bean.BaseModel"/>
</javaModelGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.fendo.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="rootInterface" value="com.fendo.mapper.BaseMapper"/>
</javaClientGenerator>
三、建立MapperPlugin
建立自己的類然後實現PluginAdapter
package org.mybatis.generator.plugins;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
import static org.mybatis.generator.internal.util.messages.Messages.getString;
import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.JavaFormatter;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.ShellCallback;
import org.mybatis.generator.api.dom.java.CompilationUnit;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.exception.ShellException;
import org.mybatis.generator.internal.DefaultShellCallback;
/**
* @Title: MapperPlugin.java
* @Package org.mybatis.generator.plugins
* @Description: TODO
* @author fendo
* @date 2017年12月2日 下午5:35:14
* @version V1.0
*/
public class MapperPlugin extends PluginAdapter{
private static final String DEFAULT_DAO_SUPER_CLASS = "com.fendo.mapper.BaseMapper";
private static final String DEFAULT_EXPAND_DAO_SUPER_CLASS = "com.fendo.mapper.BaseExpandMapper";
private String daoTargetDir;
private String daoTargetPackage;
private String daoSuperClass;
// 擴充套件
private String expandDaoTargetPackage;
private String expandDaoSuperClass;
private ShellCallback shellCallback = null;
public MapperPlugin() {
shellCallback = new DefaultShellCallback(false);
}
/**
* 驗證引數是否有效
* @param warnings
* @return
*/
public boolean validate(List<String> warnings) {
daoTargetDir = properties.getProperty("targetProject");
boolean valid = stringHasValue(daoTargetDir);
daoTargetPackage = properties.getProperty("targetPackage");
boolean valid2 = stringHasValue(daoTargetPackage);
daoSuperClass = properties.getProperty("daoSuperClass");
if (!stringHasValue(daoSuperClass)) {
daoSuperClass = DEFAULT_DAO_SUPER_CLASS;
}
expandDaoTargetPackage = properties.getProperty("expandTargetPackage");
expandDaoSuperClass = properties.getProperty("expandDaoSuperClass");
if (!stringHasValue(expandDaoSuperClass)) {
expandDaoSuperClass = DEFAULT_EXPAND_DAO_SUPER_CLASS;
}
return valid && valid2;
}
/**
* 生成mapping 新增自定義sql
*/
@Override
public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
//建立Select查詢
XmlElement select = new XmlElement("select");
select.addAttribute(new Attribute("id", "selectAll"));
select.addAttribute(new Attribute("resultMap", "BaseResultMap"));
select.addAttribute(new Attribute("parameterType", introspectedTable.getBaseRecordType()));
select.addElement(new TextElement("select * from "+ introspectedTable.getFullyQualifiedTableNameAtRuntime()));
XmlElement queryPage = new XmlElement("select");
queryPage.addAttribute(new Attribute("id", "queryPage"));
queryPage.addAttribute(new Attribute("resultMap", "BaseResultMap"));
queryPage.addAttribute(new Attribute("parameterType", "com.fendo.bean.Page"));
queryPage.addElement(new TextElement("select * from "+ introspectedTable.getFullyQualifiedTableNameAtRuntime()));
XmlElement parentElement = document.getRootElement();
parentElement.addElement(select);
parentElement.addElement(queryPage);
return super.sqlMapDocumentGenerated(document, introspectedTable);
}
public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) {
JavaFormatter javaFormatter = context.getJavaFormatter();
List<GeneratedJavaFile> mapperJavaFiles = new ArrayList<GeneratedJavaFile>();
for (GeneratedJavaFile javaFile : introspectedTable.getGeneratedJavaFiles()) {
CompilationUnit unit = javaFile.getCompilationUnit();
FullyQualifiedJavaType baseModelJavaType = unit.getType();
String shortName = baseModelJavaType.getShortName();
GeneratedJavaFile mapperJavafile = null;
if (shortName.endsWith("Mapper")) { // 擴充套件Mapper
if (stringHasValue(expandDaoTargetPackage)) {
Interface mapperInterface = new Interface(
expandDaoTargetPackage + "." + shortName.replace("Mapper", "ExpandMapper"));
mapperInterface.setVisibility(JavaVisibility.PUBLIC);
mapperInterface.addJavaDocLine("/**");
mapperInterface.addJavaDocLine(" * " + shortName + "擴充套件");
mapperInterface.addJavaDocLine(" */");
FullyQualifiedJavaType daoSuperType = new FullyQualifiedJavaType(expandDaoSuperClass);
mapperInterface.addImportedType(daoSuperType);
mapperInterface.addSuperInterface(daoSuperType);
mapperJavafile = new GeneratedJavaFile(mapperInterface, daoTargetDir, javaFormatter);
try {
File mapperDir = shellCallback.getDirectory(daoTargetDir, daoTargetPackage);
File mapperFile = new File(mapperDir, mapperJavafile.getFileName());
// 檔案不存在
if (!mapperFile.exists()) {
mapperJavaFiles.add(mapperJavafile);
}
} catch (ShellException e) {
e.printStackTrace();
}
}
} else if (!shortName.endsWith("Example")) { // CRUD Mapper
Interface mapperInterface = new Interface(daoTargetPackage + "." + shortName + "Mapper");
mapperInterface.setVisibility(JavaVisibility.PUBLIC);
mapperInterface.addJavaDocLine("/**");
mapperInterface.addJavaDocLine(" * MyBatis Generator工具自動生成");
mapperInterface.addJavaDocLine(" */");
FullyQualifiedJavaType daoSuperType = new FullyQualifiedJavaType(daoSuperClass);
// 新增泛型支援
daoSuperType.addTypeArgument(baseModelJavaType);
mapperInterface.addImportedType(baseModelJavaType);
mapperInterface.addImportedType(daoSuperType);
mapperInterface.addSuperInterface(daoSuperType);
mapperJavafile = new GeneratedJavaFile(mapperInterface, daoTargetDir, javaFormatter);
mapperJavaFiles.add(mapperJavafile);
}
}
return mapperJavaFiles;
}
}
四、生成程式碼
建立測試類:
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
/**
* @Title: StartUp.java
* @Package com.fendo.mybatis_generator_plus
* @Description: mybatis-generator測試類
* @author fendo
* @date 2017年10月5日 下午3:53:17
* @version V1.0
*/
public class StartUp {
public static void main(String[] args) throws URISyntaxException {
try {
System.out.println("--------------------start generator-------------------");
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(is);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
System.out.println("--------------------end generator-------------------");
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (InvalidConfigurationException e) {
e.printStackTrace();
} catch (XMLParserException e) {
e.printStackTrace();
}
}
}
然後執行MBG,生成的程式碼結構如下:
生成的Healthyorder實體類預設繼承了BaseModel
然後就是HealthyorderMapper對映檔案,它預設是繼承BaseMapper<Healthyorder>類,然後就可以使用BaseMapper裡面的方法,生成的xml檔案如下:
注意:
由於我這裡直接是在原始碼裡面改的,然後測試的,並沒有打包成jar,執行的。
完整的原始碼: