页面上往往会传来各种参数,比如排序参数。怎样让这些参数参与到分页实现中去呢?!
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是分页实现,不同的数据库不同,这个由大家自己实现吧就不累赘了,网上也有很多
相关推荐
myBatis3.2.2带源码和文档
MyBatis3.2.2中文官方文档,快速带你走进mybatis,对于初学者有很大帮助
myEclipse2013+spring-mvc+mybatis3.2.2实现增删改查简单完整版,可在myEclipse2013自带的Tomcat 7上运行,weblogic上可能有问题,有ModelMap 和ModelAndView的简单使用比较,ModelMap用起来更方便简洁。也使用到了c...
myBatis3.2.2(含源码、文档),想要设置免费怎么无法设置呢
源码下载!绝对的mybatis源码,在Myeclipes或Eclipse中直接引入即可看源码!
1、压缩包是一个 springmvc3.2.2 框架+mybatis3.2.2数据库操作层 的Eclipse工程 2、解压工程 修改数据库配置文件【dbConfig.xml】 编译 部署 发布 在本地或其他机器建一个表名为devType的两个字段的表,详情看映射...
myBatis3.2.2.jar
想要学习mybatis 3.2.2 源代码的童鞋可以下载去看看
MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除 了几乎所有的 JDBC 代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML 或注解用于配置和原始映射,将接口和 Java 的 ...
MyBatis3.2.2 官方文档, 用户手册 内有mybatis的详细使用说明。 已翻译为中文,易于理解
包含mybatis-3.2.2版本的jar包和源码,可以直接导包,欢迎有需要的猿们下载!
06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo...
mybatis3.2.2官网文档。自从Mybatis转移到GitHub之后就出现了官方文档在网页上很难打开的情况,严重阻碍了学习Mybatis的进程。这是本人找到的Mybatis3.2.2的PDF文档,亲测有效,免费下载。
------------------------- ------------------------ mybatis-3.2.2.jar --------------------------- ------------------------
NULL 博文链接:https://luckymjl2.iteye.com/blog/1921008
mybatis-3.2.2 jar包 mybatis-3.2.2 jar包 mybatis-3.2.2 jar包
mybatis3.2.2源码文件,欢迎大家下载!!!!!!!!!
完整的MyBatis3.2.2连接MySQL数据库示例代码,包含增删改查及测试方法代码,可以下载学习,初学者也能轻松上手。
本人博客中的代码Jsp+Servlet+MyBatis完成分页查询 http://blog.csdn.net/japanstudylang/article/details/51700874