zoukankan      html  css  js  c++  java
  • MyBatisGenerator自动生成代码

    MyBatisGenerator自动生成代码

    generatorConfig.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="D:/jar/mysql-connector-java-8.0.18.jar"/>
        <context id="my" targetRuntime="MyBatis3">
            <commentGenerator>
                <property name="suppressDate" value="false"/>
                <property name="suppressAllComments" value="true"/>
            </commentGenerator>
    
            <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                            connectionURL="jdbc:mysql://localhost:3306/db_ssm?serverTimezone=UTC&amp;characterEncoding=utf8&amp;useUnicode=true&amp;useSSL=false"
                            userId="root"
                            password="123456"/>
            <!--pojo-->
            <javaModelGenerator targetPackage="org.demombts.pojo"
                                targetProject="srcmainjava">
                <property name="enableSubPackages" value="true"/>
                <property name="trimStrings" value="true"/>
            </javaModelGenerator>
            <!--sqlMapper.xml-->
            <sqlMapGenerator targetPackage="sqlMapper"
                             targetProject="srcmain
    esources">
                <property name="enableSubPackages" value="true"/>
            </sqlMapGenerator>
            <!--Mapper-->
            <javaClientGenerator targetPackage="org.demombts.mapper"
                                 targetProject="srcmainjava" type="XMLMAPPER">
                <property name="enableSubPackages" value="true"/>
            </javaClientGenerator>
    
    <!--        <table tableName="tbl_course" domainObjectName="Course"-->
    <!--               enableCountByExample="false" enableUpdateByExample="false"-->
    <!--               enableDeleteByExample="false" enableSelectByExample="false"-->
    <!--               selectByExampleQueryId="false">-->
    <!--            <columnRenamingRule searchString="^D_" replaceString=""/> &lt;!&ndash;'_'to驼峰命名&ndash;&gt;-->
    <!--        </table>-->
    
            <table tableName="tbl_phone" domainObjectName="Phone"
                   enableCountByExample="false" enableUpdateByExample="false"
                   enableDeleteByExample="false" enableSelectByExample="false"
                   selectByExampleQueryId="false">
                <columnRenamingRule searchString="^D_" replaceString=""/> <!--'_'to驼峰命名-->
            </table>
    
        </context>
    </generatorConfiguration>

     

    ①使用JAVA代码

     pom.xml只需要加入依赖,不用添加mvn插件mybatis-generator-maven-plugin

            <dependency>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-core</artifactId>
                <version>1.3.7</version>
            </dependency>
        public static void main(String[] args) {
            System.out.println("-------------------Hello World!  Ready To MybatisGenerator!");
            try {
                generator();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private static void generator() throws Exception {
            List<String> warnings = new ArrayList<String>();
            File configFile = new File("src\main\resources\mybatis-generator\generatorConfig.xml");// the path of the 'generatorConfig.xml'
            ConfigurationParser cp = new ConfigurationParser(warnings);
            Configuration config = cp.parseConfiguration(configFile);
            DefaultShellCallback callback = new DefaultShellCallback(true);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
            myBatisGenerator.generate(null);
            // 输出 warning 日志
            warnings.forEach(System.out::println);
            System.out.println("-------------------MybatisGenerator Succeed!");
        }

    ②使用mvn插件

    pom.xml需要加入mvn插件mybatis-generator-maven-plugin

    命令:mvn mybatis-generator:generate

    <plugin>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.7</version>
        <configuration>
            <configurationFile>src/main/resources/mybatis-generator/generatorConfig.xml</configurationFile> 指定xml路径
            <verbose>true</verbose>
            <overwrite>true</overwrite>
        </configuration>
        <executions>
            <execution>
                <id>Generate MyBatis Artifacts</id>
                <goals>
                    <goal>generate</goal>
                </goals>
            </execution>
        </executions>
        <dependencies>
            <dependency>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-core</artifactId>
                <version>1.3.7</version>
            </dependency>
        </dependencies>
    </plugin>

    作者: BORS

  • 相关阅读:
    C++中使用多线程
    hdu 4223 dp 求连续子序列的和的绝对值最小值
    hdu 1372 bfs 计算起点到终点的距离
    hdu 4217 线段树 依次取第几个最小值,求其sum
    心得
    hdu 1175 bfs 按要求进行搜索,是否能到达,抵消两个(相同)棋子
    hdu 4221 greed 注意范围 工作延期,使整个工作时间罚时最少的单个罚时最长的值
    hdu 2844 多重背包 多种硬币,每一种硬币有一点数量,看他能组成多少种钱
    uva LCDDisplay
    hdu 4218 模拟 根据一个圆点和半径画一个圆 注意半径要求
  • 原文地址:https://www.cnblogs.com/bors/p/MyBatisGenerator.html
Copyright © 2011-2022 走看看