一、分页插件
官方文档-分页插件:https://baomidou.com/pages/97710a/#paginationinnerinterceptor
1、注入分页插件我们也编写一个配置类,注入分页插件。
@Configuration
public class MyBatisPlusConfig {
/**
* 注册插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 1.添加分页插件
PaginationInnerInterceptor pageInterceptor = new PaginationInnerInterceptor();
// 设置数据库方言类型
pageInterceptor.setDbType(DbType.MYSQL);
// 下面配置根据需求自行设置
// 设置请求的页面大于最大页后操作,true调回到首页,false继续请求。默认false
pageInterceptor.setOverflow(false);
// 单页分页条数限制,默认无限制
pageInterceptor.setMaxLimit(500L);
interceptor.addInnerInterceptor(pageInterceptor);
return interceptor;
}
}
然后就可以使用了。
二、分页查询官方文档-分页查询:https://baomidou.com/pages/49cc81/#page
// 无条件分页查询
IPage page(IPage page);
// 条件分页查询
IPage page(IPage page, Wrapper queryWrapper);
// 无条件分页查询
IPage pageMaps(IPage page);
// 条件分页查询
IPage pageMaps(IPage page, Wrapper queryWrapper);
注意:IPage接口只用一个实现类Page。
@Test
public void testPage() {
System.out.println(("----- 分页 method test ------"));
/**
* 两个参数:
* current的值默认是1,从1开始,不是0。
* size是每一页的条数。
*/
Page page = new Page(2, 4);
Page userPage = userService.page(page, null);
System.out.println("当前页:" + userPage.getCurrent());
System.out.println("总页数:" + userPage.getPages());
System.out.println("记录数:" + userPage.getTotal());
System.out.println("是否有上一页:" + userPage.hasPrevious());
System.out.println("是否有下一页:" + userPage.hasNext());
System.out.println("所有记录数据如下:");
userPage.getRecords().forEach(System.out::println);
}
在yml配置文件中指定 mapper文件位置。然后创建mapper文件,和之前使用 Mybatis一样。
mybatis-plus:
## 默认mapper文件在 classpath*:/mapper/**/*.xml。可自定义
mapper-locations: classpath:/mybatis/mapper/**/*.xml
1) 在 Mapper接口上定义方法
Page pageSearch(@Param("page") IPage page, @Param("userName") String userName);
2)在 Mapper文件上定义sql
select id, user_name userName, age, height, email, create_time createTime, update_time updateTime
from t_user
where user_name like concat('%',concat(#{userName},'%'))
3)测试
/**
* 使用xml 分页查询
*/
@Test
public void testPageSearch() {
System.out.println(("----- 使用xml 分页查询 pageSearch method test ------"));
Page page = new Page(2, 4);
Page pageVOList = userMapper.pageSearch(page, "赵云");
System.out.println("当前页:" + pageVOList.getCurrent());
System.out.println("总页数:" + pageVOList.getPages());
System.out.println("记录数:" + pageVOList.getTotal());
System.out.println("是否有上一页:" + pageVOList.hasPrevious());
System.out.println("是否有下一页:" + pageVOList.hasNext());
System.out.println("所有VO记录数据如下:");
pageVOList.getRecords().forEach(System.out::println);
}
直接在 Mapper接口上使用对应的注解即可。
1、查询1) 在 Mapper接口上使用 @Select注解。
@Select("select id, user_name userName, age, height, email, create_time\n" +
" from t_user\n" +
" where id = #{id}")
UserVO selectVOByPrimaryKey(Long id);
2)测试:
/**
* 使用注解 查询
*/
@Test
public void testSelectVOByPrimaryKey2() {
System.out.println(("----- 使用注解 查询 selectVOByPrimaryKey method test ------"));
UserVO userVO = userMapper.selectVOByPrimaryKey(6L);
System.out.println("userVO = " + userVO);
}
1) 在 Mapper接口上使用 @Select注解
。
@Select("select id, user_name userName, age, height, email, create_time createTime, update_time updateTime\n" +
" from t_user\n" +
" where user_name like concat('%',concat(#{userName},'%'))")
Page pageVOSearch(@Param("page") IPage page, @Param("userName") String userName);
测试和上面类似。
这里推荐两个插件。上面的附结果图主要是更直观的了解SQL的执行情况。
– 求知若饥,虚心若愚。