Springboot集成Mybatis+PageHelper分页

在spring boot项目中使用PageHelper,只需要

1、Springboot项目引入mysql和mybatis的依赖:

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.0.0
        
        
            mysql
            mysql-connector-java
            5.1.47
            runtime
        

2、在application.yml配置数据库连接信息,Mybatis信息,是否打印SQL等(具体看注释):

# Springboot2.1.2默认引入的mysql版本version是8.0.13,其driver-class-name为com.mysql.cj.jdbc.Driver
spring:
  datasource:
    url: jdbc:mysql://118.25.190.197:3306/order?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: ******
    driver-class-name: com.mysql.jdbc.Driver

# type-aliases-package批量设置别名作用:就是在mapper.xml文件中直接写类名,不配置就需要写类的全路径名
mybatis:
  type-aliases-package: com.example.demo.entity
  mapper-locations: classpath:mapping/*.xml

# 将mapper接口所在包的日志级别改成debug,可以在控制台打印sql
logging:
  level:
    com.example.demo.mapper: debug

3、编写Mapper接口(普通接口,无需任何注解参与)统一放在com.example.demo.mapper包下,编写mapper.xml文件(根据上面的配置,统一放在resources/mapping下)

4、在启动类上加注解,扫描Mapper接口所在包路径:

@MapperScan("com.example.demo.mapper")

注:
也可以不在主类使用@MapperScan,而在每个Mapper接口上面添加@Mapper注解,效果一样!

Mapper接口方法有多个参数需要加@Param(“参数名”)注解,否则抛异常说找不到参数。

以上集成完成mybatis,如需集成druid数据库连接池:
5、添加druid依赖:

        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.10
        

6、修改application.yml,添加数据库连接池的配置,完成以后内容如下:

# mysql version 8.0.13的driver-class-name:com.mysql.cj.jdbc.Driver
spring:
  datasource:
    url: jdbc:mysql://118.25.190.197:3306/order?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
    # druid
    type: com.alibaba.druid.pool.DruidDataSource
    filters: stat
    maxActive: 20
    initialSize: 1
    maxWait: 60000
    minIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 'x'
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxOpenPreparedStatements: 20

# type-aliases-package批量设置别名作用:就是在mapper.xml文件中直接写类名,不配置就需要写类的全路径名
mybatis:
  type-aliases-package: com.example.demo.entity
  mapper-locations: classpath:mapping/*.xml

# 将mapper接口所在包的日志级别改成debug,可以在控制台打印sql
logging:
  level:
    com.example.demo.mapper: debug

如需集成PageHelper分页:
7、添加依赖:

        
        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.2.10
        

8、简单使用,采用默认配置即可,调用controller接口传入pageNum(即当前页码,从1开始)和pageSize(即每页条数),添加PageHepler(在开始查询之前)和PageInfo两条语句,原来的查询不需要任何修改:

    @GetMapping("/selectByPage/{pageNum}/{pageSize}")
    public PageInfo selectByPage(@PathVariable int pageNum, @PathVariable int pageSize) {
        PageHelper.startPage(pageNum, pageSize); 
        List list = gdUsersMapper.selectByPage(); //得到从pageNum开始的pageSize条数据
        //如果直接返回list,得到了分页的数据,如果添加下面步骤,返回pageInfo,则能得到包括list在内的分页信息
        PageInfo pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

你可能感兴趣的:(mybatis,spring,boot,java)