前言:

mybatis-generator是根据配置文件中我们配置的数据库连接参数自动连接到数据库并根据对应的数据库表自动的生成与之对应mapper映射(比如增删改查,选择性增删改查等等简单语句)文件、对应的dao接口文件以及对应的entity实体(bean)

一、首先,我们需要引入所需要的jar包

1、mybatis-generator所需的jar包

mybatis-generator-core-1.3.2.jar (mybatis-generator-core的版本可以自行选择)

2、数据库连接jar包

比如oracle数据库用ojdbc6.jar或者mysql的mysql-connector-java.jar,版本依据数据库自行选择

二、Mybatis-Generator配置文件详解

1、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>
<!--这里写配置-->
</generatorConfiguration>

2、配置数据库连接jar包所在位置

以ojdbc6.jar为例
<!-- 指定数据连接驱动jar地址 -->
<!-- <classPathEntry location="${classPath}" /> -->
<classPathEntry location="D:\Repository\Publish\ojdbc6.jar" />

3、数据库连接参数和自动生成参数配置

详细配置的含义看对应注释
<context id="SqlTables" targetRuntime="MyBatis3">
<!-- 注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" /><!-- 是否取消注释 -->
<property name="suppressDate" value="true" /><!-- 是否生成注释代时间戳 -->
</commentGenerator> <!-- jdbc连接 -->
<!-- <jdbcConnection driverClass="${jdbc_driver}" -->
<!-- connectionURL="${jdbc_url}" userId="${jdbc_user}" -->
<!-- password="${jdbc_password}" /> -->
<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
connectionURL="jdbc:oracle:thin:@//localhost:1521/orcl" userId="admin"
password="123456" /> <!-- 类型转换 -->
<javaTypeResolver>
<!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver> <!-- 生成实体类地址 targetProject是生成文件的存放位置,targetPackage是生成文件的所在packet-->
<javaModelGenerator targetPackage="cc.eguid.blog.entity"
targetProject="D:Repository\eguid-blog-entity\src\main\java" >
<property name="enableSubPackages" value="false"/>
<property name="rootClass" value="com.itssky.aqjg.entity.base.BaseInfo"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator> <!-- 生成mapxml文件 -->
<sqlMapGenerator targetPackage="cc.eguid.blog.dao.mapper"
targetProject="D:\Repository\eguid-blog-dao\src\main\java" >
<!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator> <!-- 生成mapxml对应client,也就是接口dao -->
<javaClientGenerator targetPackage="cc.eguid.blog.dao"
targetProject="D:\Repository\aqjg\eguid-blog-dao\src\main\java" type="XMLMAPPER" >
<!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
<property name="enableSubPackages" value="false"/>
</javaClientGenerator> <!-- 配置表 -->
<!--tableName对应表名,domainObjectName是实体类名 xxxxxByExample这几个是是否生成选择性增删改查mapper-->
<table tableName="B_DBGL_PINGSYBAXX" domainObjectName="Pingsybaxx"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"></table> </context>

三、通过命令行运行

1、命令(generatorConfig.xml为配置文件)

java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

2、建议在mybatis-generator-core-1.3.2.jar所在文件夹下新建一个xxx.bat文件,里面放入上面的命令,就可以方便自动生成

四、完整配置例子

<?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>
<!-- <properties resource="generatorConfig.properties" /> -->
<!-- 指定数据连接驱动jar地址 -->
<!-- <classPathEntry location="${classPath}" /> -->
<classPathEntry location="D:\blessedRepository\Publish\ojdbc6.jar" />
<context id="SqlTables" targetRuntime="MyBatis3">
<!-- 注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" /><!-- 是否取消注释 -->
<property name="suppressDate" value="true" /><!-- 是否生成注释代时间戳 -->
</commentGenerator> <!-- jdbc连接 -->
<!-- <jdbcConnection driverClass="${jdbc_driver}" -->
<!-- connectionURL="${jdbc_url}" userId="${jdbc_user}" -->
<!-- password="${jdbc_password}" /> -->
<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
connectionURL="jdbc:oracle:thin:@//localhost:1521/orcl" userId="admin"
password="123456" /> <!-- 类型转换 -->
<javaTypeResolver>
<!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver> <!-- 生成实体类地址 targetProject是生成文件的存放位置,targetPackage是生成文件的所在packet-->
<javaModelGenerator targetPackage="cc.eguid.blog.entity"
targetProject="D:Repository\eguid-blog-entity\src\main\java" >
<property name="enableSubPackages" value="false"/>
<property name="rootClass" value="com.itssky.aqjg.entity.base.BaseInfo"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator> <!-- 生成mapxml文件 -->
<sqlMapGenerator targetPackage="cc.eguid.blog.dao.mapper"
targetProject="D:\Repository\eguid-blog-dao\src\main\java" >
<!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator> <!-- 生成mapxml对应client,也就是接口dao -->
<javaClientGenerator targetPackage="cc.eguid.blog.dao"
targetProject="D:\Repository\aqjg\eguid-blog-dao\src\main\java" type="XMLMAPPER" >
<!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
<property name="enableSubPackages" value="false"/>
</javaClientGenerator> <!-- 配置表 -->
<!--tableName对应表名,domainObjectName是实体类名 xxxxxByExample这几个是是否生成选择性增删改查mapper-->
<table tableName="B_DBGL_PINGSYBAXX" domainObjectName="Pingsybaxx"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"></table> </context>
</generatorConfiguration>

简单两步快速学会使用Mybatis-Generator自动生成entity实体、dao接口和简单mapper映射(用mysql和oracle举例)的更多相关文章

  1. 使用Mybatis Generator自动生成Mybatis相关代码

    本文将简要介绍怎样利用Mybatis Generator自动生成Mybatis的相关代码: 一.构建一个环境: 1. 首先创建一个表: CREATE TABLE pet (name VARCHAR(2 ...

  2. idea中mybatis generator自动生成代码配置 数据库是sqlserver

    好长时间没有写博客了,最近公司要用java语言,开始学习java,属于初学者,今天主要记录一下mybatis generator自动生成代码,首先在如下图的目录中新建两个文件,如下图 generato ...

  3. IDEA Maven Mybatis generator 自动生成代码

    IDEA Maven Mybatis generator 自动生成代码 一.安装配置maven以及在Idea中配置maven 安装过程步骤可以看上面的博文,里面介绍得很详细. 二.建数据表 DROP ...

  4. IDEA Maven Mybatis generator 自动生成代码(实例讲解)(转)

    IDEA Maven Mybatis generator 自动生成代码(实例讲解) MyBatis Generator • 简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的 ...

  5. SpringBoot 添加mybatis generator 自动生成代码插件

    自动生成数据层代码,提高开发效率 1.pom添加插件,并指定配置文件路径 <!-- mybatis generator 自动生成代码插件 --> <plugin> <gr ...

  6. SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件

    原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...

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

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

  8. 【Mybatis】使用Mybatis-Generator自动生成entity、dao、mapping

    使用过mybatis的应该都有用过Mybatis-Generator,本文主要介绍使用Mybatis-Generator来自动生成entity.dao.mapping文件. Mybatis-Gener ...

  9. 简单三步快速学会使用Mybatis-Generator自动生成entity实体、dao接口以及mapper映射文件(postgre使用实例)

    前言: mybatis-generator是根据配置文件中我们配置的数据库连接参数自动连接到数据库并根据对应的数据库表自动的生成与之对应mapper映射(比如增删改查,选择性增删改查等等简单语句)文件 ...

随机推荐

  1. 学习MVC之租房网站(五)-权限、角色、用户管理

    在上一篇<学习MVC之租房网站(四)-实现Service层并进行单元测试>中,记录了实现Service层并进行单元测试的过程,接下来该到"正题"-MVC了,也就是UI层 ...

  2. webmagic源码学习(一)

    最近工作主要是一些爬虫相关的东西,由于公司需要构建自己的爬虫框架,在调研过程中参考了许多优秀的开源作品,包括webmagic,webcollector,Spiderman等,通过学习这些优秀的源码获益 ...

  3. CentOS 6.8下安装docker并使用

    Docker是一个开源的应用容器引擎,可以轻松的为任何应用创建一个轻量级的.可移植的.自给自足的容器.利用Linux的LXC.AUFS.Go语言.cgroup实现了资源的独立,可以很轻松的实现文件.资 ...

  4. 如何使用HTML5自定义数据属性

    在本文中,我将向你介绍如何使用HTML5自定义数据属性.我还将向你介绍一些开发人员在工作中经常使用的优秀实例. 为什么需要自定义数据属性? 很多时候我们需要存储一些与不同DOM元素相关联的信息.这些信 ...

  5. linux内核Makefile整体分析

    转自:http://www.cnblogs.com/amanlikethis/p/3675486.html <请阅读原文> 一.概述 1.本文的意义 众多的资料(<嵌入式Linux应 ...

  6. 进程间通信系列 之 命名管道FIFO及其应用实例

    进程间通信系列 之 概述与对比   http://blog.csdn.net/younger_china/article/details/15808685  进程间通信系列 之 共享内存及其实例   ...

  7. 【NLP】3000篇搜狐新闻语料数据预处理器的python实现

    3000篇搜狐新闻语料数据预处理器的python实现 白宁超 2017年5月5日17:20:04 摘要: 关于自然语言处理模型训练亦或是数据挖掘.文本处理等等,均离不开数据清洗,数据预处理的工作.这里 ...

  8. python之基础中的基础(三)

    1.类,类就像是负责特定项目的主管,交给主管干这件事情,主管可以让手下的人分别去完成自己该干的活,最后综合起来把结果交给主管传递出去,即完成任务. class Dog(): ""& ...

  9. zabbix3.2 install

    以下参考官网 一.Zabbix安装配置(ubuntu) 1.Zabbix服务端安装 基础情况 系统 Ubuntu 14.04.4 LTS zabbix版本 zabbix 3.2 ip 192.168. ...

  10. git pull冲突:commit your changes or stash them before you can merge.

    今天用git pull来更新代码,遇到了下面的问题: error: Your local changes to the following files would be overwritten by ...