查询出全部数据,然后再list中截取需要的部分。 mybatis接口List queryStudentsByArray();xml配置文件 select * from student service复制代码接口List queryStudentsByArray(int currPage, int pageSize);实现接口 @Override public List queryStudentsByArray(int currPage, int pageSize) { //查询全部数据 List students = studentMapper.queryStudentsByArray(); //从第几条数据开始 int firstIndex = (currPage - 1) * pageSize; //到第几条数据结束 int lastIndex = currPage * pageSize; return students.subList(firstIndex, lastIndex); //直接在list中截取 }复制代码controller复制代码 @ResponseBody @RequestMapping("/student/array/{currPage}/{pageSize}") public List getStudentByArray(@PathVariable(“currPage”) int currPage, @PathVariable(“pageSize”) int pageSize) { List student = StuServiceIml.queryStudentsByArray(currPage, pageSize); return student; }复制代码sql分页mybatis接口List queryStudentsBySql(Map<String,Object> data);xml文件 select * from student limit #{currIndex} , #{pageSize}service复制代码接口List queryStudentsBySql(int currPage, int pageSize);实现类public List queryStudentsBySql(int currPage, int pageSize) { Map<String, Object> data = new HashedMap(); data.put(“currIndex”, (currPage-1)pageSize); data.put(“pageSize”, pageSize); return studentMapper.queryStudentsBySql(data); }复制代码springjdbctemplete写分页语句拦截器分页创建拦截器,拦截mybatis接口方法id以ByPage结束的语句复制代码package com.autumn.interceptor;import org.apache.ibatis.executor.Executor;import org.apache.ibatis.executor.parameter.ParameterHandler;import org.apache.ibatis.executor.resultset.ResultSetHandler;import org.apache.ibatis.executor.statement.StatementHandler;import org.apache.ibatis.mapping.MappedStatement;import org.apache.ibatis.plugin.;import org.apache.ibatis.reflection.MetaObject;import org.apache.ibatis.reflection.SystemMetaObject;import java.sql.Connection;import java.util.Map;import java.util.Properties;/** * @Intercepts 说明是一个拦截器 * @Signature 拦截器的签名 * type 拦截的类型 四大对象之一( Executor,ResultSetHandler,ParameterHandler,StatementHandler) * method 拦截的方法 * args 参数,高版本需要加个Integer.class参数,不然会报错 */@Intercepts({@Signature(type = StatementHandler.class, method = “prepare”, args = {Connection.class})})public class MyPageInterceptor implements Interceptor { //每页显示的条目数 private int pageSize; //当前现实的页数 private int currPage; //数据库类型 private String dbType; @Override public Object intercept(Invocation invocation) throws Throwable { //获取StatementHandler,默认是RoutingStatementHandler StatementHandler statementHandler = (StatementHandler) invocation.getTarget(); //获取statementHandler包装类 MetaObject MetaObjectHandler = SystemMetaObject.forObject(statementHandler); //分离代理对象链 while (MetaObjectHandler.hasGetter(“h”)) { Object obj = MetaObjectHandler.getValue(“h”); MetaObjectHandler = SystemMetaObject.forObject(obj); } while (MetaObjectHandler.hasGetter(“target”)) { Object obj = MetaObjectHandler.getValue(“target”); MetaObjectHandler = SystemMetaObject.forObject(obj); } //获取连接对象 //Connection connection = (Connection) invocation.getArgs()[0]; //object.getValue(“delegate”); 获取StatementHandler的实现类 //获取查询接口映射的相关信息 MappedStatement mappedStatement = (MappedStatement) MetaObjectHandler.getValue(“delegate.mappedStatement”); String mapId = mappedStatement.getId(); //statementHandler.getBoundSql().getParameterObject(); //拦截以.ByPage结尾的请求,分页功能的统一实现 if (mapId.matches(".+ByPage$")) { //获取进行数据库操作时管理参数的handler ParameterHandler parameterHandler = (ParameterHandler) MetaObjectHandler.getValue(“delegate.parameterHandler”); //获取请求时的参数 Map<String, Object> paraObject = (Map<String, Object>) parameterHandler.getParameterObject(); //也可以这样获取 //paraObject = (Map<String, Object>) statementHandler.getBoundSql().getParameterObject(); //参数名称和在service中设置到map中的名称一致 currPage = (int) paraObject.get(“currPage”); pageSize = (int) paraObject.get(“pageSize”); String sql = (String) MetaObjectHandler.getValue(“delegate.boundSql.sql”); //也可以通过statementHandler直接获取 //sql = statementHandler.getBoundSql().getSql(); //构建分页功能的sql语句 String limitSql; sql = sql.trim(); limitSql = sql + " limit " + (currPage - 1) * pageSize + “,” + pageSize; //将构建完成的分页sql语句赋值个体’delegate.boundSql.sql’,偷天换日 MetaObjectHandler.setValue(“delegate.boundSql.sql”, limitSql); } //
|