IntelliJ IDEA 通过maven插件使用Mybatis-generator (Eclipse也可用同样方式,配置方式稍有不同)
- 对比IDE集成插件的优点:
- 能灵活调整mybatis-generator生成器版本
- 能利用maven plugin实现跨平台使用
- 在maven项目的pom.xml 添加mybatis-generator-maven-plugin 插件
<!-- mybatis-generator-maven-plugin 插件依赖包 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
</dependency>
<!-- mybatis-generator-core 代码生成逻辑核心包-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<build>
<plugins>
<!-- mybatis-generator-maven-plugin 插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</build>
- 在maven项目下的src/main/resources 目录下建立名为 generatorConfig.xml的配置文件,作为mybatis-generator-maven-plugin 插件的执行目标,模板如下:
<?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="c:\Users\admin\.m2\repository\mysql\mysql-connector-java\5.1.42\mysql-connector-java-5.1.42.jar"/>
<context id="context1">
<commentGenerator>
<!-- 是否去除自动生成的注释 (true:是 / false:否) -->
<property name="suppressAllComments" value="false"/>
<!-- 数据库注释支持 -->
<property name="addRemarkComments" value="true"/>
<!-- 时间格式设置 -->
<property name="dateFormat" value="yyyy-MM-dd HH:mm:ss"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://xxx.xxx.xxx.xxx:3306/test_girl" userId="root" password="123456"/>
<!-- entity.java -->
<javaModelGenerator targetPackage="com.star.saas.dao.entity.auto" targetProject="/saas-parent/saas-dao/src/main/java"/>
<!-- mapper.xml -->
<sqlMapGenerator targetPackage="mybatis.auto" targetProject="/saas-parent/saas-dao/src/main/resources"/>
<!-- mapper.java -->
<javaClientGenerator targetPackage="com.star.saas.dao.db.auto" targetProject="/saas-parent/saas-dao/src/main/java" type="XMLMAPPER"/>
<!-- 快递表 -->
<table tableName="express_price" domainObjectName="ExpressPriceEntity" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
</context>
</generatorConfiguration>
- 在Intellij IDEA添加一个“Run运行”选项,使用maven运行mybatis-generator-maven-plugin插件 :
mybatis-generator:generate -e