MyBatis Generator生成数据库模型实现示例
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的插件执行生成操作
安装MyBatis Generator插件:
打开 generatorConfig.xml 文件,右键点击文件,选择「Run MyBatis Generator」。
一次生成多个表模型
<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模型类文件,更多关于MyBatis Generator数据库模型的资料请关注脚本之家其它相关文章!
相关文章
idea 安装 Mybatis 开发帮助插件 MyBatisCodeHelper-Pro 插件破解版的方法
MyBatisCodeHelper-Pro 插件可以帮助我们快速的开发 mybatis,这篇文章给大家介绍idea 安装 Mybatis 开发帮助插件 MyBatisCodeHelper-Pro 插件破解版的相关知识,感兴趣的朋友跟随小编一起看看吧2020-09-09
SpringCloud笔记(Hoxton)Netflix之Ribbon负载均衡示例代码
这篇文章主要介绍了SpringCloud笔记HoxtonNetflix之Ribbon负载均衡,Ribbon是管理HTTP和TCP服务客户端的负载均衡器,Ribbon具有一系列带有名称的客户端(Named Client),对SpringCloud Ribbon负载均衡相关知识感兴趣的朋友一起看看吧2022-06-06
Springboot+WebSocket+Netty实现在线聊天/群聊系统
这篇文章主要实现在好友添加、建群、聊天对话、群聊功能,使用Java作为后端语言进行支持,界面友好,开发简单,文章中有详细的代码示例供大家参考,需要的朋友可以参考下2023-08-08


最新评论