使用MyBatis的分页功能

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

<dependency>
<groupId>com.github.miemiedev</groupId>
<artifactId>mybatis-paginator</artifactId>
</dependency>

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

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />

</bean>

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

<configuration>
<plugins>
<!-- com.github.pagehelper 为 PageHelper 类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL 六种数据库-->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>

</configuration>

(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<TbBrand> page = (Page<TbBrand>) 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);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值