1.maven配置

​ 添加maven插件,在maven中配置mysql驱动,并指定配置文件的位置。

<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.2</version>
                <configuration>
                    <configurationFile>src/main/resources/mybatis/mybatis-generator-config.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>

                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.19</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

​ 配置完成后,在idea右侧栏maven中会出现mybatis-generator插件
图片说明

2.Mybatis-Generator配置文件

数据库配置文件 datasource.properties

datasource.driver-class-name=com.mysql.cj.jdbc.Driver
datasource.url=jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
datasource.username=****
datasource.password=****

mybatis-generator配置文件 mybatis-generator-config

<!DOCTYPE generatorConfiguration PUBLIC
        "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <properties resource="datasource.properties"/>
    <context id="blog" targetRuntime="MyBatis3">
        <!--<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"-->
                        <!--connectionURL="jdbc:mysql://localhost:3306/es_blog?serverTimezone=UTC"-->
                        <!--userId="root"-->
                        <!--password="123456789"/>-->
        <jdbcConnection driverClass="${datasource.driver-class-name}"
                        connectionURL="${datasource.url}"
                        userId="${datasource.username}"
                        password="${datasource.password}">
        </jdbcConnection>

        <javaModelGenerator targetPackage="com.***.entity" targetProject="src/main/java">
            <property name="enableSubPackages" value="false" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="mybatis.mappers" targetProject="src/main/resources">
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER" targetPackage="com.***.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <table tableName="article">

        </table>
    </context>
</generatorConfiguration>

具体每个标签的细节请参照 http://mybatis.org/generator/configreference/xmlconfig.html

3.启动

idea启动,点击右侧的mybatis-generator:generate选项。