Mybatis 学习之 分页实现

文章目录

  • [1. Mybatis](#1. Mybatis)
    • [1.1. 代码实现](#1.1. 代码实现)
  • [2. Mybatis Plus](#2. Mybatis Plus)
    • [2.1. 代码实现](#2.1. 代码实现)
    • [2.2. 特别注意](#2.2. 特别注意)
  • [3. PageHelper](#3. PageHelper)
    • [3.1. 代码实现](#3.1. 代码实现)
    • [3.2. 特别注意](#3.2. 特别注意)
  • 参考资料

1. Mybatis

1.1. 代码实现

java 复制代码
package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan(value = "com.example.demo.mapper")
@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}
java 复制代码
package com.example.demo.mapper;

import com.example.demo.model.SysUser;
import com.example.demo.model.page.BasePageReq;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface UserMapper {

    List<SysUser> page(@Param(value = "pageReq") BasePageReq pageReq);

}
xml 复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.example.demo.mapper.UserMapper">

    <select id="page" resultType="com.example.demo.model.SysUser">
        <bind name="startIndex" value="(pageReq.pageNum-1)*pageReq.pageSize"/>
        SELECT 
        	ID AS id, 
        	NAME AS name, 
        	AGE AS age
        FROM SYS_USER
        ORDER BY ID ASC
        LIMIT #{startIndex}, #{pageReq.pageSize}
    </select>

</mapper>
yml 复制代码
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.example.demo.model
xml 复制代码
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>2.3.0</version>
</dependency>

<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<version>8.0.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
	<version>1.18.24</version>
	<scope>provided</scope>
</dependency>
<dependency>
	<groupId>com.google.guava</groupId>
	<artifactId>guava</artifactId>
	<version>30.1-jre</version>
</dependency>

2. Mybatis Plus

2.1. 代码实现

java 复制代码
package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan(value = "com.example.demo.mapper")
@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}
java 复制代码
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

Page<SysUser> list = userMapper.page(new Page<>(pageReq.getPageNum(), pageReq.getPageSize()));
java 复制代码
package com.example.demo.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.model.SysUser;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface UserMapper {

    Page<SysUser> page(@Param(value = "pageReq") IPage<SysUser> pageReq);

}
java 复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.example.demo.mapper.UserMapper">

    <select id="page" resultType="com.example.demo.model.SysUser">
        SELECT ID AS id, NAME AS name, AGE AS age
        FROM SYS_USER
        ORDER BY ID ASC
    </select>

</mapper>
yml 复制代码
mybatis-plus:
  mapper-locations: classpath*:mapper/*.xml
  type-aliases-package: com.example.demo.model
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
xml 复制代码
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>com.baomidou</groupId>
	<artifactId>mybatis-plus-boot-starter</artifactId>
	<version>3.5.1</version>
</dependency>
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<version>8.0.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
	<version>1.18.24</version>
	<scope>provided</scope>
</dependency>
<dependency>
	<groupId>com.google.guava</groupId>
	<artifactId>guava</artifactId>
	<version>30.1-jre</version>
</dependency>

2.2. 特别注意

  • Mybatis Plus 分页功能中使用了 CCJSqlParser,其中 CCJSqlParser#Statement 在处理 复杂 SQL 时,存在 性能问题

3. PageHelper

3.1. 代码实现

java 复制代码
package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan(value = "com.example.demo.mapper")
@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}
java 复制代码
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

Page<SysUser> list = PageHelper.startPage(pageReq.getPageNum(), pageReq.getPageSize()).doSelectPage(() -> userMapper.page());
java 复制代码
package com.example.demo.mapper;

import com.example.demo.model.SysUser;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface UserMapper {

    List<SysUser> page();

}
xml 复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.example.demo.mapper.UserMapper">

    <select id="page" resultType="com.example.demo.model.SysUser">
        SELECT ID AS id, NAME AS name, AGE AS age
        FROM SYS_USER
        ORDER BY ID ASC
    </select>

</mapper>
yml 复制代码
mybatis-plus:
  mapper-locations: classpath*:mapper/*.xml
  type-aliases-package: com.example.demo.model
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
xml 复制代码
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>com.baomidou</groupId>
	<artifactId>mybatis-plus-boot-starter</artifactId>
	<version>3.5.1</version>
</dependency>
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<version>8.0.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
	<groupId>com.github.pagehelper</groupId>
	<artifactId>pagehelper</artifactId>
	<version>5.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
	<version>1.18.24</version>
	<scope>provided</scope>
</dependency>
<dependency>
	<groupId>com.google.guava</groupId>
	<artifactId>guava</artifactId>
	<version>30.1-jre</version>
</dependency>

3.2. 特别注意

  • PageHelper 分页功能中使用了 CCJSqlParser,其中 CCJSqlParser#Statement 在处理 复杂 SQL 时,存在 性能问题

参考资料

相关推荐
jokerest1235 小时前
web——sqliabs靶场——第十三关——报错注入+布尔盲注
mybatis
武子康5 小时前
Java-06 深入浅出 MyBatis - 一对一模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据仓库·sql·mybatis·springboot·springcloud
朝九晚五ฺ6 小时前
【Linux探索学习】第十四弹——进程优先级:深入理解操作系统中的进程优先级
linux·运维·学习
猫爪笔记8 小时前
前端:HTML (学习笔记)【1】
前端·笔记·学习·html
WindFutrue8 小时前
使用Mybatis向Mysql中的插入Point类型的数据全方位解析
数据库·mysql·mybatis
pq113_68 小时前
ftdi_sio应用学习笔记 3 - GPIO
笔记·学习·ftdi_sio
澄澈i8 小时前
设计模式学习[8]---原型模式
学习·设计模式·原型模式
AiFlutter9 小时前
Java实现简单的搜索引擎
java·搜索引擎·mybatis
爱米的前端小笔记9 小时前
前端八股自学笔记分享—页面布局(二)
前端·笔记·学习·面试·求职招聘
alikami9 小时前
【前端】前端学习
学习