网站首页 > java教程 正文
前言
大家都知道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哦)
- 进入mybatis-generator工具 lib 目录
xxx-2:~ lin$ cd /Users/lin/Downloads/JavaCode/mybatis-generator-core-1.3.5wx-shop/lib
- 执行命令
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$
- 执行结果
MyBatis Generator finished successfully.
- 结果查看
第四步:将生成的文件放到自己项目中
generatorConfig.xml 文件里面的项目路径配置好了 直接copy就可以用,如果没有配置好 那么生成的内容还需要手工修改。
关注我们
更多精彩内容请关注“IT实战联盟”哦~~~
猜你喜欢
- 2024-10-04 谷歌Bard可使用C++、Python等20多种语言生成、调试和注释代码
- 2024-10-04 java代码生成word文档(1)(java代码生成xml文件)
- 2024-10-04 GitHub 2W 星:一键生成前后端代码
- 2024-10-04 java 数字签名以及证书生成(电子签章java 数字证书)
- 2024-10-04 【Spring系列】10 MyBatis-Plus 如何自动生成代码 #java
- 2024-10-04 一键生成前后端代码,一个36k星的企业级低代码平台
- 2024-10-04 推荐一个基于SpringBoot + Mybatis + Vue的代码生成器
- 2024-10-04 一个简单的代码生成器(代码生成器思路)
- 2024-10-04 Java 开源的支持多种sql的基于spring boot的代码生成器的调试
- 2024-10-04 java开发开源工具,api文档自动生成工具
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- java反编译工具 (77)
- java反射 (57)
- java接口 (61)
- java随机数 (63)
- java7下载 (59)
- java数据结构 (61)
- java 三目运算符 (65)
- java对象转map (63)
- Java继承 (69)
- java字符串替换 (60)
- 快速排序java (59)
- java并发编程 (58)
- java api文档 (60)
- centos安装java (57)
- java调用webservice接口 (61)
- java深拷贝 (61)
- 工厂模式java (59)
- java代理模式 (59)
- java.lang (57)
- java连接mysql数据库 (67)
- java重载 (68)
- java 循环语句 (66)
- java反序列化 (58)
- java时间函数 (60)
- java是值传递还是引用传递 (62)
本文暂时没有评论,来添加一个吧(●'◡'●)