首先 新建一个maven项目
配置pom.xml文件,在pom.xml文件里添加:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>
<!-- 该路径就是我们创建generatorConfig.xml文件的全路径,千万别配错了 -->
src/main/resources/generator/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>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
</build>
················································
然后写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>
<classPathEntry location="D:\mavenrepo\mysql\mysql-connector-java\5.1.36\mysql-connector-java-5.1.36.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" /><!-- 是否取消注释 -->
<property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳 -->
</commentGenerator>
<!-- 数据库连接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/maven" userId="root"
password="root">
</jdbcConnection>
<!-- 只有一个属于forceBigDecimals,默认false。 如果字段精确超过0,生成BigDecimal 如果字段精确是0,总长度10-18生成Long;如果字段精确是0,
总长5-9生成Integer; 如果字段精确是0,总长小于5生成Short; 如果forceBigDecimals为true,统一生成BigDecimal -->
<javaTypeResolver>
<!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--生成Model.java文件 -->
<javaModelGenerator targetPackage="cn.chery.springboot.model"
targetProject="E:\workspace\SpringbootTest\src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 是否针对string类型的字段在set的时候进行trim调用 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成Mapper.xml文件 -->
<sqlMapGenerator targetPackage="mappering"
targetProject="E:\workspace\SpringbootTest\src\main\resources">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成Mapper.java文件,即dao层 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="cn.chery.springboot.mapper" targetProject="E:\workspace\SpringbootTest\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 待生成的数据库中的表名,生成一个表对应的Java和xml文件就需要配置一段 -->
<table tableName="user_t" domainObjectName="User_t"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
·························································
目录结构如下:
····························································
然后到 项目根目录下 ;
打开cmd窗口,到项目跟目录下:执行mvn mybatis-generator:generate命令(前提得配置了maven环境变量)
等执行完,刷新项目 就生成了向对应的 model,mapper和mappering.