Page代碼html
package com.demo.base.mybatis; import java.io.Serializable; import java.util.HashMap; import java.util.List; import java.util.Map; public class Page<T> implements Serializable { private Integer pageNo=1; private Integer pageSize=10; private Integer total = 0; private Integer totalPage = 0; private List<T> dataList; public Integer getPageNo() { return pageNo; } public void setPageNo(Integer pageNo) { this.pageNo = pageNo; } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public Integer getTotal() { return total; } public void setTotal(Integer total) { this.total = total; int totalPage = total%pageSize == 0 ? total/pageSize : total/pageSize+1; this.setTotalPage(totalPage); } public Integer getTotalPage() { return totalPage; } public void setTotalPage(Integer totalPage) { this.totalPage = totalPage; } public List<T> getDataList() { return dataList; } public void setDataList(List<T> dataList) { this.dataList = dataList; } }
PageInterceptor代碼:java
package com.demo.base.mybatis; import org.apache.ibatis.binding.MapperMethod; import org.apache.ibatis.executor.parameter.ParameterHandler; import org.apache.ibatis.executor.statement.RoutingStatementHandler; import org.apache.ibatis.executor.statement.StatementHandler; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.plugin.*; import java.lang.reflect.Field; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.Map; import java.util.Properties; import java.util.Set; /** * 在構建SQL時進行攔截,修改SQL語言進行分頁 * 分頁判斷:根據方法參數中是否包含有Page對象 * mybatis攔截器: * http://www.cnblogs.com/fangjian0423/p/mybatis-interceptor.html * https://my.oschina.net/zudajun/blog/738973 */ @Intercepts({@Signature(method = "prepare", type = StatementHandler.class, args = {Connection.class})}) public class PageInterceptor implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { RoutingStatementHandler handler = (RoutingStatementHandler) invocation.getTarget(); BoundSql boundSql = handler.getBoundSql(); Object parameterObject = boundSql.getParameterObject(); Page page = getPageParam(parameterObject);//獲取參數中Page對象 if (page != null) {//如有存在Page對象則進行相關分頁操做:總記錄數+總頁數+數據 String sql = boundSql.getSql(); ParameterHandler parameterHandler = handler.getParameterHandler(); Connection connection = (Connection) invocation.getArgs()[0]; //1.查詢總記錄 page.setTotal(qryTotalSize(sql,parameterHandler,connection)); //2.查詢分頁數據 //獲取正確當前頁數(若是當前頁大於總頁數則取總頁數) int pageNo = page.getPageNo()>page.getTotalPage() ? page.getTotalPage() : page.getPageNo(); if(pageNo < 1) pageNo = 1; page.setPageNo(pageNo); int startPos = 0; if(pageNo > 1){ startPos = (pageNo - 1) * page.getPageSize(); } String pageSql = sql + " limit " + startPos + "," + page.getPageSize(); ReflectUtil.setFieldValue(boundSql, "sql", pageSql);//使用java反射從新SQL值(帶分頁) } return invocation.proceed(); } @Override public Object plugin(Object target) { return Plugin.wrap(target, this); } @Override public void setProperties(Properties properties) { } private int qryTotalSize(String sql,ParameterHandler parameterHandler, Connection connection) throws Throwable{ String countSql = "select count(1) from ( " + sql + " ) t"; PreparedStatement pstmt = connection.prepareStatement(countSql);//設置執行SQL parameterHandler.setParameters(pstmt);//設置參數值 ResultSet rs = pstmt.executeQuery(); if (rs.next()) { return rs.getInt(1); } return 0; } private Page getPageParam(Object obj) { if (obj instanceof Page) { return (Page) obj; } else if (obj instanceof MapperMethod.ParamMap) { MapperMethod.ParamMap paramMap = (MapperMethod.ParamMap) obj; Set<Map.Entry> entrySet = paramMap.entrySet(); for (Map.Entry entry : entrySet) { if (entry.getValue() instanceof Page) { return (Page) entry.getValue(); } } } return null; } private static class ReflectUtil { /** * 利用反射獲取指定對象的指定屬性 * * @param obj 目標對象 * @param fieldName 目標屬性 * @return 目標屬性的值 */ public static Object getFieldValue(Object obj, String fieldName) { Object result = null; Field field = ReflectUtil.getField(obj, fieldName); if (field != null) { field.setAccessible(true); try { result = field.get(obj); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return result; } /** * 利用反射獲取指定對象裏面的指定屬性 * * @param obj 目標對象 * @param fieldName 目標屬性 * @return 目標字段 */ private static Field getField(Object obj, String fieldName) { Field field = null; for (Class<?> clazz = obj.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) { try { field = clazz.getDeclaredField(fieldName); break; } catch (NoSuchFieldException e) { //這裏不用作處理,子類沒有該字段可能對應的父類有,都沒有就返回null。 } } return field; } /** * 利用反射設置指定對象的指定屬性爲指定的值 * * @param obj 目標對象 * @param fieldName 目標屬性 * @param fieldValue 目標值 */ public static void setFieldValue(Object obj, String fieldName, Object fieldValue) { Field field = ReflectUtil.getField(obj, fieldName); if (field != null) { try { field.setAccessible(true); field.set(obj, fieldValue); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
在mybatis-config配置web
<plugins> <plugin interceptor="com.demo.base.mybatis.PageInterceptor"></plugin> </plugins>
UserDao加入以下方法sql
List<User> pageUser(@Param("name")String name,Page<User> page);
UserMapper配置apache
<select id="pageUser" resultMap="UserResult"> SELECT <include refid="commonColumns"/> FROM user <if test="name!=null and name!=''"> WHERE name like #{name} </if> ORDER BY id DESC </select>
public Page<User> pageUser(String name,Page<User> page){ List<User> userList = userDao.pageUser(name,page); page.setDataList(userList); return page; }
@RequestMapping("pageUser") @ResponseBody public Page<User> pageUser(String name,Page page) throws DemoException{ if(!StringUtils.isEmpty(name)){ name = "%"+name+"%"; } return userService.pageUser(name,page); }
地址:http://localhost:8080/demo-web/user/pageUser.do?name=s&pageSize=5&pageNo=2mybatis