参考链接:
https://blog.csdn.net/m0_43584016/article/details/86242756
https://blog.csdn.net/caidingnu/article/details/88411696
一、何为逆向工程?
将数据库中的表映射成代码中的实体类
二、Spring中MyBatis逆向工程操作步骤
Maven添加依赖
generatorConfig.xml配置
数据库连接配置、实体类/POJO/XML生成路径
指定数据库表
java class用于自动生成的main方法
1、Maven依赖,包括ojdbc数据库连接、mybatis支持、spring-mybatis插件这几大类。
SpringBoot对MyBatis插件的支持:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>RELEASE</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- mybatis generator 自动生成代码插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.1</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
<!-- 是否覆盖 -->
<overwrite>true</overwrite>
<!--允许移动生成的文件 -->
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</build>
2、generatorConfig.xml配置,该配置文件中配置代码生成的具体策略
<?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>
<properties resource="application.yml" />
<!-- mysql驱动的位置 这个是MySQL连接的jar包,你需要指定你自己计算机上的jar包的位置-->
<classPathEntry location="C:/Users/tengxiao.ma/.m2/repository/mysql/mysql-connector-java/5.1.46/mysql-connector-java-5.1.46.jar" />
<context id="Tables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否生成注释代时间戳 -->
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- JDBC连接 其中connectionURL后面的newtest改为你创建的数据库,紧跟在后面是数据库连接的账户和密码-->
<jdbcConnection
driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/newtest"
userId="root"
password="root">
</jdbcConnection>
<!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成实体类地址 这里需要你改动,其中targetPackage需要根据你自己创建的目录进行改动 -->
<javaModelGenerator targetPackage="com.kyrie.matx.entity" targetProject="src/main/java">
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaModelGenerator>
<!-- 生成mapper xml文件 这里不需要改动 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- 生成mapper xml对应Client 这里需要改动targetPackage,依据你自己的工程-->
<javaClientGenerator targetPackage="com.kyrie.matx.dao" targetProject="src/main/java" type="XMLMAPPER">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 配置表信息 -->
<!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample
是否生成 example类 个人觉得生成那么多的example类很繁琐,因此设置为false-->
<table schema="newtest" tableName="category"
domainObjectName="Category" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false">
</table>
<table schema="newtest" tableName="product"
domainObjectName="Product" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false">
</table>
</context>
</generatorConfiguration>
3、application.properties文件
spring.datasource.url=jdbc:mysql://192.168.100.11:3306/hehe?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&rewriteBatchedStatements=true
spring.datasource.username= root
spring.datasource.password= 123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#mybatis的配置
#mapper对应的xml文件位置
mybatis.mapper-locations=classpath:mapper/*.xml
#实体类的包
mybatis.type-aliases-package: com.kyrie.matx.entity
mybatis.config-location=classpath:mybatis-config.xml
server.port=8011
4自动生成代码:点击run----> Edit Configurations
5 添加配置