您好,欢迎来到叨叨游戏网。
搜索
您的当前位置:首页SpringBoot 使用 mybatis-plus 实现分页功能

SpringBoot 使用 mybatis-plus 实现分页功能

来源:叨叨游戏网

1. pom.xml 引入 mybatis-plus 依赖

        <!--        mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>

2.在application.yml配置mybatis和mybatis-plus

mybatis:
  mapper-locations: classpath:mapper/*.xml  #根目录下的 mapper 目录中加载所有的 XML 文件。
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #配置 MyBatis-Plus 使用的日志实现。

3.创建 MybatisPlusConfig 配置类

@Configuration
public class MybatisPlusConfig {
    /**
     * MybatisPlus分页插件配置类
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }

}

4. mapper 接口继承 BaseMapper<User>

@Mapper
public interface UserMapper extends BaseMapper<User> {

}

5.service接口 继承 IService<User>

public interface IUserService extends IService<User> {

}

6.service接口实现类 继承 ServiceImpl<UserMapper,User>

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements IUserService {

}

7.Controller 里使用 mybatis-plus 实现分页及模糊查询效果

@RestController
@RequestMapping("/user")
public class UserController {

    //注入 IUserService 接口
    @Autowired
    private IUserService userService;

    //分页查询
    @GetMapping("/page")
    public Page<User> page(@RequestParam Integer pageName,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String name){
        //添加模糊查询条件
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        if (!"".equals(name)){
            userQueryWrapper.like("name",name);
        }
        //通过使用userService的mybatis-plus方法实现分页
        return userService.page(new Page<>(pageName,pageSize),userQueryWrapper);
    }


}

运行结果:

不加模糊查询

加模糊查询

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- gamedaodao.net 版权所有 湘ICP备2024080961号-6

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务