jdbcTemplate 分页 com.github.pagehelper.Page

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

由于项目中用了mybatis分页插件

com.github.pagehelper.Page

 

有时候复杂的查询分页想用jdbcTemplate。又想用分页。

分页对象

public class PageBean implements Serializable {
    private static final long serialVersionUID = 8656597559014685635L;
    private long total;        //总记录数
    private List list;    //结果集
    private int pageNum;    // 第几页
    private int pageSize;    // 每页记录数
    private int pages;        // 总页数
    private int size;        // 当前页的数量 <= pageSize,该属性来自ArrayList的size属性
    
    public PageBean() {}
    /**
     * 包装Page对象,因为直接返回Page对象,在JSON处理以及其他情况下会被当成List来处理,
     * 而出现一些问题。
     * @param list          page结果
     * @param navigatePages 页码数量
     */
    public PageBean(List list) {
        if (list instanceof Page) {
            Page page = (Page) list;
            this.pageNum = page.getPageNum();
            this.pageSize = page.getPageSize();
            this.total = page.getTotal();
            this.pages = page.getPages();
            this.list = page;
            this.size = page.size();
        }
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPages() {
        return pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }
    
}

/**
jdbcTemplate 封装类
**/
@Slf4j
public class JdbcTemplateSupport extends JdbcTemplate {
    public JdbcTemplateSupport() {
    }

    public JdbcTemplateSupport(DataSource dataSource) {
        super(dataSource);
    }

    public JdbcTemplateSupport(DataSource dataSource, boolean lazyInit) {
        super(dataSource,lazyInit);
    }

    public  PageBean queryForPage(String sql, Page pagination,RowMapper rowMapper) throws DataAccessException {

        return queryForPage(sql,pagination, new PreparedStatementSetter() {
            public void setValues(PreparedStatement preparedStatement) throws SQLException {
                return;
            }
        },rowMapper);
    }


    public  PageBean queryForPage(String sql, Page pagination,PreparedStatementSetter var2, RowMapper var3) throws DataAccessException{

    	PageBean result= new PageBean();

        //获取记录条数
        String countSql="select count(1) as count from ("+sql+") temp";
        log.info("countSql {}",countSql);
        List countList= super.query(countSql, var2, new RowMapper() {
            public Integer mapRow(ResultSet resultSet, int i) throws SQLException {
                return new Integer(resultSet.getInt("count"));
            }
        });
        
        
        result.setTotal(countList.get(0));
        result.setSize(countList.get(0));
        result.setPageNum(pagination.getPageNum());
        result.setPageSize(pagination.getPageSize());

        int pageCount=result.getSize()%result.getPageSize();
        result.setPages(pageCount==0?(result.getSize()/result.getPageSize()):(result.getSize()/result.getPageSize()+1));

        sql+=parseLimit(result);
        log.info("queryLimitSql {}",sql);

        List data= super.query(sql,var2,var3);
        result.setList(data);
        return result;
    }

    private  String parseLimit(PageBean pagination){

        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append(" ");
        stringBuffer.append("limit");
        stringBuffer.append(" ");
        if(pagination.getPageNum() == 0) {
        	stringBuffer.append("0");
        }else {
            stringBuffer.append((pagination.getPageNum()-1)*pagination.getPageSize());
        }
        stringBuffer.append(",");
        stringBuffer.append(pagination.getPageSize());

        return stringBuffer.toString();
    }

使用

Page pagination = PageHelper.startPage(PaginationContext.getPageNum(), PaginationContext.getPageSize());

	PageBean queryForPage = jdbcTemplateSupport.queryForPage(builder.toString() , pagination, new BeanPropertyRowMapper<>(Bean.class));
	

 

转载于:https://my.oschina.net/xiaominmin/blog/2989632

你可能感兴趣的:(jdbcTemplate 分页 com.github.pagehelper.Page)