Ibatis的Oracle分页组件

使用示例

即使用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;
public class IbatisPageInitializer
  implements InitializingBean
{
  protected Logger logger = Logger.getLogger(IbatisPageInitializer.class);
  private SqlExecutor sqlExecutor;
  private SqlMapClient sqlMapClient;
  public SqlExecutor getSqlExecutor()
  {
    return this.sqlExecutor;
  }
  public void setSqlExecutor(SqlExecutor sqlExecutor) {
    this.sqlExecutor = sqlExecutor;
  }
  public SqlMapClient getSqlMapClient() {
    return this.sqlMapClient;
  }
  public void setSqlMapClient(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");
      }
    }
  }
  protected SqlExecutor getDefaultSqlExecutor()
  {
    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();
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值