关于java:Mybatis框架的代码自动生成工具如何使用呢

5次阅读

共计 2661 个字符,预计需要花费 7 分钟才能阅读完成。

代码自动化生成的形式

  • 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

<?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=”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> <!– 生成 Model 类寄存地位 –> <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> <!– 生成 Dao 类寄存地位 –> <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 即可

正文完
 0