Mybatis框架的代码自动生成工具如何使用呢?

代码自动化生成的方式

Pom.xml 文件的修改

generatorConfig.xml 配置

配置运行命令参数

案例实操

官网地址: http://generator.sturgeon.mopaas.com/index.html

对于代码自动化生成,我们借助 maven 插件来实现 mybatis crud 基本代码的生成。

配置步骤如下:

1.Pom.xml 文件的修改

添加 mybatis 插件配置

<finalName>spring_mybatis</finalName> 

<plugins> 

<plugin> 

<groupId>org.mybatis.generator</groupId> 

<artifactId>mybatis-generator-maven-plugin</artifactId> 

<version>1.3.2</version> 

<configuration> 

 <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> 

<verbose>true</verbose> 

<overwrite>true</overwrite> 

</configuration> 

</plugin> 

</plugins> 

</build>

2.generatorConfig.xml 配置

需添加到资源包下 src/mian/resources


PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 

"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 

<generatorConfiguration> 


<classPathEntry location="D:/m2/repository/mysql/mysql-connector•

java/5.1.39/mysql-connector-java-5.1.39.jar"/> 

<context id="DB2Tables" targetRuntime="MyBatis3"> 

<commentGenerator> 

<property name="suppressDate" value="true"/> 

<property name="suppressAllComments" value="true"/> 

</commentGenerator> 


<jdbcConnection driverClass="com.mysql.jdbc.Driver"

connectionURL="jdbc:mysql://127.0.0.1:3306/mybatis" userId="root"

password="root"> 

</jdbcConnection> 

<javaTypeResolver> 

<property name="forceBigDecimals" value="false"/> 

</javaTypeResolver> 


<javaModelGenerator targetPackage="com.mage.po"

targetProject="D:/java/workspace_class_0523/spring_mybatis_02/src/main/java"> 

<property name="enableSubPackages" value="true"/> 

<property name="trimStrings" value="true"/> 

</javaModelGenerator> 


<sqlMapGenerator targetPackage="com.mage.mapper"

targetProject="D:/java/workspace_class_0523/spring_mybatis_02/src/main/java"> 

<property name="enableSubPackages" value="true"/> 

</sqlMapGenerator> 


<javaClientGenerator type="XMLMAPPER" targetPackage="com.mage.dao"

targetProject="D:/java/workspace_class_0523/spring_mybatis_02/src/main/java"> 

<property name="enableSubPackages" value="true"/> 

</javaClientGenerator> 

<table tableName="house" domainObjectName="House"

enableCountByExample="false" enableUpdateByExample="false"

enableDeleteByExample="false" enableSelectByExample="false"

selectByExampleQueryId="false"></table> 

</context> 

</generatorConfiguration>

3.配置运行命令参数

window—>preferences-->java-->installed jres—>edit 在弹出的对话框中 修改 jre 运行参数

-Dmaven.multiModuleProjectDirectory=$MAVEN_HOME

MAVEN_HOME 为你配置的环境变量名

配置图如下:

以上配置如果配置完成

选中项目 run as -->maven build 在出现的对话框 Goals 输入框中 输入一下命令:

mybatis-generator:generate

然后点击 run 运行 如果你之前额配置没有错误,就会启动插件 自动生成你想要的代 码啦。

效果图如下

生成成功 日志打印如下:

最后选中项目,右键选择刷新即出现自动生成的代码!

扩展

自动生成插件压缩版

解压即可使用,和之前配置一样去配置好 config.xml,再运行 run.bat 即可

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容