官网:http://mybatis.github.io/generator/index.html
项目目录结构
添加Maven插件(pom.xml)
<project ...>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
</plugin>
...
</plugins>
...
</build>
...
</project>
配置Maven执行命令
mybatis-generator:generate
** 右键 --> Run As --> Run Configurations...
**
配置MyBatis GeneratorXML
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>
<!-- 引入配置文件 -->
<properties resource="init.properties" />
<!-- JDBC驱动 -->
<classPathEntry location="${class_path}" />
<context id="Tables" targetRuntime="MyBatis3">
<!-- 注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" /><!-- 是否取消注释 -->
<property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳 -->
</commentGenerator>
<!-- JDBC连接 -->
<jdbcConnection driverClass="${jdbc_driver}"
connectionURL="${jdbc_url}/${schema}" userId="${jdbc_user}" password="${jdbc_password}">
</jdbcConnection>
<!-- 类型转换 -->
<javaTypeResolver>
<!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 生成实体类地址 -->
<javaModelGenerator targetPackage="model"
targetProject="${target_project}">
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成mapxml文件 -->
<sqlMapGenerator targetPackage="xml" targetProject="${target_project}">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- 生成mapxml对应client-->
<javaClientGenerator type="XMLMAPPER"
targetPackage="dao" targetProject="${target_project}">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 配置表信息 -->
<!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample
是否生成 example类 -->
<table schema="${schema}" tableName="${table_name}"
domainObjectName="${domain_object_name}" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false" />
</context>
</generatorConfiguration>
init.properties
#Mybatis Generator configuration
#JDBC驱动
class_path=C:\\Users\\Administrator\\.m2\\repository\\mysql\\mysql-connector-java\\5.1.9\\mysql-connector-java-5.1.9.jar
#驱动
jdbc_driver=com.mysql.jdbc.Driver
#数据库连接
jdbc_url=jdbc:mysql://localhost:3306
#数据库用户名
jdbc_user=ROOT
#数据库密码
jdbc_password=******
#项目位置
target_project=src\\main\\java
#数据库
schema=vcdb
#数据库表
table_name=table
#model类名
domain_object_name=Model
执行运行命令
** 右键 --> Run As --> Maven build
**