1、创建一个新的MAVEN项目,例如:mybatis-generator
2、修改pom.xml文件,按照如下设置:
<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.wangphGit</groupId>
<artifactId>mybatis-generator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatis-generator</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
<version>3.3</version>
</plugin>
<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.13</version>
</dependency>
</dependencies>
<configuration>
<!--配置文件的路径-->
<configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
3、resources目录下创建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="mysql" targetRuntime="MyBatis3">
<!-- 配置则生效:javaModelGenerator模型是否继承Serializable,是否重写haspCode方法,是否重写equals方法,是否重写toString方法 -->
<!-- <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin> -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
<commentGenerator>
<!-- 这个元素用来去除指定生成的注释中是否包含生成的日期 false:表示保护 -->
<!-- 如果生成日期,会造成即使修改一个字段,整个实体类所有属性都会发生变化,不利于版本控制,所以设置为true -->
<property name="suppressDate" value="true" />
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
<!-- 生成的Java文件的编码 -->
<property name="javaFileEncoding" value="UTF-8"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 ,严格按照如下配置,否则会报错-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/数据库名?characterEncoding=utf-8&serverTimezone=UTC" userId="数据库名" password="密码">
</jdbcConnection>
<javaTypeResolver>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成模型的包名和位置 -->
<javaModelGenerator targetPackage="com.dto"
targetProject="target">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成映射文件的包名和位置 -->
<sqlMapGenerator targetPackage="com.mapping"
targetProject="target">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成DAO的包名和位置 -->
<!-- dao生成器,type 指定生成dao类的模板,可选择IBATIS、SPRING、GENERIC-CI、GENERIC-SI;
implementationPackage dao实现类的包名 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.dao" implementationPackage="com.dao" targetProject="target">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 生成所需数据库表,及生成后的类名,可配置多个-->
<!-- 表名属性映射 tableName为表名,可使用SQL通配符%和_, domainObjectName为对应类名,如不写则默认和表名相同 -->
<table tableName="t_dep" domainObjectName="dep"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table tableName="t_user" domainObjectName="user"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
4、选中项目->>右键->>Run As->>Maven build->>Goals:输入: mybatis-generator:generate->>点击Run按钮,控制台提示success,表示已经完成。然后再选中项目->>右键->>Run As->>Refresh,或者直接按F5刷新,即可看到生成的文件