1. 程式人生 > 實用技巧 >Mybatis提供的逆向工程

Mybatis提供的逆向工程

首先需要了解一個軟體:PowerDesigner——可以用來建立資料庫表的指令碼檔案

如果資料庫已經構建成功了,那麼在專案中分為三步走的方式來構建逆向工程。

第一步:pom檔案新增外掛

<plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>
1.3.6</version> <configuration> <!--配置檔案的位置--> <configurationFile>GeneratorMapper.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite
> </configuration> </plugin>

第二步:在專案中和pom.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> <!--指定連線資料庫的JDBC驅動包所在的位置--> <classPathEntry location="D:\soft\study\tools\apache-maven-3.6.3\repository\mysql\mysql-connector-java\8.0.21\mysql-connector-java-8.0.21.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <!-- 是否去除自動生成的註釋 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!-- Mysql資料庫連線的資訊:驅動類、連線地址、使用者名稱、密碼 --> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/toutiao?serverTimezone=Asia/Shanghai" userId="root" password="123456"> <property name="nullCatalogMeansCurrent" value="true" /> </jdbcConnection> <!-- Oracle資料庫 <jdbcConnection driverClass="oracle.jdbc.OracleDriver" connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg" userId="yycg" password="yycg"> </jdbcConnection> --> <!-- 預設為false,把JDBC DECIMAL 和NUMERIC型別解析為Integer,為true時 把JDBC DECIMAL 和NUMERIC型別解析為java.math.BigDecimal --> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetProject:生成POJO類的位置 ,targetPackage:工程下面的包名--> <javaModelGenerator targetPackage="com.kunkun.springboot.model" targetProject="src/main/java"> <!-- enableSubPackages:是否讓schema作為包的字尾 --> <property name="enableSubPackages" value="false" /> <!-- 從資料庫返回的值被清理前後的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- targetProject:mapper對映檔案生成的位置 .xml檔案--> <sqlMapGenerator targetPackage="com.kunkun.springboot.mapper" targetProject="src/main/java"> <!-- enableSubPackages:是否讓schema作為包的字尾 --> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- targetProject:mapper介面生成的的位置 --> <!-- targetProject也可以指定絕對路徑--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.kunkun.springboot.mapper" targetProject="src/main/java"> <!-- enableSubPackages:是否讓schema作為包的字尾 --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 資料庫表名,對應的java中model的名字 --> <table tableName="user" domainObjectName="UserDO" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/> <table tableName="news" domainObjectName="NewsDO" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <!-- 有些表的欄位需要指定java型別 <table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" > <property name="useActualColumnNames" value="true"/> <generatedKey column="ID" sqlStatement="DB2" identity="true" /> <columnOverride column="DATE_FIELD" property="startDate" /> <ignoreColumn column="FRED" /> <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> </table> --> </context> </generatorConfiguration>

這裡需要注意,如果mysql是8.0以上的版本需要使用帶cj的mysql驅動,並且需要注意在資料庫連線上加入serverTimezone時區。

第三步:使用外掛