使用MybatisGenerator自动生成Model,Mapping和Mapper文件
Mybatis和Hibernate都是持久层框架,MyBatis出现的比Hibernate晚,这两种框架我都用过,对于二者的优势我的感触不深,个人感觉MyBatis自动生成model,Mapping,mapper文件的功能使编码量减少,但也很容易出错,出错后还不易排查。
我在网上搜索了一下关于Mybatis和Hibernate的比较,知乎上的这个帖子讲得比较详细,大家可以参考一下
https://www.zhihu.com/question/21104468
想要自动生成文件,首先要下载MyBatis Generator Release,下载地址https://github.com/mybatis/generator/releases
生成的文件可以直接放在工程下面的正确的包目录下,但个人建议还是先新建一个文件夹,生成后再把对应的文件拷进不同的包路径下。
我使用的是Mysql数据库,这里还要下载一个用于连接数据库的jar包,官方网址:http://www.mysql.com/products/connector/
这里关键是配置generatorConfig.xml配置文件,我在src/main/resources目录下新建了generatorConfig.xml,代码如下
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>
<!-- 用于数据库连接的jar包 -->
<classPathEntry
location="D:/code/myeclipse/mybatisGenerator/mysql-connector-java-5.1.40-bin.jar"/>
<context id="my" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="false"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 数据库连接配置 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost/book" userId="root"
password="zlj@123"/> <javaModelGenerator targetPackage="com.ese.book.pojo"
targetProject="D:/code/myeclipse/mybatisGenerator/model">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator> <sqlMapGenerator targetPackage="com.ese.book.mapping"
targetProject="D:/code/myeclipse/mybatisGenerator/mapping">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator> <javaClientGenerator targetPackage="com.ese.book.dao"
targetProject="D:/code/myeclipse/mybatisGenerator/dao" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<table tableName="books" domainObjectName="Book"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table> </context>
</generatorConfiguration>
并把这个文件拷贝到和上面两个jar包同一路径下。文件夹内容为:

条件准备好后,便可以生成文件了。生成语句:
java -jar mybatis-generator-core-1.3.5.jar -configfile generatorConfig.xml -overwrite
注:这个语句要在目标文件夹路径下执行,先要通过命令行进入D:\code\myeclipse\mybatisGenerator文件夹下。

执行后dao,mapping,model文件夹下就会分别出现BookMapper.java,BookMapping.xml和Book.java文件。具体这里不在给出。
使用MybatisGenerator自动生成Model,Mapping和Mapper文件的更多相关文章
- 使用mybatis-generator在自动生成Model类和Mapper文件
使用mybatis-generator插件可以很轻松的实现mybatis的逆向工程,即,能通过表结构自动生成对应的java类及mapper文件,可以大大提高工作效率,并且它提供了很多自定义的设置可以应 ...
- 使用mybatis-generator自动生成model、dao、mapping文件
参考文献:http://www.cnblogs.com/smileberry/p/4145872.html 一.所需库 1.mybatis-generator库 2.连接DB的驱动(此以mysql为例 ...
- mybatis-generator自动生成dao,mapping,model
mybatis-generator下载地址:https://github.com/mybatis/generator/releases/tag/mybatis-generator-1.3.2 下载好后 ...
- mybatis根据表逆向自动化生成代码(自动生成实体类、mapper文件、mapper.xml文件)
.personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...
- Mybatis-Generator 自动生成Dao、Model、Mapping相关文档
最近在学习mybatis,结果在写Mapping的映射文件时insert语句一直报错,于是想看看标准的映射文件是什么样.百度到Mybatis-Generator 自动生成Dao.Model.Mappi ...
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件(转)-----https://www.cnblogs.com/smileberry/p/4145872.html
https://www.cnblogs.com/smileberry/p/4145872.html 使用Mybatis-Generator自动生成Dao.Model.Mapping相关文件(转)
- mybatis自动生成model、dao及对应的mapper.xml文件
背景: 日常开发中,如果新建表,手动敲写model.dao和对应的mapper.xml文件,费时费力且容易出错, 所以采用mybatis自动生成model.dao及对应的mapper.xml文件.代码 ...
- MyBatis 使用Generator自动生成Model , Dao, mapper
最近 我新建了一 个maven 项目,使用的是spring + springmvc + mybatis框架. 听说Mybatis可以自动生成model和mapper以及dao层,我就从网上查了查资 ...
- 【记录】Mybatis-Generator 数据层代码生成器,自动生成dao类,mapper,pojo类
Mybatis-Generator 工具来帮我们自动创建pojo类.mapper文件以及dao类并且会帮我们配置好它们的依赖关系. 官方文档地址:http://mybatis.org/generato ...
随机推荐
- Good Time 冲刺 三
第三天 日期:2018.6.16 一.今日完成任务情况及遇到的问题 王怡镔:继续在学习微信小程序的设计,完善设计发现页面,开始编写发现页面 于鑫宇:配合黄鹤的工作,学习端口相关知 胡雅馨:继续改进优化 ...
- [剑指Offer] 13.调整数组顺序使奇数位于偶数前面
题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. [思路1]用2n ...
- 【bzoj3514】Codechef MARCH14 GERALD07加强版 LCT+可持久化线段树
题目描述 N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数. 输入 第一行四个整数N.M.K.type,代表点数.边数.询问数以及询问是否加密.接下来M行,代表图中的每条边 ...
- ECharts饼图制作分析
ECharts,缩写来自Enterprise Charts,商业级数据图表,一个纯Javascript的图表库,可以流畅的运行在PC和移动设备上,兼容当前绝大部分浏览器(IE6/7/8/9/10/11 ...
- Codeforces Round #518 Div. 1没翻车记
A:设f[i][j][0/1]为前i个数第i位为j且第i位未满足/已满足限制的方案数.大力dp前缀和优化即可. #include<iostream> #include<cstdio& ...
- git config文件总结及git alias配置
1. 文件位置 mac /etc/.gitconfig 系统级~/.gitconifg 用户级(覆盖系统级) windows C:\Users\$user\.gitconfig 当前项目下 .git/ ...
- C语言的getopt
By francis_hao Jul 5,2017 getopt:分析命令行选项 概述 #include <unistd.h>int getopt(int argc, char ...
- HNOI2002 彩票 [搜索]
题目描述 某地发行一套彩票.彩票上写有1到M这M个自然数.彩民可以在这M个数中任意选取N个不同的数打圈.每个彩民只能买一张彩票,不同的彩民的彩票上的选择不同. 每次抽奖将抽出两个自然数X和Y.如果某人 ...
- 设置edittext的样式
1.在res->drawable编写 <?xml version="1.0" encoding="utf-8"?> <shape xml ...
- 安卓sdk安装教程
http://blog.csdn.net/love4399/article/details/77164500