• Mybatis Generator 扩展


    目标

    1. 修改Model的名称
    2. 修改Dao的名称

    配置文件

    context.targetRuntime 替换为自定义的类型

    原理在:org.mybatis.generator.internal.ObjectFactory里

    <?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="srcmainjava">
                <property name="constructorBased" value="true" />
                <property name="enableSubPackages" value="true"/>
                <property name="trimStrings" value="true"/>
            </javaModelGenerator>
            <!-- 生成映射文件的包名和位置 -->
            <sqlMapGenerator targetPackage="mapper"
                             targetProject="srcmain
    esources">
                <property name="enableSubPackages" value="true"/>
            </sqlMapGenerator>
            <!-- 生成DAO的包名和位置 -->
            <javaClientGenerator type="XMLMAPPER"
                                 targetPackage="eerp.dao" implementationPackage="eerp.dao.impl"
                                 targetProject="srcmainjava">
                <property name="enableSubPackages" value="true"/>
            </javaClientGenerator>
    
            <!-- 要生成哪些表 -->
            <table tableName="T_MGT_Group"
                   enableCountByExample="false" enableUpdateByExample="false"
                   enableDeleteByExample="false" enableSelectByExample="false"
                   selectByExampleQueryId="false"></table>
    
    
        </context>
    </generatorConfiguration>
    

    自定义IntrospectedTableMyBatis3Impl的子类

    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;
        }
    }
    

    代码方式运行mybatis generator

    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);
        }
    }
    
  • 相关阅读:
    修复ecshop商品重量BUG小数位增至五位
    ECSHOP 支付宝发货确认接口,记录支付宝返回的交易号
    php数字补零的两种方法
    PHP获取当前时间的毫秒数(yyyyMMddHHmmssSSS)
    ajax 设置Access-Control-Allow-Origin实现跨域访问
    MySQL Master High Available 源码篇
    MHA 报错:There is no alive slave. We can't do failover
    cdid
    mha error
    mysql relay log参数汇总
  • 原文地址:https://www.cnblogs.com/byxxw/p/6841020.html
Copyright © 2020-2023  润新知