1.0 MyBatis-Plus分页插件的配置和使用)
MyBatis Plus自带的分页插件,只要简单的配置节课实现分页的功能;
1.1 添加配置类
由于版本的区别更换版本如下:此前版本无法引用MybatisPlusInterceptor 故更换使用3.5.1
<!-- <dependency>--> <!-- <groupId>com.baomidou</groupId>--> <!-- <artifactId>mybatis-plus-boot-starter</artifactId>--> <!-- <version>3.3.1</version>--> <!-- </dependency>--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency>
package com.example.config; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration //地址指向mappe层 持久层也就是Dao接口 @MapperScan("com.example.mapper") public class MyBatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { //调用的mybatis的分页拦截器 MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); //我们在此选择数据库的类型,也有其他的参数 我这边选择的mysql interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } }
查看数据库类型 DbType.(MYSQL)
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) // package com.baomidou.mybatisplus.annotation; public enum DbType { MYSQL("mysql", "MySql数据库"), MARIADB("mariadb", "MariaDB数据库"), ORACLE("oracle", "Oracle11g及以下数据库(高版本推荐使用ORACLE_NEW)"), ORACLE_12C("oracle12c", "Oracle12c+数据库"), DB2("db2", "DB2数据库"), H2("h2", "H2数据库"), HSQL("hsql", "HSQL数据库"), SQLITE("sqlite", "SQLite数据库"), POSTGRE_SQL("postgresql", "Postgre数据库"), SQL_SERVER2005("sqlserver2005", "SQLServer2005数据库"), SQL_SERVER("sqlserver", "SQLServer数据库"), DM("dm", "达梦数据库"), XU_GU("xugu", "虚谷数据库"), KINGBASE_ES("kingbasees", "人大金仓数据库"), PHOENIX("phoenix", "Phoenix HBase数据库"), GAUSS("zenith", "Gauss 数据库"), CLICK_HOUSE("clickhouse", "clickhouse 数据库"), GBASE("gbase", "南大通用(华库)数据库"), GBASEDBT("gbasedbt", "南大通用数据库"), OSCAR("oscar", "神通数据库"), SYBASE("sybase", "Sybase ASE 数据库"), OCEAN_BASE("oceanbase", "OceanBase 数据库"), FIREBIRD("Firebird", "Firebird 数据库"), HIGH_GO("highgo", "瀚高数据库"), CUBRID("cubrid", "CUBRID数据库"), GOLDILOCKS("goldilocks", "GOLDILOCKS数据库"), CSIIDB("csiidb", "CSIIDB数据库"), SAP_HANA("hana", "SAP_HANA数据库"), IMPALA("impala", "impala数据库"), OTHER("other", "其他数据库"); private final String db; private final String desc; public static DbType getDbType(String dbType) { DbType[] var1 = values(); int var2 = var1.length; for(int var3 = 0; var3 < var2; ++var3) { DbType type = var1[var3]; if (type.db.equalsIgnoreCase(dbType)) { return type; } } return OTHER; } public String getDb() { return this.db; } public String getDesc() { return this.desc; } private DbType(final String db, final String desc) { this.db = db; this.desc = desc; } }
1.2 对分页功能点进行测试
package com.example; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.example.mapper.UserMapper; import com.example.pojo.User; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest public class MybatisPlusPage { @Autowired public UserMapper userMapper; @Test public void testpage(){ // 实体类作为对象 //默认参数第几页?每页几行? Page<User> page = new Page<>(1,3); userMapper.selectPage(page,null); System.out.println(page); } }
执行结果为:
默认是第一页故我们后端传入的第一页每页3行,后端只验证3行对应 LIMIT ? 故每页3行数据;
==> Preparing: SELECT uid AS id,user_name AS name,age,email,is_delect FROM t_user WHERE is_delect=0 LIMIT ? ==> Parameters: 3(Long) <== Columns: id, name, age, email, is_delect <== Row: 4, 我是老王, 18, test4@baomidou.com, 0 <== Row: 5, Billie, 10, test5@baomidou.com, 0 <== Row: 6, 老王, 10, 757631644001, 0 <== Total: 3
当我们更换为第二页,每页3行数据的时候看下图;
LIMIT ? ,?
为2个参数第一个计算规则为 2-1后得出1*3=3;第二数据就是每页3行数据;
结果如下:
<== Columns: total <== Row: 10 <== Total: 1 ==> Preparing: SELECT uid AS id,user_name AS name,age,email,is_delect FROM t_user WHERE is_delect=0 LIMIT ?,? ==> Parameters: 3(Long), 3(Long) <== Columns: id, name, age, email, is_delect <== Row: 7, 老王超1, 10, 757631644111, 0 <== Row: 8, 闫文超2, 10, 757631644221, 0 <== Row: 9, 闫文超3, 10, 757631644331, 0 <== Total: 3
1.3 对分页功能点进行测试
在这里插入图片描述
@Test public void testpage(){ // 实体类作为对象 //默认参数第几页?每页几行? Page<User> page = new Page<>(3,2); userMapper.selectPage(page,null); System.out.println("获取当前页数据"+page.getRecords()); System.out.println("获取总页数"+page.getPages()); System.out.println("获取数据总条数"+page.getTotal()); System.out.println("是否有上一页"+page.hasNext()); System.out.println("是否有下一页"+page.hasPrevious()); System.out.println("---"); System.out.println("返回: 此对象的哈希码值"+page.hashCode()); System.out.println("当前是第几页"+page.getCurrent()); System.out.println("每页几条数据"+page.getSize()); }
测试数据如下:
<== Columns: total <== Row: 10 <== Total: 1 ==> Preparing: SELECT uid AS id,user_name AS name,age,email,is_delect FROM t_user WHERE is_delect=0 LIMIT ?,? ==> Parameters: 4(Long), 2(Long) <== Columns: id, name, age, email, is_delect <== Row: 8, 闫文超2, 10, 757631644221, 0 <== Row: 9, 闫文超3, 10, 757631644331, 0 <== Total: 2 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@76db540e] 获取当前页数据[User(id=8, name=闫文超2, age=10, email=757631644221, isDelect=0), User(id=9, name=闫文超3, age=10, email=757631644331, isDelect=0)] 获取总页数5 获取数据总条数10 是否有上一页true 是否有下一页true --- 返回: 此对象的哈希码值722321959 当前是第几页3 每页几条数据2
2.0 自定义分页Demo
package com.example.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.example.pojo.User; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; //继承 BaseMapper<实体类> 即可调用crud方法 //标记持久层组件 @Repository public interface UserMapper extends BaseMapper<User> { /** * 根据年龄查询用户列表,分页显示 * @param page 分页对象,xml中可以从里面进行取值,传递参数 Page 即自动分页,必须放在第一位 * @param age 年龄 * @return */ Page<User> selectpageVo(@Param("page") Page<User> page,@Param("age") Integer age); }
<?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.mapper.UserMapper"> <sql id="File_Column_List" > uid,user_name,age,email </sql> <select id="selectpageVo" resultType="User"> select <include refid="File_Column_List"/> from t_user where age >#{age} </select> </mapper>
下面这个配置是对应xml中resultType 返回对象的值;就行配置
spring: # 配置数据源信息 datasource: #配置数据源类型 type: com.zaxxer.hikari.HikariDataSource #配置连接数据的信息 driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&userSSL=false username: root password: root mybatis-plus: configuration: #加入mybatis 日志查看执行语句sql语句 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl global-config: db-config: table-prefix: t_ #设置统一的主键生成策略 id-type: auto #配置实体类类型别名对应的包(也就是实体类和mapper.xml的返回的对象对应) type-aliases-package: com.example.pojo
测试类
@Test public void testpage01(){ // 实体类作为对象 //默认参数第几页?每页几行? Page<User> page = new Page<>(2,2); userMapper.selectpageVo(page,10); System.out.println("page"+page); }
执行结果如下
==> Preparing: SELECT COUNT(*) AS total FROM t_user WHERE age > ? ==> Parameters: 10(Integer) <== Columns: total <== Row: 9 <== Total: 1 ==> Preparing: select uid,user_name,age,email from t_user where age >? LIMIT ?,? ==> Parameters: 10(Integer), 2(Long), 2(Long) <== Columns: uid, user_name, age, email <== Row: 4, 我是老王, 18, test4@baomidou.com <== Row: 10, 闫文超4, 14, 757631644441 <== Total: 2
MyBatis-Plus分页插件的配置和使用到这里就暂时结束了;