1.Pom.xml文件配置:
代码:
<plugins>
<!-- Mybatis generator代码生成插件 配置 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>${plugin.mybatis.generator}</version>
<configuration>
<configurationFile>${mybatis.generator.generatorConfig.xml}</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
2.在resource目录下创建generatorConfig.properties和generatorConfig.xml两个文件。
2.1 .generatorConfig.properties文件配置
# 数据库驱动jar 路径
drive.class.path=C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\5.1.30\mysql-connector-java-5.1.30.jar
# 数据库连接参数
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8
jdbc.username=simple
jdbc.password=1234
# 包路径配置
根据项目的包路径设置成自己的路径
model.package=com.ln.web.model
dao.package=com.ln.web.dao
xml.mapper.package=com.ln.web.mapper
target.project=src/main/java
2.2.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">
<!-- 配置Run As Maven build : Goals 参数 : mybatis-generator:generate -Dmybatis.generator.overwrite=true -->
<!-- 配置 tableName,使用 Run As Maven build 生成 dao model 层 -->
<generatorConfiguration>
<!-- 配置文件路径 -->
<properties url="${mybatis.generator.generatorConfig.properties}"/>
<!--数据库驱动包路径 -->
<classPathEntry location="${drive.class.path}"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<!--关闭注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库连接信息 -->
<jdbcConnection driverClass="${jdbc.driver}" connectionURL="${jdbc.url}" userId="${jdbc.username}"
password="${jdbc.password}">
</jdbcConnection>
<!--生成的model 包路径 -->
<javaModelGenerator targetPackage="${model.package}" targetProject="${target.project}">
<property name="enableSubPackages" value="ture"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成xml mapper文件 路径 -->
<sqlMapGenerator targetPackage="${xml.mapper.package}" targetProject="${target.project}">
<property name="enableSubPackages" value="ture"/>
</sqlMapGenerator>
<!-- 生成的Dao接口 的包路径 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="${dao.package}" targetProject="${target.project}">
<property name="enableSubPackages" value="ture"/>
</javaClientGenerator>
<!--对应数据库表名 -->
<table tableName="user_table"></table>
</context>
</generatorConfiguration>
3.运行
项目右键-->run as-->maven build