1 项目目录结构 (IDE: Idea)
目录结构
2 配置Maven插件以及相关依赖——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>
<!--视数据库版本而定,可以选择5.x版本的connector,对应的driver为com.mysql.jdbc.Driver,如需使用该版本,请记得修改dataSource.properties中的driver项。-->
<!--此处版本为8.x,对应的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参数——dataSource.properties
#driver版本视数据库版本而定
jdbc.driver=com.mysql.cj.jdbc.Driver
#数据库URL
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
#数据库登录用户
jdbc.username=victor
#数据库登录密码
jdbc.password=123456789
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000
4 配置生成规则——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相关配置文件,存放JDBC连接所需要的各个参数-->
<properties resource="dataSource.properties"/>
<context id="mysqlTables" targetRuntime="MyBatis3">
<!--JDBC连接配置-->
<jdbcConnection driverClass="${jdbc.driver}"
connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"/>
<!--指定生成的类型为java类型,避免数据库中number等类型字段 -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--自动生成的实体的存放包路径 -->
<javaModelGenerator targetPackage="com.vlin.entity"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--自动生成的*Mapper.xml文件存放路径 -->
<sqlMapGenerator targetPackage="com.vlin.dao.mappers"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--自动生成的*Mapper.java存放路径 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.vlin.dao.mappers" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--数据库中对应的表,生成的类名,以及配置自动生成方法有哪些的参数-->
<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 运行Mybatis Generator生成Maven插件
Mybatis Generator插件
插件运行过程
生成的Entity以及Mapper
网友评论