Thiết lập môi trường và cấu hình phụ thuộc
Để tăng tốc độ phát triển backend Java, việc tự động hóa quá trình tạo các lớp mô hình (POJO), interface DAO và file mapping XML là rất cần thiết. MyBatis Generator (MBG) là công cụ tiêu chuẩn để thực hiện điều này trong hệ sinh thái Maven. Dưới đây là quy trình cấu hình chi tiết.
Đầu tiên, bạn cần khai báo plugin mybatis-generator-maven-plugin trong file pom.xml của dự án. Hãy đảm bảo phiên bản plugin và các thư viện liên quan được cập nhật để tương thích với các chuẩn mới.
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.demo</groupId>
<artifactId>mybatis-auto-gen</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.1</version>
<configuration>
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Cấu hình file sinh mã nguồn
Tiếp theo, tạo file cấu hình chi tiết cho MBG. File này thường được đặt tại src/main/resources/generatorConfig.xml. Nội dung file sẽ định nghĩa thông tin kết nối cơ sở dữ liệu, gói mã nguồn đầu ra và các bảng cần sinh code.
Lưu ý thay đổi đường dẫn targetProject phù hợp với cấu trúc thư mục thực tế của bạn và cập nhật thông tin xác thực database.
<?xml version="1.0" encoding="UTF-8"?>
<generatorConfiguration>
<context id="MySQLContext" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
<property name="suppressDate" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/demo_db?useSSL=false&serverTimezone=UTC"
userId="admin"
password="secure_password">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="com.example.demo.model"
targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="mappers"
targetProject="src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.example.demo.dao"
targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="users" domainObjectName="User"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false" />
<table tableName="products" domainObjectName="Product"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false" />
</context>
</generatorConfiguration>
Thực thi lệnh sinh code
Sau khi hoàn tất các bước cấu hình, mở terminal hoặc command prompt tại thư mục gốc của dự án (nơi chứa file pom.xml). Đảm bảo môi trường Maven đã được cài đặt và biến môi trường đã được cấu hình chính xác.
Thực thi lệnh sau để kích hoạt plugin và bắt đầu quá trình tạo file:
mvn mybatis-generator:generate
Khi lệnh chạy thành công, kiểm tra lại thư mục dự án. Bạn sẽ thấy các lớp Java và file XML tương ứng đã xuất hiện trong các gói đã định nghĩa trước đó. Nếu có thay đổi về cấu trúc bảng, chỉ cần chạy lại lệnh này để cập nhật mã nguồn.