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

  • 相关阅读:
    Programming Contest Ranking(题解)
    Alphabet Cookies
    hpu 1267 Cafeteria (01背包)
    Triangles 正多边形分割锐角三角形
    ACdream 1067:Triangles
    hdu 1253 胜利大逃亡 (代码详解)解题报告
    最短路
    POJ- 1511 Invitation Cards
    E
    HDU
  • 原文地址:https://www.cnblogs.com/bors/p/MyBatisGenerator.html
Copyright © 2011-2022 走看看