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="src\main\java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--sqlMapper.xml-->
        <sqlMapGenerator targetPackage="sqlMapper"
                         targetProject="src\main\resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--Mapper-->
        <javaClientGenerator targetPackage="org.demombts.mapper"
                             targetProject="src\main\java" 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>

 

posted @ 2021-03-12 16:29  BORS  阅读(164)  评论(0编辑  收藏  举报
bors