mybatis3.2.2 分页、排序封装实现

页面上往往会传来各种参数,比如排序参数。怎样让这些参数参与到分页实现中去呢?!

mybatis查询方法接受RowBounds对象。只好继承该对象扩展一下了。

public class MyRowBounds extends RowBounds{

private String sidx; //排序参数

private String sord; //asc or desc

public MyRowBounds(int offset, int limit ){

super(offset,limit);

}

public MyRowBounds(int offset, int limit ,String sidx,String sord){

super(offset,limit);

if(StringUtils.isNotEmpty(sidx)){

this.sidx=sidx;

this.sord=sord;

}

}

public String getSidx() {

return sidx;

}

public void setSidx(String sidx) {

this.sidx = sidx;

}

public String getSord() {

return sord;

}

public void setSord(String sord) {

this.sord = sord;

}

}

参数封装好了,该找入口了

@Intercepts({@Signature(type=StatementHandler.class,method="prepare",args={Connection.class})})

public class PaginationInterceptor implements Interceptor{

private final static Logger log = Logger.getLogger(PaginationInterceptor.class);

private Dialect dialect;

public void setDialect(Dialect dialect) {

this.dialect = dialect;

}

@Override

public Object intercept(Invocation invocation) throws Throwable {

StatementHandler statementHandler = (StatementHandler)invocation.getTarget();

BoundSql boundSql = statementHandler.getBoundSql();

MetaObject metaStatementHandler = MetaObject.forObject(

statementHandler, new DefaultObjectFactory(),new DefaultObjectWrapperFactory());

RowBounds rowBounds = (RowBounds)metaStatementHandler.getValue("delegate.rowBounds");

if(rowBounds == null || rowBounds == RowBounds.DEFAULT){

return invocation.proceed();

}

if(this.dialect == null){

throw new RuntimeException("the value of the dialect property in configuration.xml is not defined : ");

}

String originalSql = (String) metaStatementHandler.getValue("delegate.boundSql.sql");

MyRowBounds myRowBounds =(MyRowBounds)rowBounds;  

//支持排序

String pageSql=null;

if(StringUtils.isEmpty(myRowBounds.getSidx())){

pageSql=this.dialect.getLimitString(originalSql, rowBounds.getOffset(),rowBounds.getLimit());

} else {

pageSql = this.dialect.getLimitString(originalSql,

rowBounds.getOffset(), rowBounds.getLimit(),

myRowBounds.getSidx(), myRowBounds.getSord());

}

metaStatementHandler.setValue("delegate.boundSql.sql",pageSql);

metaStatementHandler.setValue("delegate.rowBounds.offset",

RowBounds.NO_ROW_OFFSET);

metaStatementHandler.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT );

if(log.isDebugEnabled()){

log.debug("生成分页SQL : " + boundSql.getSql());

}

return invocation.proceed();

}

@Override

public Object plugin(Object target) {

return Plugin.wrap(target, this);

}

@Override

public void setProperties(Properties properties) {

}

}

入口告诉大家了,具体分页语句的拼接实现,我想就不用说了

记得注入给这个类注入

<!-- 定义mybatis -->

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

        <property name="dataSource" ref="dataSource" />

        <property name="typeAliasesPackage" value="cn.com.ahsoft" />

        <property name="configLocation" value="classpath:SqlMapConfig.xml"/>

        <!-- 分页 -->

<property name="plugins">      

<bean class="cn.com.ahsoft.framework.mybatis.page.PaginationInterceptor">  

   <property name="dialect">  

       <bean class="cn.com.xxxx.framework.jdbc.dialect.MySQLDialect"></bean>  

   </property>  

</bean> 

</property> 

    </bean>

dialect是分页实现,不同的数据库不同,这个由大家自己实现吧就不累赘了,网上也有很多

猜你喜欢

转载自luckymjl2.iteye.com/blog/1921020