mybatis (Ibatis ibator) generator - 1 mysql 分页插件(page/paging/pagination plugin)

前言

在线文档,参考 http://ibatis.apache.org/docs/tools/ibator/

由于ibatis转移为mybatis,所以参考mybatis 最新文档:http://code.google.com/p/mybatis/wiki/Downloads?tm=2

下载的MyBatis Generator Tool中有文档说明

 

http://ibatis.apache.org/docs/tools/ibator/reference/pluggingIn.html

将plugin的生命周期和怎么扩展,都已经交代明了,接下来就是动手实践了

How to

1 Plugin Class 

生成plugin class,  extends PluginAdapter. 如同文档所说,直接implements Plugin 接口会很繁琐,超多的方法要实现。

 

 

 

在写Mybatis plugin时,只需要弄两清两个问题:

  • 在哪里添加 - 决定实现plugin中的哪个方法

mybatis会生成一下artificial. 

1) Model class && Model example class

2) DAO && DAO Impl (配置文件中 javaClientGenerator type="SPRING")

3) sql map file

  • 添加什么内容

由于我们需要支持分页查询,mysql 中使用limit来进行分页查询, e.g

       select * from event where XX

 ->

       select * from event where XX  limit 0, 10  返回最前面的10个查询结果

 

因此我们需要在 select 类的sql 语句后面添加 limit $limitStart$ , $limitNum$, limitStart和limitNum作为参数传入。

我们发现limit在sql中的位置和order by 在sql中的位置很接近,因此我们借鉴了order by 语句的生成。

 

1) 在 Model Example class 中添加两个字段 limitStart, limitNum以及相应的Getter, setter方法 -> modelExampleClassGenerated

2) 在sql map 文件中的select 语句尾部添加limit $limitStart$ , $limitNum$ -> sqlMapSelectByExampleWithoutBLOBsElementGenerated

    注意我们将limitStart初始值设为-1, 同时添加 isGreaterEqual 条件判断,这样使用Model Example时,如果没有设置limitStart,则sql语句中不会加上limit。

 

我们是如何知道Method, xml element添加的写法呢?

Mybatis 本身自带了几个plguin,查看PluginAdaptor(Plugin)的子类即可了解其具体实现 --  不得不感谢IVY/MAVEN,使用自动化的依赖管理后,这些开源项目的源码自动获取,阅读相当轻松。

 

最后我们添加了Main方法,方便直接在Eclipse中运行generator,生成Code

2 config file

 

 

mybatisConfig.xml

1) 我们使用properties file来配置可能会变更的设置mybatis.properties

 

 

jdbc.mysql.url=jdbc:mysql://127.0.0.1:3306/lbs

jdbc.mysql.username=root

jdbc.mysql.password=root

 

generate.src.dir=src/main/java

generate.config.dir=src/main/resources

 

我们使用的样例数据名城是lbs,里面只有一张表event,包含两个字段id, title

 

2)  <context id="DB2Tables" targetRuntime="Ibatis2Java5">

 

 

targetRuntime我们选择Ibatis2Java5而不是MyBatis3,因为在后面的 javaClientGenerator中我们希望使用Spring DAO

If the <context> targetRuntime is MyBatis3:
XMLMAPPERThe generated objects will be Java interfaces for the MyBatis 3.x mapper infrastructure. The interfaces will be dependent on generated XML mapper files.
If the <context> targetRuntime is Ibatis2Java2 or Ibatis2Java5:
IBATISThe generated objects will conform to the (deprecated) iBATIS DAO framework.
GENERIC-CIThe generated objects will rely only on the SqlMapClient. The SqlMapClient will be supplied by constructor dependency injection. The generated objects will be in the form of DAO interfaces amd implementation classes.
GENERIC-SIThe generated objects will rely only on the SqlMapClient. The SqlMapClient will be supplied by setter dependency injection. The generated objects will be in the form of DAO interfaces amd implementation classes.
SPRINGThe generated objects will conform to the Spring DAO framework.

 

 

3) <plugin type="org.leef.db.mybatis.plugin.PaginationPlugin" />

     添加我们使用的插件     

 

生成的sql map selectByExample 如下

 

3 Use: Example class limit field

 

 

OK, 大功告成,so easy, rt?

关于mybatis generator 的故事并没有完结,后文中我们将会介绍

1) Spring DAO和 Mapper class 的区别

2) 如何handle 常量表(cache 常用表)

3) 如何handle column default value

 

 

 

 

 

无觅相关文章插件

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FireCoder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值