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文件的更多相关文章

  1. 使用mybatis-generator在自动生成Model类和Mapper文件

    使用mybatis-generator插件可以很轻松的实现mybatis的逆向工程,即,能通过表结构自动生成对应的java类及mapper文件,可以大大提高工作效率,并且它提供了很多自定义的设置可以应 ...

  2. 使用mybatis-generator自动生成model、dao、mapping文件

    参考文献:http://www.cnblogs.com/smileberry/p/4145872.html 一.所需库 1.mybatis-generator库 2.连接DB的驱动(此以mysql为例 ...

  3. mybatis-generator自动生成dao,mapping,model

    mybatis-generator下载地址:https://github.com/mybatis/generator/releases/tag/mybatis-generator-1.3.2 下载好后 ...

  4. mybatis根据表逆向自动化生成代码(自动生成实体类、mapper文件、mapper.xml文件)

    .personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...

  5. Mybatis-Generator 自动生成Dao、Model、Mapping相关文档

    最近在学习mybatis,结果在写Mapping的映射文件时insert语句一直报错,于是想看看标准的映射文件是什么样.百度到Mybatis-Generator 自动生成Dao.Model.Mappi ...

  6. 使用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相关文件(转)

  7. mybatis自动生成model、dao及对应的mapper.xml文件

    背景: 日常开发中,如果新建表,手动敲写model.dao和对应的mapper.xml文件,费时费力且容易出错, 所以采用mybatis自动生成model.dao及对应的mapper.xml文件.代码 ...

  8. MyBatis 使用Generator自动生成Model , Dao, mapper

    最近   我新建了一 个maven 项目,使用的是spring + springmvc + mybatis框架. 听说Mybatis可以自动生成model和mapper以及dao层,我就从网上查了查资 ...

  9. 【记录】Mybatis-Generator 数据层代码生成器,自动生成dao类,mapper,pojo类

    Mybatis-Generator 工具来帮我们自动创建pojo类.mapper文件以及dao类并且会帮我们配置好它们的依赖关系. 官方文档地址:http://mybatis.org/generato ...

随机推荐

  1. Ext.net中TreePanel动态生成

    这个问题可以参考官网例子:http://examples2.ext.net/#/TreePanel/Basic/Built_in_CodeBehind/ 贴一段本人程序中用到的动态生成核心代码: Ex ...

  2. 编译程序提示配置PKG_CONFIG_PATH

    http://blog.csdn.net/langeldep/article/details/6804331 在安装开源软件的过程中, 经常会碰到提示配置PKG_CONFIG_PATH路径, 或者直接 ...

  3. 201621123033 《Java程序设计》第13周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 为你的系统增加网络功能(购物车.图书馆管理.斗地主等)-分组完成 为了让你的系统可以被多个用户通过网 ...

  4. web online ide &web online editor & web online playground & web online runtime

    web online ide &web online editor web online ide &web online editor & web online playgro ...

  5. 【bzoj1176】[Balkan2007]Mokia/【bzoj2683】简单题 CDQ分治+树状数组

    bzoj1176 题目描述 维护一个W*W的矩阵,初始值均为S(题目描述有误,这里的S没有任何作用!).每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数 ...

  6. 计蒜客16495 Truefriend(fwt)

    #include <iostream> #include <cstring> #include <cstdio> using namespace std; type ...

  7. [洛谷P2032]扫描

    题目大意:有一串数,有一个长度为k的木板,求木板每次移动后覆盖的最大值 题解:单调队列 C++ Code: #include<cstdio> using namespace std; co ...

  8. [Leetcode] Reorder list 重排链表

    Given a singly linked list L: L 0→L 1→…→L n-1→L n,reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→… You ...

  9. [LeetCode] decode ways 解码方式

    A message containing letters fromA-Zis being encoded to numbers using the following mapping: 'A' -&g ...

  10. spring中Constructor、@Autowired、@PostConstruct的顺序【转】

    其实从依赖注入的字面意思就可以知道,要将对象p注入到对象a,那么首先就必须得生成对象p与对象a,才能执行注入.所以,如果一个类A中有个成员变量p被@Autowired注解,那么@Autowired注入 ...