mybatis plus 3.4以上分页无效问题,limit一直加不上,MybatisPlusInterceptor无效

解决方案

1、已注册

 @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
        paginationInnerInterceptor.setOverflow(true); //合理化
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        return interceptor;
    }

2、原因是MybatisSqlSessionFactoryBean没有添加插件

    @Bean
    public MybatisSqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource,MybatisPlusInterceptor mybatisPlusInterceptor) throws IOException {
        MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resolver.getResources("classpath:mapper/*.xml");
        factoryBean.setMapperLocations(resources);
        /*在加载MybatisSqlSessionFactoryBean时,把相应的插件加载进去*/
        factoryBean.setPlugins(mybatisPlusInterceptor);
        return factoryBean;
    }
在 Spring Boot 中集成最新版的 Mybatis-Plus 分页查询,需要进行以下步骤: 1. 引入 Mybatis-Plus分页插件 ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.1</version> </dependency> ``` 2. 在配置文件中进行配置 ```yaml mybatis-plus: mapper-locations: classpath*:mapper/**/*.xml configuration: map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl global-config: db-config: id-type: auto field-strategy: not_null table-prefix: t_ page-params: limit: 10 max-limit: 100 ``` 其中,`mapper-locations` 配置了 Mapper 文件的地址,`configuration` 配置了 Mybatis 的全局配置,`global-config` 配置了 Mybatis-Plus 的全局配置,`page-params` 配置了分页参数的默认值。 3. 在 Mapper 接口中添分页方法 ```java public interface UserMapper extends BaseMapper<User> { List<User> selectUserPage(Page<User> page, @Param("name") String name); } ``` 4. 在 Service 层中调用分页方法进行查询 ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public Page<User> selectUserPage(int pageNum, int pageSize, String name) { Page<User> page = new Page<>(pageNum, pageSize); userMapper.selectUserPage(page, name); return page; } } ``` 其中,`pageNum` 为当前页码,`pageSize` 为每页显示的数据量,`name` 为查询条件。分页查询将返回一个 `Page` 对象,其中包含了当前页码、每页显示的数据量、总数据量等信息,以及查询到的数据列表。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值