简单两步快速学会使用Mybatis-Generator自动生成entity实体、dao接口和简单mapper映射(用mysql和oracle举例)
前言:
一、首先,我们需要引入所需要的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包所在位置
<!-- 指定数据连接驱动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为配置文件)
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举例)的更多相关文章
- 使用Mybatis Generator自动生成Mybatis相关代码
本文将简要介绍怎样利用Mybatis Generator自动生成Mybatis的相关代码: 一.构建一个环境: 1. 首先创建一个表: CREATE TABLE pet (name VARCHAR(2 ...
- idea中mybatis generator自动生成代码配置 数据库是sqlserver
好长时间没有写博客了,最近公司要用java语言,开始学习java,属于初学者,今天主要记录一下mybatis generator自动生成代码,首先在如下图的目录中新建两个文件,如下图 generato ...
- IDEA Maven Mybatis generator 自动生成代码
IDEA Maven Mybatis generator 自动生成代码 一.安装配置maven以及在Idea中配置maven 安装过程步骤可以看上面的博文,里面介绍得很详细. 二.建数据表 DROP ...
- IDEA Maven Mybatis generator 自动生成代码(实例讲解)(转)
IDEA Maven Mybatis generator 自动生成代码(实例讲解) MyBatis Generator • 简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的 ...
- SpringBoot 添加mybatis generator 自动生成代码插件
自动生成数据层代码,提高开发效率 1.pom添加插件,并指定配置文件路径 <!-- mybatis generator 自动生成代码插件 --> <plugin> <gr ...
- SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件
原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...
- mybatis自动生成model、dao及对应的mapper.xml文件
背景: 日常开发中,如果新建表,手动敲写model.dao和对应的mapper.xml文件,费时费力且容易出错, 所以采用mybatis自动生成model.dao及对应的mapper.xml文件.代码 ...
- 【Mybatis】使用Mybatis-Generator自动生成entity、dao、mapping
使用过mybatis的应该都有用过Mybatis-Generator,本文主要介绍使用Mybatis-Generator来自动生成entity.dao.mapping文件. Mybatis-Gener ...
- 简单三步快速学会使用Mybatis-Generator自动生成entity实体、dao接口以及mapper映射文件(postgre使用实例)
前言: mybatis-generator是根据配置文件中我们配置的数据库连接参数自动连接到数据库并根据对应的数据库表自动的生成与之对应mapper映射(比如增删改查,选择性增删改查等等简单语句)文件 ...
随机推荐
- 10个漂亮的jQuery日历插件下载【转载】
10个漂亮的jQuery日历插件下载 2013-08-07 标签:jQuery日历插件jQuery日历jQuery插件 日期是非常重要的,随时随地.微薄或网站的日期选取器日历必须在那里.您可以使用 ...
- 通过 U 盘启动重装 macOS 系统
重装系统是工作和生活中经常需要做的事情,作为一名开发人员,学会该技能你才是一名合格的程序猿!以后再也不会遇到"程旭元你会装系统吗?"的尴尬了!本文主要介绍怎样通过U盘启动重新安装 ...
- 关于v-model、v-for、v-on的用法
展示Holle Vue window.onload = function(){ var box = new Vue({ el:'#div', ...
- Android系统--输入系统(十一)Reader线程_简单处理
Android系统--输入系统(十一)Reader线程_简单处理 1. 引入 Reader线程主要负责三件事情 获得输入事件 简单处理 上传给Dispatch线程 InputReader.cpp vo ...
- My First GitHub
第一次使用github 在https://github.com/注册账号. 登陆之后,首先创建一个仓库(+ new repository),开源(public)的仓库是免费的,私人(private)的 ...
- phtread_mutex 组合
phtread_mutex通过mutexattr设定其类型,并保存在成员__kind中.pthread_mutex的锁操作函数根据__kind进行方法的分派(dispatch).__kind由5个字段 ...
- C++ 元编程 —— 让编译器帮你写程序
目录 1 C++ 中的元编程 1.1 什么是元编程 1.2 元编程在 C++ 中的位置 1.3 C++ 元编程的历史 2 元编程的语言支持 2.1 C++ 中的模板类型 2.2 C++ 中的模板参数 ...
- Mysql求百分比
根据相应条件抽出相应count数(myCount) 抽出总count数(totalCount) 计算百分比:myCount / totalCount * 100 四舍五入:使用ROUND函数ROUND ...
- Git详细教程---多人协作开发
Git可以完成两件事情: 1. 版本控制 2.多人协作开发 如今的项目,规模越来越大,功能越来越多,需要有一个团队进行开发. 如果有多个开发人员共同开发一个项目,如何进行协作的呢. Git提供了一个非 ...
- [刷题]算法竞赛入门经典(第2版) 4-3/UVa220 - Othello
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa 220 - Othello #include<iostream ...