介绍

MyBatis Generator(http://www.mybatis.org/generator/index.html) 是MyBatis的一款代码生成器,它可以针对数据库表自动生成DTO、Mapper接口及相应的xml文件。

操作步骤

1.在pom.xml中配置plugin:

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!-- 指定MyBatis Generartor配置文件 -->
                   <configurationFile>src/main/resources/mybatis/generatorConfig.xml</configurationFile>
                </configuration>
            </plugin>

        </plugins>
    </build>

2.在pom.xml中添加依赖:

<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.3.2</version>
</dependency>

3.添加配置文件generatorConfig.xml,配置文件的内容可参考官方文档(http://www.mybatis.org/generator/configreference/xmlconfig.html)

<?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>
    <!-- 数据库驱动,如果是MySQL,此配置换为MySQL驱动类即可 -->
    <classPathEntry location="D:\Data\maven\repository\com\microsoft\sqlserver\sqljdbc4\4.0\sqljdbc4-4.0.jar" />

    <context id="DB2Tables" targetRuntime="MyBatis3">

        <!-- 数据库连接地址 -->
        <jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"
                        connectionURL="jdbc:sqlserver://127.0.0.1:1433;Databasename=Demo"
                        userId="sa"
                        password="1">
        </jdbcConnection>

        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- 生成实体类配置 -->
        <javaModelGenerator targetPackage="com.lanweihong.demo.dto" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- 生成mapper映射文件xml配置 -->
        <sqlMapGenerator targetPackage="mapper"  targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- 生成dao配置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.lanweihong.demo.dao"  targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- 生成对应表及类名,和数据库对应 -->
        <table tableName="UserInfo" domainObjectName="UserInfoDTO"></table>

    </context>
</generatorConfiguration>

4.添加以上配置完成后,单击IDEA菜单栏的Run选项,选择Edit Configurations...

https://public-image-lwh.oss-cn-shenzhen.aliyuncs.com/mybatis-generator/run-edit-configurations.png

5.单击左上角的新增按钮,新增Maven配置

https://public-image-lwh.oss-cn-shenzhen.aliyuncs.com/mybatis-generator/add-maven-configuration.png

6.配置可参考以下,配置完成后单击运行按钮即可自动生成。

https://public-image-lwh.oss-cn-shenzhen.aliyuncs.com/mybatis-generator/edit-maven-configuration.png

命令输出如下则表明成功自动生成对应的实体类和Mapper及映射xml。

"C:\Program Files\Java\jdk1.8.0_181\bin\java.exe" -Dmaven.multiModuleProjectDirectory=D:\Data\Code\Java\venue-ms "-Dmaven.home=C:\Program Files\apache-maven-3.6.0" "-Dclassworlds.conf=C:\Program Files\apache-maven-3.6.0\bin\m2.conf" -javaagent:C:\Users\lanweihong\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\191.7479.19\lib\idea_rt.jar=2284:C:\Users\lanweihong\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\191.7479.19\bin -Dfile.encoding=UTF-8 -classpath "C:\Program Files\apache-maven-3.6.0\boot\plexus-classworlds-2.5.2.jar" org.codehaus.classworlds.Launcher -Didea.version2019.1.3 mybatis-generator:generate
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------------< com.lanweihong:demo >-------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO] 
[INFO] --- mybatis-generator-maven-plugin:1.3.7:generate (default-cli) @ demo ---
[INFO] Connecting to the Database
[INFO] Introspecting table UserInfo
[INFO] Generating Example class for table UserInfo
[INFO] Generating Record class for table UserInfo
[INFO] Generating Mapper Interface for table UserInfo
[INFO] Generating SQL Map for table UserInfo
[INFO] Saving file UserInfoDTOMapper.xml
[INFO] Saving file UserInfoDTOExample.java
[INFO] Saving file UserInfoDTO.java
[INFO] Saving file UserInfoDTOMapper.java
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.880 s
[INFO] Finished at: 2019-03-06T22:40:52+08:00
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0
文章目录