首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

MyBatis 逆向工程完整指南:從資料庫自動生成 POJO、Mapper、XML

     編輯:Java 相關知識

前言

MyBatis 逆向工程可依據資料庫結構自動生成實體類、Mapper 介面、XML 映射檔,大幅加速開發。本文以 mybatis-generator-core-1.3.2.jar 搭配 MySQL 為例,示範完整設定與執行步驟。

環境準備

  1. 下載 mybatis-generator-core-1.3.2.jar
  2. 下載 MySQL 驅動 mysql-connector-java-5.1.13-bin.jar(或對應版本)
  3. 建立工作目錄結構:
    project-root/
    ├── mybatis-generator-core-1.3.2.jar
    ├── mysql-connector-java-5.1.13-bin.jar
    ├── generatorConfig.xml
    ├── run.bat
    └── src/          (輸出目錄,預先建立空資料夾)
    

核心設定檔:generatorConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC
  "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "https://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
  <!-- JDBC 驅動位置 -->
  <classPathEntry location="mysql-connector-java-5.1.13-bin.jar"/>

  <context id="DB2Tables" targetRuntime="MyBatis3">
    <commentGenerator>
      <property name="suppressDate" value="true"/>
      <property name="suppressAllComments" value="true"/>
    </commentGenerator>

    <!-- 資料庫連線 -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://localhost:3306/your_db"
        userId="root" password="root">
    </jdbcConnection>

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

    <!-- 實體類輸出 -->
    <javaModelGenerator targetPackage="com.example.model" targetProject="src">
      <property name="enableSubPackages" value="true"/>
      <property name="trimStrings" value="true"/>
    </javaModelGenerator>

    <!-- XML 映射檔輸出 -->
    <sqlMapGenerator targetPackage="com.example.mapping" targetProject="src">
      <property name="enableSubPackages" value="true"/>
    </sqlMapGenerator>

    <!-- Mapper 介面輸出 -->
    <javaClientGenerator type="XMLMAPPER"
        targetPackage="com.example.mapping" targetProject="src">
      <property name="enableSubPackages" value="true"/>
    </javaClientGenerator>

    <!-- 指定要生成的表 -->
    <table tableName="user" domainObjectName="User"
        enableCountByExample="false" enableUpdateByExample="false"
        enableDeleteByExample="false" enableSelectByExample="false"
        selectByExampleQueryId="false">
    </table>
    <!-- 若要全部表,tableName="%" 並移除 domainObjectName -->
  </context>
</generatorConfiguration>

執行腳本:run.bat

java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
pause

執行與驗證

  1. 雙擊 run.bat 執行
  2. 觀察 Console 輸出是否有錯誤
  3. 查看 src/ 目錄下是否生成對應套件結構與檔案

常見問題與技巧

  • JDBC URL 編碼:建議加上 ?useUnicode=true&characterEncoding=UTF-8 避免中文亂碼
  • 多表生成:可在 <context> 內重複 <table> 元素,或使用 tableName="%" 一次生成所有表(需移除 domainObjectName)
  • 自訂模板:若需調整生成代碼風格(如加上 Lombok 註解、Swagger 註解),可修改 Generator 核心 Jar 內的 Velocity 模板並重新打包
  • Maven 整合:實務專案建議改用 mybatis-generator-maven-plugin 整合至建置流程,避免手動執行

小結

MyBatis Generator 讓「資料庫優先」開發模式落地更輕鬆,配置一次即可重複使用。掌握 generatorConfig.xml 關鍵節點設定,即可依專案需求彈性產出代碼,搭配 Maven Plugin 更能納入 CI/CD 流程。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved