使用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 ...
随机推荐
- lintcode-113-删除排序链表中的重复数字 II
113-删除排序链表中的重复数字 II 给定一个排序链表,删除所有重复的元素只留下原链表中没有重复的元素. 样例 给出 1->2->3->3->4->4->5-&g ...
- PAT 1084 外观数列
https://pintia.cn/problem-sets/994805260223102976/problems/994805260583813120 外观数列是指具有以下特点的整数序列: d, ...
- velocity模板加载
http://hi.baidu.com/ly_dayu/item/828b09c5c3c5e547a8ba9409 velocity使用基本来说比较简单,但在加载模板时老出问题,很多初学者经常会遇到找 ...
- ui-grid表格怎么实现内容居中
这次是思想落后了,只关注怎么使用原生的ui-grid样式来实现这一需求,后来发现可以通过此列的cellTemplate来为列指定内容,从而可以使用css调整样式. ps:其实有时候换种思路,豁然开朗. ...
- Delphi GetCurrentDir 获取当前文件夹
//获取当前文件夹 GetCurrentDirvardir: string;begindir := GetCurrentDir;ShowMessage(dir); //C:\Documents and ...
- 使用ExecutorService实现线程池
ExecutorService是java提供的用于管理线程池的类. 线程池的作用: - 控制线程数量 - 重用线程 当一个程序中创建了许多线程,并在任务结束后销毁,会给系统带来过度消耗资源,以及过度切 ...
- P3375【模板】KMP字符串匹配
前言: 额……很久以前就写了KMP模板(只是半知不解),话说看完了manacher,再回过头看KMP,是真TM简单啊!字符串专题整体较抽象,所以必须牢记思路并时常复习 题目描述 如题,给出两个字符串s ...
- mplab c30 注册方法
http://blog.csdn.net/q553716434/article/details/7459036 关键文件是: C:\Program Files\Microchip\MPLAB C30\ ...
- [Leetcode] Binary tree maximum path sum求二叉树最大路径和
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- wyh的物品~(二分)
链接:https://www.nowcoder.com/acm/contest/93/I来源:牛客网 题目描述 wyh学长现在手里有n个物品,这n个物品的重量和价值都告诉你,然后现在让你从中选取k个, ...