其他分享
首页 > 其他分享> > 17-SSM中通过pagehelper分页的实现

17-SSM中通过pagehelper分页的实现

作者:互联网

SSM中通过pagehelper分页的实现

1. 在SSM框架的基础上实现,导包
<!--  分页 -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.1.2</version>
</dependency>
2. application.xml的配置,在sqlSessionFactory的Bean标签中加入下面的属性
<property name="plugins">
    <array>
        <bean class="com.github.pagehelper.PageInterceptor">
            <property name="properties">
                <value>
                <!-- helperDialect:有别于3.0+版本,现在必须是helperDialect,否则spring启动加载时会报错 -->
                    helperDialect=mysql
                </value>
            </property>
        </bean>
    </array>
</property>
2.1 如果你是纯javaconfig配置的SSM框架,你应该在SpringConfig下面这样配置plugins,代替第二步
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(dataSource);
    //分页 以下
    PageInterceptor pageInterceptor = new PageInterceptor();
    //创建插件需要的参数集合
            Properties properties = new Properties();
    //配置数据库方言 为oracle
            properties.setProperty("helperDialect", "mysql");
    //配置分页的合理化数据
            properties.setProperty("reasonable", "true");
            pageInterceptor.setProperties(properties);
    //将拦截器设置到sqlSessionFactroy中
            sqlSessionFactoryBean.setPlugins(new Interceptor[] {pageInterceptor});
    //以上
    return sqlSessionFactoryBean;
}
3. 编写service层,CustomService代码如下:
public interface CustomService {
 List<Custom> findAllCustom(int page,int rows);
}
4. CustomServiceImpl代码如下:
@Service
public class CustomServiceImpl implements CustomService {
@Autowired
CustomMapper customMapper;
/**
 * page 开始页数
 * rows 每页显示的数据条数
 */
@Override
public List<Custom> findAllCustom(int page,int rows) {
    //将参数传给方法实现分页
    PageHelper.startPage(page, rows);
    CustomExample example = new CustomExample();
    List<Custom> customs = customMapper.selectByExample(example);
    return customs;
}
}
5. 编写web层,CustomController代码如下:
@Controller
@RequestMapping("custom")
public class CustomController {

@Autowired
CustomService customService;

@RequestMapping("list")
@ResponseBody
public PageBean<Custom> list(int page,int rows){
    List<Custom> customs = customService.findAllCustom( page,rows);
    //查询到的数据给到PageInfo ,只需要把结果集给到该对象,获取分页信息
    // 就可以通过该对象get方法拿到总页数,总记录数,等等你想要的数据
    PageInfo<Custom> pageInfo=new PageInfo<>(customs);
    //根据前台需要在自定义一个分页对象
    //我的本次项目只需要传入页面需要的list集合,和total,同时json形式返回
    PageBean<Custom> pageBean=new PageBean<>(customs,pageInfo.getTotal());
    //把该对象json返回
    return  pageBean;
}
6. PageBean<T>创建
public class PageBean<T> {

private List<T> rows;
private  long total;
public PageBean(List<T> rows, long total) {
    this.rows = rows;
    this.total = total;
}
public PageBean() {
}
public List<T> getRows() {
    return rows;
}
public void setRows(List<T> rows) {
    this.rows = rows;
}
public long getTotal() {
    return total;
}
public void setTotal(long total) {
    this.total = total;
}
}
7. 测试
http://localhost/custom/list?rows=30&page=1

标签:rows,17,List,pagehelper,SSM,PageBean,total,public,page
来源: https://www.cnblogs.com/rqy0526/p/11326361.html