好用的Mybatis分页插件PageHelper

依赖引入


        
        <dependency>
            <groupId>com.github.pagehelpergroupId>
            <artifactId>pagehelper-spring-boot-starterartifactId>
            <version>1.2.3version>
        dependency>
        

配置文件

## 配置redis和数据库等
spring:
  ## redis
  redis:
    host: 127.0.0.1
    port: 6379
    #password: xiangjiao
    database: 0 ## 设置redis数据库索引,默认为0
    timeout: 30000 ## 连接超时时间(毫秒)
    jedis:
      pool:
        max-active: 8 ##最大连接数
        max-wait: 5000 ## 连接池最大阻塞等待时间(-1表示无等待时间)
        max-idle: 8 ##最大连接空闲
        min-idle: 0 ##最小连接空闲
  ## 数据库   
  datasource:
    url: jdbc:mysql://${requestService.url}:3306/lp_switch?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
        
## mybatis 配置
mybatis:
  ## 驼峰命名匹配
  configuration:
    map-underscore-to-camel-case: true
  type-aliases-package: cn.linkpower.dao
  ## 扫描mapper文件
  mapper-locations:
    - classpath:mybatis/switchmapper/**/*.xml
    
#配置分页插件pagehelper
pagehelper:
    helperDialect: mysql
    reasonable: true
    supportMethodsArguments: true
    params: count=countSql

sql编写

	<select id="querySwitchAndUserLists" parameterType="java.lang.Integer" 
			resultType="cn.linkpower.dto.SwitchAndUserDto">
		SELECT 
				sl.id,
				sl.imei,
				sl.name,
				sl.type,
				sl.heart,
				sl.longitude,
				sl.latitude,
				sl.area,
				sl.code,
				sl.dept_id as deptId,
				sl.is_active as isActive,
				sl.is_online as isOnline,
				sl.status,
				sl.open_status as openStatus,
				sl.user_id as userId,
				sl.create_time as createTime,
				ul.mobile,
				ul.name as userName,
				ul.group_id as groupId,
				ul.dept_id as userDeptId,
				ul.dept_name as deptName,
				ul.role_id as roleId ,
				ss.operateType,
				ss.lastOperateTimes,
				ss.lastOperateUserName
			FROM 
				switch_list sl JOIN user_list ul on sl.user_id = ul.id 
				LEFT JOIN (
					SELECT sol.imei, sol.operate_type operateType,MAX(sol.create_time) lastOperateTimes,uul.name lastOperateUserName 
					FROM switch_operate_log sol,user_list uul GROUP BY sol.imei ORDER BY sol.create_time DESC 
				) ss on sl.imei = ss.imei
			where 
				1 = 1
				<if test="userDeptId != null and userDeptId != '' ">
					and ul.dept_id = #{userDeptId}
				if>
				order by sl.create_time desc
		
	select>

调用引用

PageHelper.startPage(pageNum, pageSize);
List<SwitchAndUserDto> switchAndUserDtoLists = switchServiceImpl.querySwitchAndUserLists(userDeptId);
//数据包装
new PageInfo<SwitchAndUserDto>(switchAndUserDtoLists);

日志sql部分截取

在这里插入图片描述

自动拼接 limit ? 或者 limit ?,?

请求回执,自带数据信息:
好用的Mybatis分页插件PageHelper_第1张图片

你可能感兴趣的:(springboot)