Mybatis自动化生成代码
Mybatis是Java EE中比较主流的一种持久化orm框架,其缺点是不够灵活,需要写的代码较多,包括:
- 一个sql-map-config.xml
- 对应每个表的xml文件
- 对应每个表的实体POJO
- DAO(数据访问对象)
好在官方提供了mybatis-generator这个组件,经过对官方例子的学习,已经正式在项目中应用了自动化代码,腰不酸腿不疼了,大大提高了生产效率。
如何使用
首先需要准备三个东西:
- mybatis-generator-core.jar 自动化需要的jar包
- mybatis-generator.xml 文件,里面需要配置数据源和一系列的自动化规则
- 执行脚本
一项一项说,mybatis-generator-core.jar 这个jar包,官网上可以下载,作者使用的是1.3.2版本。
做个网盘方便懒人吧:http://pan.baidu.com/s/1dFDhWxN
mybatis-generator.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>
<classPathEntry
location="/Users/xxxx/workspace/xxx/lib/mysql-connector-java-5.1.5-bin.jar" />
<context id="MysqlContext" targetRuntime="Ibatis2Java2" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<commentGenerator>
<property name="suppressDate" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxx"
userId="your_account"
password="your_password">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="generate.model" targetProject="/Users/xxxx/workspace/xxxx/src/main/java">
<property name="trimStrings" value="true" />
<property name="enableSubPackages" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="generate" targetProject="/Users/xxxx/workspace/xxxx/src/main/resources/db">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="SPRING" targetPackage="generate.dao" targetProject="/Users/xxxx/workspace/xxxx/src/main/java"/>
<table tableName="open_user" domainObjectName="OpenUser"
enableSelectByPrimaryKey="true"
enableSelectByExample="false"
enableDeleteByPrimaryKey="true"
enableDeleteByExample="false"
enableCountByExample="false"
enableUpdateByPrimaryKey="true"
enableUpdateByExample="false"
selectByExampleQueryId="false"
selectByPrimaryKeyQueryId="false"
>
<generatedKey column="id" sqlStatement="Mysql" identity="true"/>
</table>
</context>
</generatorConfiguration>
略微做一些解释:其中项目地址和数据源很清楚,需要正确配置。javaModelGenerator和sqlMapGenerator标签中的targetPackage是需要把自动生成的代码放到什么位置。我的做法是先统一放到一个地方generate文件夹,然后根据需要挪到目标文件夹,这样不用每次修改这个targetPackage。每次只需要修改tableName即可。
其次就是table中的一系列属性如enableSelectByPrimaryKey,含义是是否需要某某方法。这里如果全部是true,将会生成一个非常长的sqlmap文件,很不清晰,我这里只把最基本的CRUD操作放了出来,复杂查询还是后续自己手工写。
另外,项目使用的是Spring+Mybatis,所以使用的是Spring的SqlMapClientTemplate,所以javaClientGenerator是type="SPRING"。
还有context中targetRuntime这儿是跟你的Mybatis版本有关,作者使用的是古老的ibatis2.3.4,比较稳定(后来的Mybatis会有一些新引入的问题,这里不赘述了),所以这里用的是Ibatis2Java2。
最后,执行脚本的话是这样的:
java -jar ~/workspace/xxxx/lib/mybatis-generator-core-1.3.2.jar -configfile ~/workspace/xxxx/src/main/resources/db/generator.xml -overwrite
作者:纳达丶无忌
链接:https://www.jianshu.com/p/11e7629938c1
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
Mybatis自动化生成代码的更多相关文章
- 【MyBatis】MyBatis自动生成代码查询之爬坑记
前言 项目使用SSM框架搭建Web后台服务,前台后使用restful api,后台使用MyBatisGenerator自动生成代码,在前台使用关键字进行查询时,遇到了一些很宝贵的坑,现记录如下.为展示 ...
- Mybatis 自动生成代码,数据库postgresql
最近做了一个项目,使用Mybatis自动生成代码,下面做一下总结,被以后参考: 一.提前准备: 1.工具类:mybatis-generator-core-1.3.2.jar 2.postgresql驱 ...
- springboot mybatis 自动生成代码(maven+IntelliJ IDEA)
1.在pom文件中加入需要的依赖(mybatis-generator-core) 和 插件(mybatis-generator-maven-plugin) <dependency> < ...
- mybatis自动生成代码插件mybatis-generator使用流程(亲测可用)
mybatis-generator是一款在使用mybatis框架时,自动生成model,dao和mapper的工具,很大程度上减少了业务开发人员的手动编码时间 坐着在idea上用maven构建spri ...
- mybatis根据表逆向自动化生成代码(自动生成实体类、mapper文件、mapper.xml文件)
.personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...
- mybatis Generator生成代码及使用方式
本文原创,转载请注明:http://www.cnblogs.com/fengzheng/p/5889312.html 为什么要有mybatis mybatis 是一个 Java 的 ORM 框架,OR ...
- MyBatis自动生成代码示例
在项目中使用到mybatis时,都会选择自动生成实体类,Mapper,SqlMap这三个东东. 手头上在用的又不方便,找了下网上,其实有很多文章,但有些引用外部文件时不成功,也不方便,所以重新整理了下 ...
- mybatis自动生成代码
使用maven集成mybatis-generator插件生成Mybatis的实体类,DAO接口和Map映射文件 本例中,使用的是mysql数据库 前提:表已经建好 mybatis框架的jar包,数据 ...
- mybatis逆向工程生成代码
1 什么是逆向工程 mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程 可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mapper.xml.po. ...
随机推荐
- [Angular] New async 'as' syntax and ngIf.. else
From Anuglar v4 above, we are able to using 'as' with async pipe. This allow as using 'new variable' ...
- ubuntu,右键添加在终端中打开
右键中添加"在终端中打开" 在终端输入 sudo apt-get install nautilus-open-terminal 重新启动, 进入操作系统就会发现单击鼠标右键就会出 ...
- POJ 2479 Maximum sum POJ 2593 Max Sequence
d(A) = max{sum(a[s1]..a[t1]) + sum(a[s2]..a[t2]) | 1<=s1<=t1<s2<=t2<=n} 即求两个子序列和的和的最大 ...
- 9.4 Binder系统_驱动情景分析_服务使用过程
5. 服务使用过程 test_client进程: 用户态: (1)已结获得了“hello”服务,handle=1; (2)构造数据:code(那个函数)和函数参数 (3)发送ioctl后进入内核态,先 ...
- eclipse插件安装验证及问题处理
eclipse插件安装验异常时可看当前workspace下面的.metadata/.log文件,找到具体的问题来处理.一般常用到插件安装不成功的原因如下: 1.jar包冲突: 2.jar包依赖的jav ...
- 20、RTC驱动程序
drivers\rtc\rtc-s3c.c s3c_rtc_init platform_driver_register s3c_rtc_probe rtc_device_register(" ...
- HASH算法具体解释
做了几年开发,一直不理解HASH算法的原理.今天偶从百度知道上看到一个牛人神一样的理解: 这个问题有点难度.不是非常好说清楚. 我来做一个比喻吧. 我们有非常多的小猪,每一个的体重都不一样,假设体重分 ...
- swift学习第十天:函数
函数的介绍 函数相当于OC中的方法 函数的格式如下 func 函数名(参数列表) -> 返回值类型 { 代码块 return 返回值 } func是关键字,多个参数列表之间可以用逗号(,)分隔, ...
- 【50.26%】【hdu 5907】Find Q
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others) 问题描述 Byteasar迷恋上了 ...
- iOS开发Quartz2D之八:图形上下文状态栈
#import "DrawView.h" @implementation DrawView - (void)drawRect:(CGRect)rect { // Drawing c ...