title: MyBatis Generator Maven插件使用
tags: MyBatis,Generator,Maven插件
Maven插件配置
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<configuration>
<verbose>true</verbose><!--运行时显示详细的信息 -->
<overwrite>true</overwrite><!-- 每次覆盖自动生成的文件 -->
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency><!-- 配置连接依赖后,generatorConfig.xml不需要再配置本地驱动 -->
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.9-rc</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
配置generatorConfig.xml
该文件放置在src/main/resources下,该路径是默认配置路径,如果要放置在其他位置,请在上面插件configuration下面进行配置,以下列出了基本的配置信息,需要更深入的可以查官方文档。
<?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="mysqlTables" targetRuntime="MyBatis3">
<!-- 数据库连接的信息:驱动类、连接地址、用户名、密码,MySql5.x的 driverClass=com.mysql.jdbc.Driver -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306"
userId="root"
password="password">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,
为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成model类的位置 -->
<javaModelGenerator targetPackage="cn.demo.model" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="cn.demo.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="cn.demo.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<table schema="mySchema" tableName="myTable">
<generatedKey column="ID" sqlStatement="MySql" identity="true" />
</table>
</context>
</generatorConfiguration>
运行
可以使用maven命令运行,也可以通过IDE自带工具运行
mvn mybatis-generator:generate