環(huán)境版本
Mysql-8.0
springboot-2.1.6
jdk-1.8
1.新建項目
2.在pom文件的<build>
下的<plugins>
添加以下配置
記住mysql連接一定添加在plugins中
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<!-- 在控制臺打印執(zhí)行日志 -->
<verbose>true</verbose>
<!-- 重復(fù)生成時會覆蓋之前的文件-->
<overwrite>true</overwrite>
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
</configuration>
<!-- 數(shù)據(jù)庫連接選擇8.0以上的价说,因為用的mysql8.0-->
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
</dependencies>
</plugin>
3.新建后面需要使用到的包
4.在mysql中建立測試數(shù)據(jù)庫wg_inset
5.在resources下創(chuàng)建generatorConfig.xml
具體內(nèi)容:
<?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:起個名字 -->
<!-- targetRuntime:設(shè)置生成的文件適用于那個 mybatis 版本 -->
<context id="default" targetRuntime="MyBatis3">
<!--optional,指在創(chuàng)建class時哼绑,對注釋進(jìn)行控制-->
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自動生成的注釋 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--jdbc的數(shù)據(jù)庫連接 wg_insert 為數(shù)據(jù)庫名字-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/wg_insert?useUnicode=true&characeterEncoding=utf-8&serverTimezone=UTC" userId="root"
password="123456"></jdbcConnection>
<!--非必須旁涤,類型處理器,在數(shù)據(jù)庫類型和java類型之間的轉(zhuǎn)換控制-->
<javaTypeResolver>
<!-- 默認(rèn)情況下數(shù)據(jù)庫中的 decimal洲敢,bigInt 在 Java 對應(yīng)是 sql 下的 BigDecimal 類 -->
<!-- 不是 double 和 long 類型 -->
<!-- 使用常用的基本類型代替 sql 包下的引用類型 -->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- targetPackage:生成的實(shí)體類所在的包 -->
<!-- targetProject:生成的實(shí)體類所在的硬盤位置 -->
<javaModelGenerator targetPackage="com.wglvzyx.mybatisredis.entity"
targetProject="src/main/java">
<!-- 是否允許子包 -->
<property name="enableSubPackages" value="false"/>
<!-- 是否對modal添加構(gòu)造函數(shù) -->
<property name="constructorBased" value="true"/>
<!-- 是否清理從數(shù)據(jù)庫中查詢出的字符串左右兩邊的空白字符 -->
<property name="trimStrings" value="true"/>
<!-- 建立modal對象是否不可改變 即生成的modal對象不會有setter方法漫玄,只有構(gòu)造方法 -->
<property name="immutable" value="false"/>
</javaModelGenerator>
<!-- targetPackage 和 targetProject:生成的 mapper 文件的包和位置 -->
<sqlMapGenerator targetPackage="mapper"
targetProject="src/main/resources">
<!-- 針對數(shù)據(jù)庫的一個配置,是否把 schema 作為字包名 -->
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- targetPackage 和 targetProject:生成的 interface 文件的包和位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.wglvzyx.mybatisredis.dao" targetProject="src/main/java">
<!-- 針對 oracle 數(shù)據(jù)庫的一個配置压彭,是否把 schema 作為字包名 -->
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- tableName是數(shù)據(jù)庫中的表名称近,domainObjectName是生成的JAVA模型名,后面的參數(shù)不用改哮塞,要生成更多的表就在下面繼續(xù)加table標(biāo)簽 -->
<table tableName="student" domainObjectName="Student"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>
<!-- tableName是數(shù)據(jù)庫中的表名刨秆,domainObjectName是生成的JAVA模型名,后面的參數(shù)不用改忆畅,要生成更多的表就在下面繼續(xù)加table標(biāo)簽 -->
<table tableName="post" domainObjectName="Post"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
6.跑起來看是否成功
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.562 s
[INFO] Finished at: 2019-07-29T11:55:44+08:00
[INFO] Final Memory: 17M/187M
[INFO] ------------------------------------------------------------------------
Process finished with exit code 0
控制臺出現(xiàn)這個衡未。表示成功了
查看生成的文件