Springboot整合myBatis(附加pagehelper分页插件)

前提

大家好,我在身边的人当着发现对spring boot整合mybatis和pageHepler的理解不是很好,经常会报错,所以我编写了此文档,给刚入门的人观看,仅限自己的理解,如果有问题的地方,希望大家可以提出,我一定修改。

第一步,创建spring boot,导入maven依赖

依赖共6个

  1. spring-boot-starter-web ,springmvc的依赖;
  2. mybatis-spring-boot-starter ,springboot整合mybatis依赖;
  3. mysql-connector-java ,springboot整合mysql依赖;
  4. lombok ,springboot整合实体类的getsettostring等方法依赖;
  5. spring-boot-starter-test ,单元测试依赖;
  6. pagehelper-spring-boot-starter ,分页依赖;

代码

<!--springmvc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--springboot整合mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>
        <!--springboot整合mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--springboot整合实体类的getsettostring等方法-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--单元测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--分页-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
        </dependency>

第二步,进行appliction配置

  1. 配置mybatis扫描文件:mybatis.mapper-locations= classpath:com/bdqn//mapper/.xml

  2. 配置mybatis打印sql语句:mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

  3. 配置连接数据库:

    spring:
    datasource:
    url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

  4. 分页配置

    pagehelper:
    helperDialect: mysql
    reasonable: true
    supportMethodsArguments: true
    params: count=countSql

代码(将application后缀名改为yml,这是springboot推荐支持的,带提示)

mybatis:
#扫描xml文件
  mapper-locations: classpath:com/bdqn/*/mapper/*.xml
#打印sql语句
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#    map-underscore-to-camel-case: true
#配置连接数据库
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
#分页配置
pagehelper:
  helperDialect: mysql
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql

第三步,将springboot启动类进行扫描接口配置

扫描Mapper接口
@MapperScan(basePackages={“com.bdqn.*.dao”})

在这里插入图片描述
扫描第二种方法,在mapper接口上进行@Mapper,当项目运行后,进行扫描

第四步创建三层架构项目

在这里插入图片描述

第四步,运行项目,访问Controller接口

测试结果
在这里插入图片描述
控制台打印的sql语句
在这里插入图片描述

分页插件pagehelper

springboot整合mybatis最简单的一个demo已经完成了,下面就来说一说分页如何编写。
分页有固定的接受参数,第一是当前页数,第二是每页个数。返回值是总数据条数和当前页码的数据。
在业务逻辑层ServiceImpl中进行编写。

   @Override
    public PageInfo<Group> getPage(Integer pageNum, Integer pageSize) {
        //分页处理,显示第pageNum页的pageSize条数据
        PageHelper.startPage(pageNum,pageSize);
        //查询分组列表
        List<Group> list = groupDao.getAll();
        // 获取分页信息
        PageInfo<Group> infoList = new  PageInfo<>(list);
        return infoList;
    }

1.在前台获得pageNum和pageSize
2.在PageHelper中定义当前页码和显示条数
3.通过PageInfo获得分页信息
4.返回PageInfo对象

在分页插件的PageInfo中还有很多的属性:
pageNum 当前页
pageSize 每页的数量
size 当前页的数量
startRow 当前页面第一个元素在数据库中的行号
endRow 当前页面最后一个元素在数据库中的行号
total 总记录数
pages 总页数
list 结果集
等等。。。。
还有一些属性没有介绍。

总结

本文章表名了springboot整合mybatis,外加附带讲解mybatis的分页插件。如果有讲的模糊不清晰的地方,希望大家提出来,一起共同进步。
如果有对springboot整合mybatisPlus有兴趣的可以查看我的文章,在里面也有详细的讲解和mybatisPlus的分页都有讲解。

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值