一、mybatis 逆向工程简介
逆向工程简单来说就是传统写代码一般先写对应数据库里面的实体类、数据库接口类、实现类(即xml配置文件)。然而逆向工程是先通过数据库系统自动为你生成这些文件。
二、实现步骤
1、首先创建一个springboot项目
2、在springboot中配置好数据库
3、springboot整合mybatis
yaml配置文件
server:
port: 8899
servlet:
context-path: /
spring:
datasource: #数据库
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/leave1?useUnicode=true&characterEncoding=utf8&nullCatalogMeansCurrent=true&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=UTC
username: root
password: 1127
web: #静态资源
resources:
static-locations: classpath:/static/,classpath:/templates/
devtools: #热部署
livereload:
enabled: true
jackson: #时间
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
activiti:
#1 true 没有时候创建,有呢进行更新
database-schema-update: false
db-history-used: true
history-level: full
check-process-definitions: false
mybatis:
type-aliases-package: com.chun.model
mapper-locations: classpath:com/chu/dao/*.xml
configuration:
map-underscore-to-camel-case: true
4、利用maven导入mybatis逆向工程相关依赖
<!--逆向工程-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
5.创建generatorConfig.xml文件
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="mybatis.properties" />
-->
<classPathEntry location="E:\07-maven-resp\mysql\mysql-connector-java\8.0.22\mysql-connector-java-8.0.22.jar" />
<context id="msqlTables" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
<jdbcConnection connectionURL="jdbc:mysql://localhost:3306/leave?serverTimezone=GMT%2B8"
driverClass="com.mysql.cj.jdbc.Driver" password="1127" userId="root" >
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="com.chun.model" targetProject="D:\桌面\leave\src\main\java">
<property name="enableSubPackages" value="true"/>
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="com.chun.dao" targetProject="D:\桌面\leave\src\main\resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.chun.dao" targetProject="D:\桌面\leave\src\main\java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--数据库表-->
<table tableName="user" domainObjectName="User"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false" >
<property name="useActualColumnNames" value="false"/>
</table>
<table tableName="dept_info" domainObjectName="DeptInfo"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false" >
<property name="useActualColumnNames" value="false"/>
</table>
<table tableName="leave_info" domainObjectName="LeaveInfo"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false" >
<property name="useActualColumnNames" value="false"/>
</table>
<table tableName="role_info" domainObjectName="RoleInfo"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false" >
<property name="useActualColumnNames" value="false"/>
</table>
</context>
</generatorConfiguration>
5、添加maven配置
6、运行maven
生成对应文件