欢迎光临
感受代码之美

Maven配置之MyBatis Generator Maven Plugin(三步学会使用MyBatis Generator生成代码)

学习使用Maven Mybatis插件MyBatis Generator Maven Plugin自动生成实体类、Example类等详细配置。

开发环境:IntelliJ IDEA 2018.3

1.添加插件依赖

插件依赖如下:

<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.7</version>
    <configuration>
        <!--mybatis generator插件配置文件位置,默认值${basedir}/src/main/resources/generatorConfig.xml-->
        <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
        <overwrite>true</overwrite>
        <verbose>true</verbose>
    </configuration>
</plugin>

完整pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.itfeichai</groupId>
    <artifactId>springboot_2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>springboot_2</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <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>
                <configuration>
                    <!--mybatis generator插件配置文件位置,默认值${basedir}/src/main/resources/generatorConfig.xml-->
                    <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

2.配置generatorConfig.xml文件

完整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>
    <!--参数配置-->
    <properties resource="generator/generator.properties"/>
    <!--驱动jar-->
    <classPathEntry location="${classPathEntry}"/>

    <context id="OracleTables" targetRuntime="MyBatis3">
        <jdbcConnection driverClass="${driverClass}" connectionURL="${connectionURL}" userId="${userId}" password="${password}">
        </jdbcConnection>

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

        <javaModelGenerator targetPackage="${modelTargetPackage}" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="${sqlMapTargetPackage}" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER" targetPackage="${javaClientTargetPackage}" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <table tableName="${tableName}" domainObjectName="${domainObjectName}">
        </table>

    </context>
</generatorConfiguration>

这里将相关参数在generator.properties中配置:

classPathEntry=F:/maven_repository/com/oracle/ojdbc7/12/ojdbc7-12.jar
driverClass=oracle.jdbc.driver.OracleDriver
connectionURL=jdbc:oracle:thin:@10.110.34.133:1521:xe
userId=lgzr
password=lgzr
# 实体类
modelTargetPackage=com.domain.srclass
# mapping文件
sqlMapTargetPackage=mapping/srclass
# mapper类
javaClientTargetPackage=com.dao.srclass
# 哪个用户的表
schema=lgzr
tableName=SR_CLASS
domainObjectName=SrClass

3.运行插件生成代码

在IDEA右侧双击运行插件,如下图:

run-mybatis-generator-plugin-with-maven-1.jpg

然后生成的代码如下图:

run-mybatis-generator-plugin-with-maven-2.jpg

  1. Running MyBatis Generator With Maven
  2. 官方配置文件示例
  3. 使用MyBatis Generator自动生成代码
转载请注明来源:四个空格 » Maven配置之MyBatis Generator Maven Plugin(三步学会使用MyBatis Generator生成代码)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址