1 項(xiàng)目目錄結(jié)構(gòu) (IDE: Idea)

目錄結(jié)構(gòu)
2 配置Maven插件以及相關(guān)依賴——pom.xml
<!--pom.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.vlin</groupId>
<artifactId>mybatis-generator</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<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>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
<!--視數(shù)據(jù)庫版本而定,可以選擇5.x版本的connector,對(duì)應(yīng)的driver為com.mysql.jdbc.Driver,如需使用該版本,請(qǐng)記得修改dataSource.properties中的driver項(xiàng)。-->
<!--此處版本為8.x,對(duì)應(yīng)的driver為com.mysql.cj.jdbc.Driver-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
3 配置JDBC參數(shù)——dataSource.properties
#driver版本視數(shù)據(jù)庫版本而定
jdbc.driver=com.mysql.cj.jdbc.Driver
#數(shù)據(jù)庫URL
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
#數(shù)據(jù)庫登錄用戶
jdbc.username=victor
#數(shù)據(jù)庫登錄密碼
jdbc.password=123456789
#定義初始連接數(shù)
initialSize=0
#定義最大連接數(shù)
maxActive=20
#定義最大空閑
maxIdle=20
#定義最小空閑
minIdle=1
#定義最長等待時(shí)間
maxWait=60000
4 配置生成規(guī)則——mybatisConfig.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>
<!--JDBC相關(guān)配置文件,存放JDBC連接所需要的各個(gè)參數(shù)-->
<properties resource="dataSource.properties"/>
<context id="mysqlTables" targetRuntime="MyBatis3">
<!--JDBC連接配置-->
<jdbcConnection driverClass="${jdbc.driver}"
connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"/>
<!--指定生成的類型為java類型,避免數(shù)據(jù)庫中number等類型字段 -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--自動(dòng)生成的實(shí)體的存放包路徑 -->
<javaModelGenerator targetPackage="com.vlin.entity"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--自動(dòng)生成的*Mapper.xml文件存放路徑 -->
<sqlMapGenerator targetPackage="com.vlin.dao.mappers"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--自動(dòng)生成的*Mapper.java存放路徑 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.vlin.dao.mappers" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--數(shù)據(jù)庫中對(duì)應(yīng)的表,生成的類名,以及配置自動(dòng)生成方法有哪些的參數(shù)-->
<table tableName="buyer_info" domainObjectName="BuyerInfo"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="true"
selectByExampleQueryId="false">
</table>
<!--<table tableName="teacher" domainObjectName="Tercher">-->
<!--<generatedKey column="ID" sqlStatement="select uuid_short()" identity="false"/>-->
<!--</table>-->
</context>
</generatorConfiguration>
5 運(yùn)行Mybatis Generator生成Maven插件

Mybatis Generator插件

插件運(yùn)行過程

生成的Entity以及Mapper