【扩展】利用 mybatis-plus 自带分页插件Interceptor实现分页功能

1、在工程中设置配置类

在工程的包com.lingyi.mybatis.config中添加配置类MybatisPlusConfig.java

package com.lingyi.mybatis.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        paginationInnerInterceptor.setOptimizeJoin(true);
        paginationInnerInterceptor.setDbType(DbType.MYSQL);
        paginationInnerInterceptor.setOverflow(true);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor = new OptimisticLockerInnerInterceptor();
        interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor);
        return interceptor;
    }
}

2、Service类PersonService.java

package com.lingyi.mybatis.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.lingyi.mybatis.bean.Person;

import java.util.List;

/**
 * 1.在mybatis中,是在PersonService声明方法,然后在其实现类实现
 * 2.而在MyBatis-Plus中,我们可以通过继承父接口:IService,从而使用里面的很多方法
 * 3.如果它提供的方法不能满足业务需求,再自定义开发新的方法
 */
public interface PersonService extends IService<Person> {
    public List<Person> getAllPersons();

}

3、Service Impl类 PersonServiceImpl.java

package com.lingyi.mybatis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingyi.mybatis.bean.Person;
import com.lingyi.mybatis.mapper.PersonMapper;
import com.lingyi.mybatis.service.PersonService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 需要添加Service注解
 */
@Service
public class PersonServiceImpl extends ServiceImpl<PersonMapper, Person> implements PersonService {
    //通过继承ServiceImpl,这个实现类中就只用实现业务Service自定义的方法
    @Override
    public List<Person> getAllPersons() {
        //使用条件构建器,构建查询条件
        QueryWrapper<Person> queryWrapper = new QueryWrapper<>();
//        queryWrapper.ge("name","张三");

        //分页查询,每页显示10条数据,查询第1页
        Page<Person> page = new Page<>(1,10);
        //使用Lamda 表达式进行查询
        IPage<Person> personPage = this.page(page,queryWrapper);
        //返回查询结果
        return  personPage.getRecords();

    }
}

在PersonServiceImpl类中,使用了QueryWrapper构建了查询条件。然后使用Page对象表示分页查询,每页显示10条数据,查询第1页。

QueryWrapper详细用法:

QueryWrapper是Mybatis-Plus提供的查询条件构造器,用于构建条件查询。

1)等值查询

QueryWrapper<Person> queryWrapper = new QueryWrapper<>();
queryWrapper.ge("name","张三"); //等于 name = 张三

2)模糊查询

QueryWrapper<Person> queryWrapper = new QueryWrapper<>();
queryWrapper.like("name","lili");

3)多条件查询

QueryWrapper<Person> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("age",23).like("name","lili");

Page详细用法

Page用于分页查询,提供分页信息的封装。

1)创建Page对象

Page<Person> page = new Page<>(1,10);

2)使用Page进行分页查询

IPage<Person> personPage = personMapper.selectPage(page,queryWrapper);
//IPage<Person> personPage = this.page(page,queryWrapper);
List<Person> personList = personPage.getRecords();//获取查询结果

3)获取分页信息

long total = personPage.getTotal(); //获取总记录数
long current = personPage.getCurrent(); //获取当前页码
long size = personPage.getSize(); //获取每页显示条数。

4、Controller类 PersonController.java

package com.lingyi.mybatis.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingyi.mybatis.bean.Person;
import com.lingyi.mybatis.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/persons")
public class PersonController {
    //属性注入
    @Autowired
    private PersonService personService;

    //获取所有用户信息
    @GetMapping
    public ResponseEntity<List<Person>> getAllPersons(){
        List<Person> persons = personService.getAllPersons();
        return ResponseEntity.ok(persons);
    }
    //分页查询所有用户
    @GetMapping("/paged")
    public  ResponseEntity<Page<Person>> getPersonsWithPagenation(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int siz){
        //使用Mybatis-plus中Iservice接口自带page方法
        Page<Person> personPage = personService.page(new Page<>(page,siz));
        return ResponseEntity.ok(personPage);
    }

   //根据id获取用户信息
    @GetMapping("/{id}")
    public ResponseEntity<Person> getPersonById(@PathVariable Long id){
        Person person = personService.getById(id);
        return ResponseEntity.ok(person);
    }
    //添加用户
    @PostMapping
    public  ResponseEntity<Void> addPerson(@RequestBody Person person){
        personService.save(person);
        return ResponseEntity.ok().build();
    }
    //根据id更新用户
    @PostMapping("/{id}")
    public ResponseEntity<Void> updatePerson(@PathVariable Integer id,@RequestBody Person person){
        person.setId(id);
        personService.saveOrUpdate(person);
        return ResponseEntity.ok().build();
    }

    //根据id删除用户
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deletePerson(@PathVariable Long id) {
        personService.removeById(id);
        return ResponseEntity.ok().build();
    }

    @RequestMapping("/test")
    String home() {
        System.out.println("hello world");
        return "Hello springboot!";
    }

}

5、测试。

1)测试Mybatis-plus中分页对象Page(GET请求)

2)测试Mybatis-plus中Iservice接口自带page分页方法。(数据库中12条数据,返回结果中返回第1页中的10条数据,测试通过)

3)添加用户(Post请求)

  • 请求URL:127.0.0.1:8081/persons
  • 请求方式:POST
  • 请求体(Body):选择raw,格式选择json(application/json),输入需要添加的json数据

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

恋上钢琴的虫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值