SSM中通过pagehelper分页的实现
1. 在SSM框架的基础上实现,导包
2. application.xml的配置,在sqlSessionFactory的Bean标签中加入下面的属性
<property name="plugins">
<array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <value>
2.1 如果你是纯javaconfig配置的SSM框架,你应该在SpringConfig下面这样配置plugins,代替第二步
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
3. 编写service层,CustomService代码如下:
public interface CustomService {
List<Custom> findAllCustom(int page,int rows);
}
4. CustomServiceImpl代码如下:
@Service
public class CustomServiceImpl implements CustomService {
@Autowired
CustomMapper customMapper;
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);
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