一、导入依赖
<!-- MyBatis-plus的依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.4</version>
</dependency>
<!-- mysql的依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- spring-test 测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
二、指定要扫描mapper所在的包(或者在mapper类上加上@mappper注解)
代码如下:
// 指定mapper所在包
@MapperScan(basePackages = "org.xxx.mapper")
@SpringBootApplication
public class Application {
public static void main( String[] args ) {
SpringApplication.run(Application.class,args);
}
}
三、分页查询
方式一:使用 selectPage 进行分页
① 配置MyBatisPlus的拦截器
import com.baomidou.mybatisplus.annotation.DbType;
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 {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
// 1. 创建 Mybatis-plus 的拦截器
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 2. 指定要对分页操作进行拦截,指定数据库的类型为 Mysql
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
② 进行分页查询
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.xxx.pojo.Player;
import java.util.List;
@SpringBootTest
class PlayerMapperTest {
@Autowired
private PlayerMapper playerMapper;
@Test
public void test_selectPage(){
// 页码
int page = 5;
// 每一页的数据量
int pageSize = 5;
// 分页查询 null代表不指定查询条件
Page<Player> playerPage = playerMapper.selectPage(new Page<>(page, pageSize), null);
// 拿到查询结果 并输出打印
List<Player> players = playerPage.getRecords();
System.out.println(players);
}
}
方式二:使用 PageHelper 开启分页
导入PageHelper依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.6</version>
</dependency>
进行分页查询
① 利用 PageHelper.startPage(页码,每一页的数据量) 开启分页
如果利用 PageHelper.startPage() 开启了分页查询,那么selectList()本质上返回的是分页对象 (List的子类),
不然selectList查询出的结果就是普通的List类
② 通过 selectList(查询条件) 直接查询
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.xxx.pojo.Player;
import java.util.List;
@SpringBootTest
class PlayerMapperTest {
@Autowired
private PlayerMapper playerMapper;
@Test
public void test_page(){
// 页码
int page = 5;
// 每一页的数据量
int pageSize = 5;
// 开启分页查询
PageHelper.startPage(page,pageSize);
// 查询并打印结果
List<Player> players = playerMapper.selectList(null);
System.out.println(players);
}
}