net.jrouter.paging.ibatis.delegate.SqlSourceDelegate Maven / Gradle / Ivy
package net.jrouter.paging.ibatis.delegate;
import net.jrouter.paging.ibatis.AbstractRowBounds;
import net.jrouter.paging.ibatis.PageInterceptor;
import net.jrouter.paging.jdbc.dialect.Dialect;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.session.Configuration;
/**
* SqlSource delegate.
*
* @see org.apache.ibatis.mapping.MappedStatement#getSqlSource()
* @see org.apache.ibatis.mapping.MappedStatement#getBoundSql(java.lang.Object)
* @see PageInterceptor
*/
public abstract class SqlSourceDelegate implements SqlSource {
/**
* MyBatis Configuration.
*/
protected final Configuration configuration;
/**
* SqlSource delegate.
*/
protected final SqlSource delegate;
/**
* 数据库语义工具。
*/
protected final Dialect dialect;
public SqlSourceDelegate(Configuration c, SqlSource delegate, Dialect dialect) {
this.configuration = c;
this.delegate = delegate;
this.dialect = dialect;
}
/**
* 获取分页参数对象。此处需要动态的获取,非SqlSourceDelegate绑定对象。
*
* @return PageRowBounds对象。
*/
public abstract AbstractRowBounds getPageRowBounds();
////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* 构造Limit查询语句的SqlSource代理类。
*/
public static abstract class Page extends SqlSourceDelegate { //NOPMD ShortClassName
public Page(Configuration c, SqlSource delegate, Dialect dialect) {
super(c, delegate, dialect);
}
@Override
public BoundSql getBoundSql(Object parameterObject) {
BoundSql bs = delegate.getBoundSql(parameterObject);
AbstractRowBounds p = getPageRowBounds();
String sql = (dialect == null ? bs.getSql()
: dialect.getLimitString(bs.getSql(), p.getOffset(), p.getLimit()));
return new BoundSqlDelegate(sql, bs, configuration);
}
}
/**
* 构造Count查询语句的SqlSource代理类。
*/
public static abstract class Count extends SqlSourceDelegate {
public Count(Configuration c, SqlSource delegate, Dialect dialect) {
super(c, delegate, dialect);
}
@Override
public BoundSql getBoundSql(Object parameterObject) {
BoundSql bs = delegate.getBoundSql(parameterObject);
String sql = (dialect == null ? bs.getSql()
: dialect.getCountString(bs.getSql(), getPageRowBounds().getCountLimit()));
return new BoundSqlDelegate(sql, bs, configuration);
}
}
}