• Mybatis反向生成


    最近在写一个接口中要自己手动建表和对应Mapper文件,这张表80个字段,我手动建立的文件总是会出现很多细小的问题,重点是还很难发现。主要是字段太多了,看的脑壳疼

    所以百度搜了很多可以根据数据库表自动生成对应的pojo实体类和Mapper文件

    1、创建一个generatorConfig.xml文件,注意:这个文件一定要放到src目录,否则在读取配置文件的时候可能会报空指针异常

    <?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="testTables" targetRuntime="MyBatis3">
                    <commentGenerator>
                        <!-- 是否去除自动生成的注释 true:是 : false:否 -->
                        <property name="suppressAllComments" value="true" />
                    </commentGenerator>
            
                    <!--数据库连接的信息:驱动类、连接地址、用户名、密码 
                    <jdbcConnection 
                        driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/taotao" 
                        userId="root"
                        password="root">
                    </jdbcConnection>-->
            
                    <!--<jdbcConnection
                        driverClass="oracle.jdbc.OracleDriver"
                        connectionURL="jdbc:oracle:thin:@远程数据库IP:1521:SID"
                        userId="rwkdev"
                        password="rwkdev">
                    </jdbcConnection>-->
            
             <jdbcConnection
                        driverClass="oracle.jdbc.OracleDriver"
                        connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:orcl"
                        userId="root"
                        password="admin">
                    </jdbcConnection>

    <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetProject:生成Model类的位置 --> <javaModelGenerator targetPackage="com.pojo" targetProject=".src"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- targetProject:mapper映射文件生成的位置 --> <sqlMapGenerator targetPackage="com.dao.mapper" targetProject=".src"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- targetPackage:mapper接口生成的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.dao" targetProject=".src"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 指定数据库表 ,生成对应表及类名,可以生成多个,复制下面这行代码改名--> <table domainObjectName="FraudulentOrder" tableName="T_DFP_FRAUDULENT_ORDER" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context> </generatorConfiguration>

    自己使用的时候只需要修改上面的jdbcConnection配置中的远程IP、端口、用户名、密码;还有一个就是domainObjectName属性中的值是你表生成的对应的实体类名,tableName对应你的表

    2、在创建一个GeneratorSqlmap.java类

            public class GeneratorSqlmap {
                public void generator() throws Exception{
                    List<String> warnings = new ArrayList<String>();
                    boolean overwrite = true;
                    //指定 逆向工程配置文件
                    File configFile = new File("src/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) throws Exception {
                    try {
                        GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
                        generatorSqlmap.generator();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

    3、添加三个jar 下载地址: https://www.lanzous.com/b00t84e8j   密码:crdl

  • 相关阅读:
    Groovy Urlencode编码
    PowerShell导入自定义公共函数
    Groovy获取对象类型、属性
    adb名称操作模拟器
    《坚不可摧》——乔布斯演讲
    马拉松节奏跑(转载)
    马拉松配速
    什么是思考力三要素?(转载)
    Python解析JSON为实体
    excel宏的用法
  • 原文地址:https://www.cnblogs.com/myfaith-feng/p/12202500.html
Copyright © 2020-2023  润新知