mybatis--分页插件使用

pom依赖

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
        </dependency>
        <!-- Mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
        </dependency>

        <dependency>
            <groupId>com.github.miemiedev</groupId>
            <artifactId>mybatis-paginator</artifactId>
        </dependency>
        <!-- MySql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- 连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>

xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<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>

结构

代码使用

     /**
      * 品牌分页
      * @param message 通用返回值
      * @return
      */
     @Override
     public Message findPage(Message message) {
          try {
               //mybatis 分页插件startPage(当前页,当前页大小)
               PageHelper.startPage(message.getTotal(),message.getTotalPage());
               //mybatis查询所有方法 返回结果强转为Page  
               //import com.github.pagehelper.Page;
               //import com.github.pagehelper.PageHelper;
               Page page = (Page) tbBrandMapper.selectByExample(null);
               // page.getTotal() 获取数据库总条数
               message.setTotal((int) page.getTotal());
               //page.getResult() 获取查询到的数据 message.setData 泛型T
               message.setData(page.getResult());
               // MessageUtils.getMessage 进行封装返回的结果
               MessageUtils.getMessage(message, "获取分页品牌分类出错", "获取分页商品分类成功");
          } catch (Exception e) {
               MessageUtils.getMessage(message, "获取分页品牌分类出错", "获取分页商品分类成功");
          }
          return message;
     }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值