此次使用的生成方式是使用mybatis插件,总共两步
步骤1:在pom文件加入依赖
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<phase>package</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<!--允许移动生成的文件 -->
<verbose>true</verbose>
<!-- 是否覆盖 -->
<overwrite>false</overwrite>
<!-- 自动生成的配置 -->
<configurationFile>src/main/resources/mybatis-generator.xml</configurationFile>
</configuration>
</plugin>
</plugins>
</build>
步骤2:创建mybatis-generator.xml文件,主要就是设置生成文件的目录设置,数据源设置,以及其他的一些设置等。注意:创建文件的地址需要和依赖里面配置的configurationFile标签的一致。
<?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="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库连接 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/spring?serverTimezone=UTC"
userId="root"
password="Ma310320..">
<property name="useInformationSchema" value="true"/>
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="true" />
</javaTypeResolver>
<!--实体类生成目录配置-->
<javaModelGenerator targetPackage="com.mzc.dao"
targetProject="src/main/java">
<!-- 是否允许子包,即targetPackage.schemaName.tableName -->
<property name="enableSubPackages" value="true"/>
</javaModelGenerator>
<!--mapper.xml生成目录-->
<sqlMapGenerator targetPackage="com.mzc.dao" targetProject="src/main/java"/>
<!--mapper接口生成目录-->
<javaClientGenerator targetPackage="com.study.wzf.myuserprovider.dal.persistence"
targetProject="src/main/java"
type="XMLMAPPER"/>
<table tableName="books" domainObjectName="Books"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
然后刷新maven,会发现maven的plugins里面多了一个mybatis-generator,鼠标右键点击子标签里面的generator,然后点击run maven build
控制台里面打印出build seccess就已经生成成功了