最近在看iBatis时,想做用动态Sql做个分布。因为在做项目时用iBator工具生成没有分页的功能,只有一些我们常用的功能。所以要对生成后的代码做修改。我在Java高手真经的一书中看到有做了MySql SqlServer Oracle的分页。实现如下:
MySql-->
首先说说MySql的分页语句
Sql代码
- select * from user where ... order by ... limit 10,25
[sql] view plaincopy
根据以上的语句我们可以对ibator生成后的代码进行修改。
第一修改User_sqlMap.xml
修改前
Xml代码
- < select id = "ibatorgenerated_selectByExample" resultMap = "ibatorgenerated_BaseResultMap"
- parameterClass = "com.demo.ibatis.beans.UserExample" >
- select ID, LOGIN_NAME, PASSWORD
- from user
- < isParameterPresent >
- < include refid = "user.ibatorgenerated_Example_Where_Clause" />
- < isNotNull property = "orderByClause" >
- order by $orderByClause$
- </ isNotNull >
- </ isParameterPresent >
- </ select >
[xml] view plaincopy
修改后
Xml代码
- < select id = "ibatorgenerated_selectByExample" resultMap = "ibatorgenerated_BaseResultMap"
- parameterClass = "com.demo.ibatis.beans.UserExample" >
- select ID, LOGIN_NAME, PASSWORD
- from user
- < isParameterPresent >
- < include refid = "user.ibatorgenerated_Example_Where_Clause" />
- < isNotNull property = "orderByClause" >
- order by $orderByClause$
- </ isNotNull >
- [b] < isNotNull property = "limitClauseStart" >
- limit
- $limitClauseStart$,$limitClauseCount$
- </ isNotNull > [/b]
- </ isParameterPresent >
- </ select >
[xml] view plaincopy
重要的是黑色的字体是添加到原来的xml中的。接下来修改UserExample.java
在该类中添加两个字段和相应的getter/setter方法
Java代码
- protected String limitClauseStart; //起始参数
- protected String limitClauseCount; //数量参数
[java] view plaincopy
接下来就可以通过Dao调用selectByExample()方法测试,好的以上就是MySql的分页。SqlServer的分页比较难了,因为 SqlServer中没有limit的关键字。
看看SqlServer的分页语句,那当然 SqlServer人分布方法有很多种。这里我采用其中的一种。
语句如下:
Sql代码
- SELECT TOP 页大小 *
- FROM TestTable
- WHERE (ID NOT IN
- ( SELECT TOP 页大小*页数 id
- FROM 表
- ORDER BY id))
- ORDER BY ID
[sql] view plaincopy
接下来和上面的MySql一样也进行修改
修改xml
Xml代码
- < select id = "ibatorgenerated_selectByExample" resultMap = "ibatorgenerated_BaseResultMap"
- parameterClass = "com.demo.ibatis.beans.UserExample" >
- [b] < isNotNull property = "limitClauseStart" >
- SELETE TOP $limitClauseCount$ * FROM user where (ID NOT IN
- (SELECT TOP $limitClauseStart$ ID
- </ isNotNull > [/b]
- < isNull property = "limitClauseStart" >
- SELETE *
- </ isNull >
- FROM user
- from user
- < isParameterPresent >
- < include refid = "user.ibatorgenerated_Example_Where_Clause" />
- < isNotNull property = "orderByClause" >
- order by $orderByClause$
- [b] < isNotNull property = "limitClauseStart" >
- ))order by $orderByClause$
- </ isNotNull > [/b]
- </ isNotNull >
- </ isParameterPresent >
- </ select >
[xml] view plaincopy
接下来是ORacle的,和上面都是差不多的这里不多说。
语法:
select* from(select rownum tid,user.* FROM (select * from user where id>1 order by ID desc)user where where rownum<35) where tid>10;
修改Xml,
Xml代码
- < select id = "ibatorgenerated_selectByExample" resultMap = "ibatorgenerated_BaseResultMap"
- parameterClass = "com.demo.ibatis.beans.UserExample" >
- [b] < isNotNull property = "limitClauseStart" >
- select* from(select rownum tid,user.* FROM (
- </ isNotNull > [/b]
- select * from user
- < isParameterPresent >
- < include refid = "user.ibatorgenerated_Example_Where_Clause" />
- < isNotNull property = "orderByClause" >
- order by $orderByClause$
- </ isNotNull >
- [b] < isNotNull property = "limitClauseStart" >
- <![[CDATA )user where where rownum<$limitClauseCount$+$limitClauseStart$+1) where tid>$limitClauseStart$;]]>
- </ isNotNull > [/b]
- </ isParameterPresent >
- </ select >