SpringBoot 3.1.7 ?集成 mybatis-generator-maven-plugin 插件,使得方便数据库字段快速同步到项目中,不用手敲,节约开发时间
在网上搜索mybatis-generator,找到Mybatis的官网地址如下
MyBatis Generator Core – Introduction to MyBatis Generator
打开看了看,了解大概的使用方法,我们使用maven的方式,也是最常用的方式:
MyBatis Generator Core – Running MyBatis Generator With Maven
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.2</version>
</plugin>
这里的包在官网中是hsqldb数据库,是我根据自己的情况找的方案,用了MySQL
<dependencies>
<!-- 数据库 Mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.version}</version>
</dependency>
</dependencies>
这段配置代码,官网也没有,也是我在网上找的
<configuration>
<!--配置文件的位置-->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
最后的pom.xml 就长这样
<plugin>
<!--Mybatis-generator插件,用于自动生成Mapper和POJO-->
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.2</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<!-- 数据库 Mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.version}</version>
</dependency>
</dependencies>
</plugin>
配置详情,参考官网:?
MyBatis Generator Core – MyBatis Generator XML Configuration File Reference
我这边按照自己的项目做了配置,下面配置很简单,就不多说了,看看应该都会了
<?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="my" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="false"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://192.168.31.111:3306/goods_center_db?characterEncoding=UTF-8"
userId="root"
password="123456"/>
<javaModelGenerator targetPackage="com.ychen.goodscenter.entity"
targetProject="src/main/java"
>
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="mapper"
targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<javaClientGenerator targetPackage="com.ychen.goodscenter.dao"
targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<table tableName="order_info_tab" domainObjectName="OrderInfo"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
双击 [mybatis-generator:generate] 运行看看效果吧!!!