逆向工程就是通过相应插件由MyBatis数据库表自动生成一些文件。如mapper.java、mapper.xml、pojo…mybatis需要编写sql语句,通过mybatis官方提供逆向工程提高工作效率。
1.添加依赖
进入maven管网搜索依赖https://mvnrepository.com/搜索 "mybatis-generator-maven-plugin”
根据自己想要的版本引用
2.引用
3.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>
<context id="mysqlTables" targetRuntime="MyBatis3">
<property name="javaFileEncoding" value="UTF-8" />
<property name="addColumnAnnotation" value="false" />
<!-- 生成带有分页方法的插件-->
<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin>
<!--实现序列化-->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
<!--模型生成equals和hashcode方法-->
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
<!--XXXExample类中生成大小写敏感的LIKE方法-->
<plugin type="org.mybatis.generator.plugins.CaseInsensitiveLikePlugin" />
<!--创建一个toString方法-->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
<commentGenerator>
<property name="suppressDate" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/表名?characterEncoding=utf8"
userId="root" password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="com.项目路径.entity"
targetProject="${javaModelGenerator.targetProject}">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="mapper"
targetProject="${sqlMapGenerator.targetProject}">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.项目路径.mapper" targetProject="${javaClientGenerator.targetProject}">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!--以下为各类表格-->
<table tableName="customer" mapperName="CustomerMapper" domainObjectName="Customer" >
<generatedKey column="id" sqlStatement="MySql" identity="true" />
</table>
</context>
</generatorConfiguration>
4.pow添加配置项
<properties>
<java.version>1.8</java.version>
<javaModelGenerator.targetProject>${basedir}/src/main/java</javaModelGenerator.targetProject>
<sqlMapGenerator.targetProject>${basedir}/src/main/resources</sqlMapGenerator.targetProject>
<javaClientGenerator.targetProject>${basedir}/src/main/java</javaClientGenerator.targetProject>
<mybatis.generator.configurationFile>${basedir}/src/main/resources/generatorConfig.xml</mybatis.generator.configurationFile>
<mybatis.generator.overwrite>true</mybatis.generator.overwrite>
<mybatis.generator.version>1.3.5</mybatis.generator.version>
</properties>
<build>
<resources>
<resource>
<directory>${basedir}/src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.html</include>
<include>**/*.jpg</include>
<include>**/*.png</include>
<include>**/*.svg</include>
<include>**/*.css</include>
<include>**/*.js</include>
</includes>
</resource>
<!--项目打包时引入导入外部的jar包-->
<resource>
<directory>${basedir}/src/main/resources</directory>
<targetPath>BOOT-INF/jars/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
<!-- 需要生成model时打开,运行后提交model,并还原注释 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>${mybatis.generator.version}</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<phase>generate-sources</phase>
</execution>
</executions>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<extdirs>${project.basedir}\src\main\resources\jars</extdirs>
</compilerArguments>
</configuration>
</plugin>
</plugins>
</build>