SpringBoot集成MyBatis的分页插件PageHelper

原文来自博客园【禁心尽力】https://www.cnblogs.com/1315925303zxz/p/7364552.html

SpringBoot集成MyBatis的分页插件PageHelper

Spring集成PageHelper:

第一步:pom文件引入依赖

        <!-- Mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </dependency>
        <!-- mybatis-spring-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
        </dependency>
        <!--pagehelper分页插件依赖-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
        </dependency>
        <!-- MySql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

第二步:MyBatis的核心配置文件中引入配置项

<?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>
    <!-- 【mybatis的核心配置文件】 -->

    <!-- 批量设置别名(可以不配) 作用:就是在mapper.xml文件中直接写类名,也可以不用写全路径名。 -->
    <typeAliases>
        <package name="com.jiuyue.pojo.model" />
    </typeAliases>

    <!-- 配置mybatis的分页插件PageHelper -->
    <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>

第三步:业务逻辑实现分页功能,我们只需将当前查询的页数page和每页显示的总条数rows传进去,然后Pagehelper已经帮我们分好数据了,只需在web层获取数据即可。

@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private TbItemMapper itemMapper;
    @Override
    public EasyUIDataGridResult getItemList(Integer page, Integer rows) {
        //设置分页的信息使用pagethelper
        if (page==null){
            page=1;
        }
        if (rows==null){
            rows=30;
        }
        PageHelper.startPage(page,rows);
        //注入mapper
        //创建example对象,不需要查询条件
        TbItemExample example = new TbItemExample();
        //根据mapper调用查询方法
        List<TbItem> list = itemMapper.selectByExample(example);
        //获取分页信息
        PageInfo<TbItem> info = new PageInfo<>(list);
        //封装到EasyUIDataGridResult
        EasyUIDataGridResult result = new EasyUIDataGridResult();
        result.setTotal((int) info.getTotal());
        result.setRows(info.getList());
        //返回
        return result;
    }
}

springboot集成PageHelper:

第一步:pom文件还是需要引入依赖

    <dependencies>
        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--mybatis-spring-boot-starter-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!--pagehelper分页起步依赖-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.5</version>
        </dependency>
    </dependencies>

第二步:在application.yml中配置数据源和分页插件的信息

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db_blog?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
    username: root
    password: 1111
pagehelper:
  auto-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  page-size-zero: true
server:
  port: 9000

第三步:使用插件分页功能

@Service
public class CommentService {
    @Autowired
    private CommentMapper commentMapper;

    public PageInfo<Comments> getCommentsByPage(int page, int pagesize){
        //设置分页信息
        PageHelper.startPage(page,pagesize);
        //在mysql中分页 limit,在下面查询sql中拼接
        List<Comments> list = commentMapper.getCommentsByPage();
        //获取分页信息
        PageInfo<Comments> pageInfoCommentList = new PageInfo<>(list);
        return pageInfoCommentList;
    }
}

Controller类:

@Controller
public class CommentContoller {
    @Autowired
    private CommentService commentService;

    @RequestMapping("/getCommentsByPage")
    @ResponseBody
    public PageInfo<Comments> getCommentsByPage(int page,int pagesize){
        commentService.getCommentsByPage(page,pagesize);
        return commentService.getCommentsByPage(page,pagesize);
    }
}

Mapper接口

@Mapper
public interface CommentMapper {
    @Select("select * from t_comments")
    List<Comments> getCommentsByPage();
}

查询结果:

{
    "total":13,
    "list":[
        {
            "coid":8,
            "cid":38,
            "created":"2019-04-07T20:38:28.000+0000",
            "author":"优快云",
            "ownerId":null,
            "mail":"csdn@163.com",
            "url":"https://blog.youkuaiyun.com/qq_37745470",
            "ip":"0:0:0:0:0:0:0:1",
            "status":"approved",
            "content":"http://localhost:8089/"
        },
        Object{...},
        {
            "coid":10,
            "cid":37,
            "created":"2019-04-07T13:08:42.000+0000",
            "author":"冰与火之歌",
            "ownerId":null,
            "mail":"jiuyue@163.com",
            "url":"",
            "ip":"0:0:0:0:0:0:0:1",
            "status":"approved",
            "content":"欢迎关注我的微信公众号:September"
        },
        Object{...},
        Object{...}
    ],
    "pageNum":1,
    "pageSize":5,
    "size":5,
    "startRow":1,
    "endRow":5,
    "pages":3,
    "prePage":0,
    "nextPage":2,
    "isFirstPage":true,
    "isLastPage":false,
    "hasPreviousPage":false,
    "hasNextPage":true,
    "navigatePages":8,
    "navigatepageNums":[
        1,
        2,
        3
    ],
    "navigateFirstPage":1,
    "navigateLastPage":3,
    "firstPage":1,
    "lastPage":3
}

如果我们在启动类中使用@MapperScan扫描,注意它扫描的是Mapper的接口而不是Mapper.xml.

@SpringBootApplication
@MapperScan(basePackages = "com.jiuyue.pagehelper.mapper")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值