使用示例
即使用ibatis提供的queryForList(sql, param, skipped, maxResults) API即可
/**
@param page 页码
@param pageSize 一页最多多少记录
*/
public List<Object> paginationTest(int page, int pageSize)
throws DAOException {
//参数
HashMap<String,String> m = new HashMap<String, String>();
m.put("p1", "test1");
m.put("p2", "test2");
if(page < 1) //page 为第几页,从 1 开始
page = 1;
int offset = (page - 1) * pageSize; //计算起始位置, 从0开始
//从第offset条记录开始取数,最多取pageSize条记录(单页记录数); offset从0开始
return getSqlMapClientTemplate().queryForList("paginationTest", m ,offset, pageSize);
}
biz-context.xml中:
<!-- 给指定的sqlMapClient添加Oracle分页实现,允许通过DAO的sqlMapClientTemplate.queryForList(sql,bizObj,offset,limit)方法 进行分页查询,从而无需自己拼装rownum条件做分页 Need JTFA 2.4.0+ --> <bean id="ibatisPageInitializer" class="com.ejintai.fa.app.integration.page.IbatisPageInitializer"> <property name="sqlMapClient" ref="sqlMapClient" /> </bean>
package com.ejintai.fa.app.integration.page;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.engine.execution.SqlExecutor;
import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient;
import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate;
import java.lang.reflect.Field;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.ReflectionUtils;
import com.ibatis.sqlmap.engine.execution.SqlExecutor;
import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient;
import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate;
import java.lang.reflect.Field;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.ReflectionUtils;
public class IbatisPageInitializer
implements InitializingBean
{
protected Logger logger = Logger.getLogger(IbatisPageInitializer.class);
private SqlExecutor sqlExecutor;
private SqlMapClient sqlMapClient;
implements InitializingBean
{
protected Logger logger = Logger.getLogger(IbatisPageInitializer.class);
private SqlExecutor sqlExecutor;
private SqlMapClient sqlMapClient;
public SqlExecutor getSqlExecutor()
{
return this.sqlExecutor;
}
{
return this.sqlExecutor;
}
public void setSqlExecutor(SqlExecutor sqlExecutor) {
this.sqlExecutor = sqlExecutor;
}
this.sqlExecutor = sqlExecutor;
}
public SqlMapClient getSqlMapClient() {
return this.sqlMapClient;
}
return this.sqlMapClient;
}
public void setSqlMapClient(SqlMapClient sqlMapClient) {
this.sqlMapClient = sqlMapClient;
}
this.sqlMapClient = sqlMapClient;
}
private void initialize()
throws Exception
{
if ((this.sqlExecutor != null) && (this.sqlMapClient != null))
{
if ((this.sqlMapClient instanceof ExtendedSqlMapClient)) {
Field field = SqlMapExecutorDelegate.class.getDeclaredField("sqlExecutor");
ReflectionUtils.makeAccessible(field);
ReflectionUtils.setField(field, ((ExtendedSqlMapClient)this.sqlMapClient).getDelegate(), this.sqlExecutor);
this.logger.info("成功替换" + this.sqlMapClient + "的SqlExecutor为:" + this.sqlExecutor);
} else {
this.logger.info("未替换" + this.sqlMapClient + "的SqlExecutor");
}
}
}
throws Exception
{
if ((this.sqlExecutor != null) && (this.sqlMapClient != null))
{
if ((this.sqlMapClient instanceof ExtendedSqlMapClient)) {
Field field = SqlMapExecutorDelegate.class.getDeclaredField("sqlExecutor");
ReflectionUtils.makeAccessible(field);
ReflectionUtils.setField(field, ((ExtendedSqlMapClient)this.sqlMapClient).getDelegate(), this.sqlExecutor);
this.logger.info("成功替换" + this.sqlMapClient + "的SqlExecutor为:" + this.sqlExecutor);
} else {
this.logger.info("未替换" + this.sqlMapClient + "的SqlExecutor");
}
}
}
protected SqlExecutor getDefaultSqlExecutor()
{
return new OracleIbatisPageSqlExecutor();
}
{
return new OracleIbatisPageSqlExecutor();
}
public void afterPropertiesSet()
throws Exception
{
if (this.sqlMapClient == null) {
throw new IllegalArgumentException("IbatisPageInitializer.sqlMapClient can not be null.");
}
if (this.sqlExecutor == null) {
this.sqlExecutor = getDefaultSqlExecutor();
this.logger.info("Ibatis使用默认的Oracle分页SQL执行器实例");
}
initialize();
}
}
throws Exception
{
if (this.sqlMapClient == null) {
throw new IllegalArgumentException("IbatisPageInitializer.sqlMapClient can not be null.");
}
if (this.sqlExecutor == null) {
this.sqlExecutor = getDefaultSqlExecutor();
this.logger.info("Ibatis使用默认的Oracle分页SQL执行器实例");
}
initialize();
}
}