Sprin Boot2.0之整合Mybatis整合分页插件详解编程语言

pageHelper

PageHelper 是一款好用的开源免费的 Mybatis 第三方物理分页插件

物理分页

支持常见的 12 种数据库。Oracle,MySql,MariaDB,SQLite,DB2,PostgreSQL,SqlServer 等

支持多种分页方式

支持常见的 RowBounds(PageRowBounds),PageHelper.startPage 方法调用,Mapper 接口参数调用

 

pageHelper底层会帮助生成一下分页语句  limit相关

废话不多说,赶紧动手!

pom文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
  <modelVersion>4.0.0</modelVersion> 
  <groupId>pageHelper</groupId> 
  <artifactId>com.toov5.pageHelper</artifactId> 
  <version>0.0.1-SNAPSHOT</version> 
   
  <parent> 
		<groupId>org.springframework.boot</groupId> 
		<artifactId>spring-boot-starter-parent</artifactId> 
		<version>2.0.0.RELEASE</version> 
	</parent> 
	<dependencies> 
		<dependency> 
			<groupId>org.projectlombok</groupId> 
			<artifactId>lombok</artifactId> 
		</dependency> 
 
		<dependency> 
			<groupId>org.springframework.boot</groupId> 
			<artifactId>spring-boot-starter</artifactId> 
		</dependency> 
		<!-- 测试 --> 
		<dependency> 
			<groupId>org.springframework.boot</groupId> 
			<artifactId>spring-boot-starter-test</artifactId> 
			<scope>test</scope> 
		</dependency> 
		<dependency> 
			<groupId>org.mybatis.spring.boot</groupId> 
			<artifactId>mybatis-spring-boot-starter</artifactId> 
			<version>1.1.1</version> 
		</dependency> 
		<!-- mysql 依赖 --> 
		<dependency> 
			<groupId>mysql</groupId> 
			<artifactId>mysql-connector-java</artifactId> 
		</dependency> 
		<!-- springboot-web组件 --> 
		<dependency> 
			<groupId>org.springframework.boot</groupId> 
			<artifactId>spring-boot-starter-web</artifactId> 
		</dependency> 
		<!-- springboot 整合 pagehelper --> 
		<dependency> 
			<groupId>com.github.pagehelper</groupId> 
			<artifactId>pagehelper-spring-boot-starter</artifactId> 
			<version>1.2.5</version> 
		</dependency> 
		<dependency> 
			<groupId>org.apache.commons</groupId> 
			<artifactId>commons-lang3</artifactId> 
			<version>3.7</version> 
		</dependency> 
	</dependencies> 
   
</project> 

 application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/test 
spring.datasource.username=root 
spring.datasource.password=root 
spring.datasource.driver-class-name=com.mysql.jdbc.Driver 
 
 
logging.level.com.example.demo.dao=DEBUG 
pagehelper.helperDialect=mysql 
pagehelper.reasonable=true 
pagehelper.supportMethodsArguments=true 
pagehelper.params=count=countSql 
pagehelper.page-size-zero=true 

entity:

package com.toov5.entity; 
 
import lombok.Data; 
 
@Data 
public class User { 
 
    private Integer id; 
    private String name; 
    private Integer age; 
}

 

mapper:

package com.toov5.mapper; 
 
import java.util.List; 
 
import org.apache.ibatis.annotations.Select; 
 
import com.toov5.entity.User; 
 
public interface UserMapper { 
    @Select("SELECT * FROM USERS ") 
    List<User> findUserList(); 
}

service

package com.toov5.service; 
 
import java.util.List; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 
 
import com.github.pagehelper.PageHelper; 
import com.github.pagehelper.PageInfo; 
import com.toov5.entity.User; 
import com.toov5.mapper.UserMapper; 
 
@Service 
public class UserService { 
    @Autowired 
    private UserMapper userMapper; 
    //当前页 一页多少个  mysql通过limit分页的哈 
    public PageInfo<User> findUserList(int page, int size) { 
        // 开启分页插件,放在查询语句上面 帮助生成分页语句 
        PageHelper.startPage(page, size); //底层实现原理采用改写语句   将下面的方法中的sql语句获取到然后做个拼接 limit  AOPjishu  
        List<User> listUser = userMapper.findUserList(); 
        // 封装分页之后的数据  返回给客户端展示  PageInfo做了一些封装 作为一个类 
        PageInfo<User> pageInfoUser = new PageInfo<User>(listUser); 
        return pageInfoUser; 
    } 
 
}

controller

package com.toov5.controller; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 
 
import com.github.pagehelper.PageInfo; 
import com.toov5.entity.User; 
import com.toov5.service.UserService; 
 
@RestController 
public class IndexController { 
    @Autowired 
    private UserService userService; 
 
    @RequestMapping("/findUserList") 
    public PageInfo<User> findUserList(int page, int size) { 
        System.out.println("#############################"); 
        return userService.findUserList(page, size); 
    } 
 
}

启动类

package com.toov5; 
 
import org.mybatis.spring.annotation.MapperScan; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
 
@MapperScan("com.toov5.*") 
@SpringBootApplication 
public class PageHelper { 
 
    public static void main(String[] args) { 
        SpringApplication.run(PageHelper.class, args); 
    } 
 
} 

项目启动:

访问

 Sprin Boot2.0之整合Mybatis整合分页插件详解编程语言

json解析:

{ 
	"total": 4, 
	"list": [{ 
		"id": 1, 
		"name": "toov5", 
		"age": 12 
	}, { 
		"id": 2, 
		"name": "cc", 
		"age": 99 
	}], 
	"pageNum": 1, 
	"pageSize": 2, 
	"size": 2, 
	"startRow": 1, 
	"endRow": 2, 
	"pages": 2, 
	"prePage": 0, 
	"nextPage": 2, 
	"isFirstPage": true, 
	"isLastPage": false, 
	"hasPreviousPage": false, 
	"hasNextPage": true, 
	"navigatePages": 8, 
	"navigatepageNums": [1, 2], 
	"navigateFirstPage": 1, 
	"navigateLastPage": 2, 
	"firstPage": 1, 
	"lastPage": 2 
} 

  小伙伴们是不是返回的信息量十足呀~

PS:如果每个查询方面前面都这样:

Sprin Boot2.0之整合Mybatis整合分页插件详解编程语言

 

如果每个方法查询之前 之后 都加这么个方法的话 很冗余啊

冗余 则 aop封装 

 模板设计方法模式!!!哈哈哈  有兴趣的可以玩玩

 

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/16153.html

(0)
上一篇 2021年7月19日 19:01
下一篇 2021年7月19日 19:01

相关推荐

发表回复

登录后才能评论