使用MyBatis的分页功能

(1)pom依赖(maven添加依赖):


com.github.miemiedev
mybatis-paginator

(2)applicationContext-dao.xml中(spring加载配置信息):





SqlMapConfig.xml" />

(3)SqlMapConfig.xml中(配置信息放在xml中):




PageHelper">




(4)定义实体类 PageResult:

package entity;
import java.io.Serializable;
import java.util.List;
public class PageResult implements Serializable{
private long total;
private List rows;
public PageResult(long total, List rows) {
super();
this.total = total;
this.rows = rows;
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
public List getRows() {
return rows;
}
public void setRows(List rows) {
this.rows = rows;
}
}

(5)interface中:

public PageResult findPage(int pageNum, int pageSize);

service中重写interface中的方法:

@Override
public PageResult findPage(int pageNum, int pageSize) {
//这行代码相当于是做了初始配置,后面的代码不受其影响
PageHelper.startPage(pageNum, pageSize);

Page page = (Page) brandMapper.selectByExample(null);

return new PageResult(page.getTotal(), page.getResult());
}

(6)在controller中进行调用:

@RequestMapping("/findPage")
public PageResult findPage(int page, int size){
return brandService.findPage(page, size);
}

你可能感兴趣的:(java学习)