mybatis自定义分页解决方案
1、PageSqlProvider<T> —— 提供默认的分页列表查询
package com.xinyartech.erp.core.base; import com.xinyartech.erp.core.dto.page.PageDTO; /** * Provider默认抽象实现 * * @author Lynch */
public abstract class PageSqlProvider<T> { /** * 分页sql语句,注意:最外层显示字段不能包含子查询 * * @return * @author Lynch */
protected abstract String preSql(PageDTO<T> pageDto); /** * 分页列表查询 * * @param pageDto * @return * @author Lynch */
public String findAll(PageDTO<T> pageDto){ return preSql(pageDto); } /** * 统计分页总条数 * * @return * @author Lynch */
public String countAll(PageDTO<T> pageDto){ String sql = preSql(pageDto); String countSql = "select count(1) " + sql.substring(sql.indexOf("from"), sql.length()); return countSql; } }
2、StudentMapper.java —— 继承PageSqlProvider<T>,重新preSql(),自动实现分页查询和总条数查询
package com.xinyartech.erp.system.mapper; import java.util.List; import org.apache.ibatis.annotations.SelectProvider; import com.xinyartech.erp.core.base.PageSqlProvider; import com.xinyartech.erp.core.dto.page.PageDTO; import com.xinyartech.erp.system.dto.RoleResourceDTO; import com.xinyartech.erp.system.model.SysButton; public interface StudentMapper { @SelectProvider(type = StudentProvider.class, method = "findAll") public List<SysButton> querySysButtonList(PageDTO<RoleResourceDTO> pageDto); @SelectProvider(type = StudentProvider.class, method = "countAll") public Long count(PageDTO<RoleResourceDTO> pageDto); class StudentProvider extends PageSqlProvider<RoleResourceDTO> { @Override protected String preSql(PageDTO<RoleResourceDTO> pageDto) { String sql = "select * from sys_button where 1=1"; if(pageDto.getParameters().getRoleId() != null) { sql +=" and id=" + pageDto.getParameters().getRoleId(); } return sql; } } }
|