context.targetRuntime 替換爲自定義的類型java
原理在:org.mybatis.generator.internal.ObjectFactory裏sql
<?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"> <generatorConfiguration> <context id="test" targetRuntime="eerp.mybatis.generator.plugin.CnoocIntrospectedTable" defaultModelType="flat"> <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin> <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin> <commentGenerator> <!-- 這個元素用來去除指定生成的註釋中是否包含生成的日期 false:表示保護 --> <!-- 若是生成日期,會形成即便修改一個字段,整個實體類全部屬性都會發生變化,不利於版本控制,因此設置爲true --> <property name="suppressDate" value="true"/> <!-- 是否去除自動生成的註釋 true:是 : false:否 --> <property name="suppressAllComments" value="false"/> </commentGenerator> <!--數據庫連接URL,用戶名、密碼 --> <jdbcConnection driverClass="net.sourceforge.jtds.jdbc.Driver" connectionURL="jdbc:jtds:sqlserver://10.68.108.3:1433;DatabaseName=CMAPE" userId="eerpsa" password="eerpP@ssword"> </jdbcConnection> <javaTypeResolver> <!-- This property is used to specify whether MyBatis Generator should force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, --> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成模型的包名和位置 --> <javaModelGenerator targetPackage="eerp.model" targetProject="src\main\java"> <property name="constructorBased" value="true" /> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成映射文件的包名和位置 --> <sqlMapGenerator targetPackage="mapper" targetProject="src\main\resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成DAO的包名和位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="eerp.dao" implementationPackage="eerp.dao.impl" targetProject="src\main\java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 要生成哪些表 --> <table tableName="T_MGT_Group" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context> </generatorConfiguration>
public class CnoocIntrospectedTable extends IntrospectedTableMyBatis3Impl { private Logger logger = Logger.getLogger(CnoocIntrospectedTable.class); @Override protected void calculateJavaClientAttributes() { super.calculateJavaClientAttributes(); StringBuffer sb = new StringBuffer(); sb.append(this.calculateJavaClientInterfacePackage()); sb.append('.'); // sb.append(this.fullyQualifiedTable.getDomainObjectName()); sb.append(this.generateDomainName()); sb.append("Dao"); String mapperName = sb.toString(); logger.debug("Rename MyBatis3JavaMapperTypeName:" + mapperName); super.setMyBatis3JavaMapperType(mapperName); } @Override public void calculateModelAttributes(){ super.calculateModelAttributes(); String packageName = super.calculateJavaModelPackage(); StringBuilder sb = new StringBuilder(); sb.append(packageName); sb.append('.'); // sb.append(this.fullyQualifiedTable.getDomainObjectName()); String newDomainName = this.generateDomainName(); sb.append(newDomainName); logger.debug("Rename setBaseRecordType:" + newDomainName); this.setBaseRecordType(sb.toString()); } protected String generateDomainName() { String newDomainName = this.fullyQualifiedTable.getIntrospectedTableName(); int index = newDomainName.lastIndexOf("_"); if (index > 0) { newDomainName = newDomainName.substring(index + 1); } else { newDomainName = this.fullyQualifiedTable.getDomainObjectName(); } return newDomainName; } }
public class MybatisGeneratorRunner { public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; URL url = JavaGeneratorRunner.class.getClassLoader().getResource("generatorConfig.xml"); File configFile = new File(url.getFile()); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); } }