计算机系统应用教程网站

网站首页 > 技术文章 正文

使用mybatis-generator自动生成代码(附GitHub下载地址)

btikc 2024-09-29 09:59:01 技术文章 12 ℃ 0 评论

前言

大家都知道Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,并且手动书写很容易出错,那么今天来介绍一下使用Mybatis-Generator来帮我们自动生成文件。如果大家有更好实现方式欢迎留言一起探讨哦,让大家开发起来更爽更便捷~~~

第一步:下载mybatis-generator工具包

GitHub地址:https://github.com/yundianzixun/mybatis-generator-1.35,如下图所示:

第二步:修改配置信息

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>
 <!-- 数据库驱动 -->
 <classPathEntry location="mysql-connector-java-5.1.9.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="数据库URL" userId="数据库用户名" password="数据库密码">
 </jdbcConnection>
 <!-- 数据库类型与java类型转换 -->
 <javaTypeResolver>
 <property name="forceBigDecimals" value="false"/>
 </javaTypeResolver>
 <!-- 生成Model类存放位置 -->
 <javaModelGenerator targetPackage="com.itunion.wxshop.model" targetProject="src">
 <property name="enableSubPackages" value="true"/>
 <property name="trimStrings" value="false"/>
 </javaModelGenerator>
 <!-- 生成映射文件存放位置 -->
 <sqlMapGenerator targetPackage="mapping" targetProject="src">
 <property name="enableSubPackages" value="true"/>
 </sqlMapGenerator>
 <!-- 生成Dao类存放位置 -->
 <javaClientGenerator type="XMLMAPPER" targetPackage="com.itunion.wxshop.mapper" targetProject="src">
 <property name="enableSubPackages" value="true"/>
 </javaClientGenerator>
 <!-- 生成对应表及类名 -->
 <table tableName="user_info" domainObjectName="UserInfo"
 enableCountByExample="false"
 enableUpdateByExample="false"
 enableDeleteByExample="false"
 enableSelectByExample="false"
 selectByExampleQueryId="false">
 </table>
 </context></generatorConfiguration>

修改点1:数据库配置

<jdbcConnection driverClass="com.mysql.jdbc.Driver"
 connectionURL="数据库URL" userId="数据库用户名" password="数据库密码"> </jdbcConnection>

修改点2:生成model类存放位置

#com.itunion.wxshop.model 可修改为自己项目映射目录
<javaModelGenerator targetPackage="com.itunion.wxshop.model" targetProject="src">
 <property name="enableSubPackages" value="true"/>
 <property name="trimStrings" value="false"/>
 </javaModelGenerator>

修改点3:生成mapping文件存放位置

#targetPackage 报名可以修改
<!-- 生成映射文件存放位置 -->
 <sqlMapGenerator targetPackage="mapping" targetProject="src">
 <property name="enableSubPackages" value="true"/>
 </sqlMapGenerator>

修改点4:生产Dao类存放位置

#targetPackage 目录可修改
<javaClientGenerator type="XMLMAPPER" targetPackage="com.itunion.wxshop.mapper" targetProject="src">
 <property name="enableSubPackages" value="true"/>
 </javaClientGenerator>

修改点5:生成对应表及类名

#对应自己的表信息(可copy多个)
<table tableName="user_info" domainObjectName="UserInfo"
 enableCountByExample="false"
 enableUpdateByExample="false"
 enableDeleteByExample="false"
 enableSelectByExample="false"
 selectByExampleQueryId="false">
 </table>

第三步:控制台执行生成命令(必须要安装好jdk哦)

  1. 进入mybatis-generator工具 lib 目录
xxx-2:~ lin$ cd /Users/lin/Downloads/JavaCode/mybatis-generator-core-1.3.5wx-shop/lib 
  1. 执行命令
xxx-2:~ lin$ cd /Users/lin/Downloads/JavaCode/mybatis-generator-core-1.3.5wx-shop/lib 
xxx-2:lib lin$ java -jar mybatis-generator-core-1.3.5.jar -configfile generatorConfig.xml -overwrite
MyBatis Generator finished successfully.
xxx-2:lib lin$ 
  1. 执行结果
MyBatis Generator finished successfully.
  1. 结果查看

第四步:将生成的文件放到自己项目中

generatorConfig.xml 文件里面的项目路径配置好了 直接copy就可以用,如果没有配置好 那么生成的内容还需要手工修改。

关注我们

更多精彩内容请关注“IT实战联盟”哦~~~

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表