目录
springboot集成Mybatis_Plus见我的另一篇文章:
Springboot 集成Mybatis_Plus_€€-飞翔 -~£的博客-CSDN博客
一、实体类
package com.mp.test.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
private Integer id;
private String name;
private Integer age;
private String address;
public Person(String name, Integer age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
}
二、数据库操作层
package com.mp.test.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mp.test.pojo.Person;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Mapper
@Repository
public interface PersonDao extends BaseMapper<Person> {
}
三、配置中间件
package com.mp.test.config;
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 MPInterceptor {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor=new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
四、测试
package com.mp.test;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mp.test.dao.PersonDao;
import com.mp.test.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class TestApplicationTests {
@Autowired
private PersonDao personDao;
@Test
void testGetPage() {
IPage<Person> page=new Page<>(1,2);
personDao.selectPage(page,null);
System.out.println(page.getCurrent());//第几页
System.out.println(page.getSize());//页大小
System.out.println(page.getTotal());//总共条目
System.out.println(page.getPages());//共有多少页
System.out.println(page.getRecords());//返回实体类对象的数组
}
}