天天看點

【IDEA】mybatis generator逆向工程生成代碼

目錄

  • 一、MyBatis 逆向工程
  • 二、使用方法
    • 1.準備資料表
    • 2.建立generatorConfig.xml
    • 3.配置generatorConfig.xml
    • 4.引入插件
    • 5.建立啟動項
  • 三.檢視結果

一、MyBatis 逆向工程

在使用IDEA搭建後端項目時,快速生成資料庫對應的 POJO 和 DAO 類還有對應的 mapper 映射檔案和mapper代理接口,提高開發效率。

【IDEA】mybatis generator逆向工程生成代碼

在【resources】下建立 generatorConfig.xml 配置檔案。

【IDEA】mybatis generator逆向工程生成代碼

配置以下内容:

1.資料庫驅動包

2.資料庫URL、使用者名、密碼

3.生成的實體模型(pojo)、XML映射檔案(sql語句)、Mapper接口的包名(targetPackage)和位置(targetProject)

4.生成目标的資料庫表名(tableName)和實體類名(domainObjectName)

<?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="C:\Users\asus\.m2\repository\mysql\mysql-connector-java\5.1.6\mysql-connector-java-5.1.6.jar"/>
    <context id="DB2Tables"  targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自動生成的注釋 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--資料庫連接配接驅動類,URL,使用者名、密碼 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/tpadmin" userId="root" password="123456">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 生成(實體)模型的包名和位置-->
        <javaModelGenerator targetPackage="com.example.dzy.pojo" targetProject="src\main\java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成XML映射檔案的包名和位置-->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 生成Mapper接口的包名和位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.dzy.mapper" targetProject="src\main\java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 要生成的表 tableName是資料庫中的表名或視圖名 domainObjectName是實體類名-->
        <table tableName="tpadmin_admin_user" domainObjectName="Admin" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        </context>
</generatorConfiguration>
           

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <!-- mybatis generator 自動生成代碼插件 -->
        <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.3.1</version>
            <configuration>
                <!-- generatorConfig.xml所在路徑 -->
                <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
                <overwrite>true</overwrite>
                <verbose>true</verbose>
            </configuration>
        </plugin>
    </plugins>
</build>