zoukankan      html  css  js  c++  java
  • Mybatis 逆向工程

    Mybatis逆向工程:

    推荐用Java和XML Configuration的方式生成逆向文件

    Java类:

    package generation;
    
    import java.io.File;
    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.internal.DefaultShellCallback;
    
    public class Generation {
    
        public void generation() throws Exception {
            List<String> warnings = new ArrayList<String>();
            boolean overwrite = true;
            File configFile = new File("generatorConfig.xml");
            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);
        }
    
        public static void main(String[] args) {
            Generation generation = new Generation();
            try {
                generation.generation();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    }

    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">
    <generatorConfiguration>
    
        <!--数据库驱动 -->
        <classPathEntry
            location="E:JavaPackJARMySQLmysql-connector-java-5.1.28-bin.jar" />
        <context id="DB2Tables" targetRuntime="MyBatis3">
            <commentGenerator>
                <property name="suppressDate" value="true" />
                <property name="suppressAllComments" value="true" />
            </commentGenerator>
            <!--数据库链接地址账号密码 -->
            <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                connectionURL="jdbc:mysql://172.22.144.214/HaiNan" userId="root"
                password="Abcd1234">
            </jdbcConnection>
            <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 
                和 NUMERIC 类型解析为java.math.BigDecimal -->
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false" />
            </javaTypeResolver>
            <!--生成Model类存放位置 -->
            <javaModelGenerator targetPackage="cn.edu.hainan.model"
                targetProject="src">
                <property name="enableSubPackages" value="true" />
                <property name="trimStrings" value="true" />
                
            </javaModelGenerator>
            <!--生成映射文件存放位置 -->
            <sqlMapGenerator targetPackage="cn.edu.hainan.mapper"
                targetProject="src">
                <property name="enableSubPackages" value="true" />
            </sqlMapGenerator>
            <!--生成Dao类存放位置 -->
            <javaClientGenerator type="XMLMAPPER"
                targetPackage="cn.edu.hainan.mapper" targetProject="src">
                <property name="enableSubPackages" value="true" />
            </javaClientGenerator>
            <!--生成对应表及类名 -->
            <table tableName="company" domainObjectName="Company"
                enableCountByExample="false" enableUpdateByExample="false"
                enableDeleteByExample="false" enableSelectByExample="false"
                selectByExampleQueryId="false">
                <property name="useActualColumnNames" value="true" />
            </table>
        </context>
        
    </generatorConfiguration>

    注意:在表中如果<table>标签中没有配置<property name="useActualColumnNames" value="true" />,生成的类的属性不符合驼峰命名的规则

  • 相关阅读:
    springboot之scheduled任务调度
    nginx部署与安装
    hive 导出如数为csv格式
    hive函数应用之操作json
    解决Azure 消息队列ServiceBus提示证书不信任无权限的问题
    读书笔记--《不能承受的生命之轻》读后感
    localhost和127.0.01 区别
    C# 利用反射动态加载dll
    .NET MVC 简单的插件式开发
    爬虫发起抓取被服务器拒绝访问返回403禁止访问解决方案
  • 原文地址:https://www.cnblogs.com/googlemeoften/p/4922282.html
Copyright © 2011-2022 走看看