网站首页 > 技术文章 正文
前言
大家都知道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-09-29 MybatisPlus—kotlin代码生成 mybatisplus 代码生成器
- 2024-09-29 Spring boot Mybatis 整合 springboot整合mybatis流程
- 2024-09-29 SpringBoot使用Mybatis-FreeMarker
- 2024-09-29 MyBatis自动生成Mapper插件 mybatis 生成mapper
- 2024-09-29 增强Mybatis常用方案 mybatis-plus扩展
- 2024-09-29 网易架构师吐血整理:2分钟看完Mybatis核心知识点
- 2024-09-29 Mybatis的XML映射文件的继承问题 mybatis映射文件的主要元素及作用
- 2024-09-29 Mybatis 自动生成bean mybatis 自动生成bo
- 2024-09-29 深入理解Python生成器(Generators)
- 2024-09-29 如何避免出现 SQL 注入漏洞 怎样避免sql注入
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- oraclesql优化 (66)
- 类的加载机制 (75)
- feignclient (62)
- 一致性hash算法 (71)
- dockfile (66)
- 锁机制 (57)
- javaresponse (60)
- 查看hive版本 (59)
- phpworkerman (57)
- spark算子 (58)
- vue双向绑定的原理 (68)
- springbootget请求 (58)
- docker网络三种模式 (67)
- spring控制反转 (71)
- data:image/jpeg (69)
- base64 (69)
- java分页 (64)
- kibanadocker (60)
- qabstracttablemodel (62)
- java生成pdf文件 (69)
- deletelater (62)
- com.aspose.words (58)
- android.mk (62)
- qopengl (73)
- epoch_millis (61)
本文暂时没有评论,来添加一个吧(●'◡'●)