Mybatis-Plus的分页插件的使用和配置
在com包下创建文件config.MybatisPlusConfig
package com.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
创建测试文件
package com.example.mybatisplus;
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 MybatisPLusPluginsTest {
@Autowired
private UserMapper userMapper;
@Test
public void test(){
//1页 3条数据
Page<User> page=new Page<>(1,3);
Page<User> userPage = userMapper.selectPage(page, null);
System.out.println("总页数: "+userPage.getPages());
System.out.println("所有数据显示: "+userPage.getRecords());
System.out.println("总记录数: "+userPage.getTotal());
userPage.getRecords().forEach(System.out::println);
}
}
自定义分页功能
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;
import java.util.Map;
@Repository
public interface UserMapper extends BaseMapper<User> {
//根据id查询用户信息为map集合
//自定义
Map<String,Object> selectMapById(Long id);
//自定义分页功能
//通过年龄查询用户信息并分页
//Mybatis-PLus所提供的分页对象,必须位于第一个参数位置
Page<User> selectPageVo(@Param("page")Page<User> page,@Param("age") Integer age);
}
spring:
#配置数据源的信息
datasource:
#配置数据源的类型
type:
#配置数据库的各个信息
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis_plus?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
username: root
password: '011012'
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#配置Mybatis-plus的全局变量
global-config:
db-config:
#来设置实体类对应表的统一前缀
table-prefix: t_
#设置统一的主键生成策略
id-type: auto
#配置类型别名所对应的包
type-aliases-package: com/example/pojo
userMapper.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.mapper.UserMapper">
<!--Map<String,Object> selectMapById(Long id);-->
<select id="selectMapById" resultType="map">
select * from user where id=#{id}
</select>
<!--Page<User> selectPageVo(@Param("page")Page<User> page,@Param("age") Integer age);-->
<select id="selectPageVo" resultType="User">
select * from user where age>#{age}
</select>
</mapper>
@Test
public void test2(){
Page<User> page=new Page<>(1,3);
userMapper.selectPageVo(page,20);
System.out.println(page);
}