• MyBatis Generator生成数据库模型步骤详解
  • 发布于 2个月前
  • 184 热度
    0 评论
MyBatis Generator以根据数据库表结构生成Java模型类、Mapper接口和对应的XML映射文件。以下是使用MyBatis Generator的一般步骤:

1.添加MyBatis Generator依赖:在项目的pom.xml(如果是Maven项目)或build.gradle(如果是Gradle项目)中添加MyBatis Generator的依赖。
Maven的例子:
<build>
    <plugins>
        <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.4.0</version> <!-- 替换为最新版本 -->
            <configuration>
                <!-- 配置文件的路径,后面会创建一个generatorConfig.xml文件 -->
                <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                <overwrite>true</overwrite>
            </configuration>
            <dependencies>
                <!-- 驱动程序依赖 -->
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>8.0.23</version> <!-- 替换为你使用的数据库版本 -->
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>
2.创建MyBatis Generator配置文件:在项目的src/main/resources目录下创建一个generatorConfig.xml文件,配置数据库连接信息、生成规则等。以下是一个简单的例子:
<!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">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/your_database"
                        userId="your_username"
                        password="your_password">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <javaModelGenerator targetPackage="com.example.model" targetProject="src/main/java"/>
        <sqlMapGenerator targetPackage="com.example.mapper" targetProject="src/main/resources"/>
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mapper" targetProject="src/main/java"/>
        <table tableName="your_table_name"/>
    </context>
</generatorConfiguration>

请替换上述配置中的your_database、your_username、your_password、com.example.model、com.example.mapper和your_table_name为你自己的数据库连接信息和项目包结构。


3.运行MyBatis Generator:你可以通过Maven命令或IDE插件来运行MyBatis Generator。
如果使用Maven,在命令行中运行:
mvn mybatis-generator:generate
通过IDE的插件执行生成操作
3.1 安装MyBatis Generator插件:
3.2 打开 generatorConfig.xml 文件,右键点击文件,选择「Run MyBatis Generator」。

4.一次生成多个表模型
<generatorConfiguration>
    <!-- Other configurations -->

    <context id="PGTables" targetRuntime="MyBatis3">
        <!-- Other context configurations -->

        <table tableName="table1"/>
        <table tableName="table2"/>
        <table tableName="table3"/>
        <!-- Add more tables as needed -->
    </context>
</generatorConfiguration>
以上步骤中,MyBatis Generator将会根据配置文件中的信息连接到数据库,读取表结构,然后生成对应的Java模型类文件、Mapper接口文件和XML映射文件。这些文件将会被生成到指定的目录中,根据你的配置,你可以在src/main/java目录下找到生成的Java模型类文件。
用户评论