1. 程式人生 > 其它 >IDEA+Maven+Java程式碼測試的簡單MyBatis逆向工程

IDEA+Maven+Java程式碼測試的簡單MyBatis逆向工程

簡單MyBatis逆向工程步驟

新增maven依賴

<dependencies>
	<dependency>
		<groupId>org.mybatis.generator</groupId>
		<artifactId>mybatis-generator</artifactId>
		<version>1.3.5</version>
	</dependency>
	<dependency>
		<groupId>org.mybatis.generator</groupId>
		<artifactId>mybatis-generator-maven-plugin</artifactId>
		<version>1.3.5</version>
	</dependency>
	<dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.3</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.20</version>
        </dependency>
</dependencies>

配置MBG.xml

因為作者使用相對路徑時會報找不到xml檔案異常,所以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>
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <!--jdbcConnection:指導連線到哪個資料庫-->
        <jdbcConnection
            driverClass="com.mysql.cj.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/資料庫名?serverTimezone=UTC"
            userId="資料庫使用者名稱"
            password="資料庫使用者密碼">
        </jdbcConnection>

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

        <!--javaModelGenerator:生成pojo
        targetPackage:生成的pojo放在哪個包
        targetProject:放在哪個工程下
        -->
        <javaModelGenerator targetPackage="com.atguigu.bean"
                            targetProject="E:\8、MyBatis_mbg\src\main\java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!--sqlMapGenerator:sql對映檔案生成器;指定xml生成的地方-->
        <sqlMapGenerator targetPackage="com.atguigu.dao"
                         targetProject="E:\8、MyBatis_mbg\src\main\resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!--javaClientGenerator:dao介面生成的地方-->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.atguigu.dao"
                             targetProject="E:\8、MyBatis_mbg\src\main\java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!--table:指定要逆向生成哪個資料表
        tableName="t_teacher":表名
        domainObjectName:這個表對應的物件名
        -->
        <table tableName="t_cat" domainObjectName="Cat" ></table>
        <table tableName="t_employee" domainObjectName="Employee" ></table>
        <table tableName="t_teacher" domainObjectName="Teacher" ></table>

    </context>
</generatorConfiguration>

寫Java測試類

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MBGTest {

    public static void main(String[] args) throws Exception {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File("E:\\8、MyBatis_mbg\\src\\main\\resources\\mbg.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        //程式碼生成
        myBatisGenerator.generate(null);
    }
}

最後執行即可生成dao介面、xml實現檔案、bean類

Maven最終結果如下