MyBatis-Plus分页插件和使用Mapper文件

一、分页插件

官方文档-分页插件: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<T> page(IPage<T> page);

// 条件分页查询
IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper);

// 无条件分页查询
IPage<Map<String, Object>> pageMaps(IPage<T> page);

// 条件分页查询
IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper);

注意:IPage接口只用一个实现类Page。

1、分页查询-返回DO

	@Test
	public void testPage() {
		System.out.println(("----- 分页 method test ------"));
		/**
		 * 两个参数:<br/>
		 * 		current的值默认是1,从1开始,不是0。<br/>
		 * 		size是每一页的条数。<br/>
		 */
		Page<UserDO> page = new Page<>(2, 4);
		Page<UserDO> 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);
	}

在这里插入图片描述

三、使用Mapper文件

1、指定Mapper文件目录

在yml配置文件中指定 mapper文件位置。然后创建mapper文件,和之前使用 Mybatis一样。

mybatis-plus:
  ## 默认mapper文件在 classpath*:/mapper/**/*.xml。可自定义
  mapper-locations: classpath:/mybatis/mapper/**/*.xml

在这里插入图片描述

2、分页查询-返回VO

1) 在 Mapper接口上定义方法

Page<UserVO>  pageSearch(@Param("page") IPage page, @Param("userName") String userName);

2)在 Mapper文件上定义sql

  <select id="pageSearch" resultType="com.charge.learn.mybatis.plus.vo.UserVO">
    select id, user_name userName, age, height, email, create_time createTime, update_time updateTime
    from t_user
    where user_name like concat('%',concat(#{userName},'%'))
  </select>

3)测试

    /**
     * 使用xml 分页查询
     */
    @Test
    public void testPageSearch() {
        System.out.println(("----- 使用xml 分页查询 pageSearch method test ------"));
        Page<UserVO> page = new Page<>(2, 4);
        Page<UserVO> 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);
    }

在这里插入图片描述

2、分页查询-返回VO

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<UserVO>  pageVOSearch(@Param("page") IPage page, @Param("userName") String userName);

测试和上面类似。

这里推荐两个插件。上面的附结果图主要是更直观的了解SQL的执行情况。

在这里插入图片描述

– 求知若饥,虚心若愚。

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MyBatis-Plus提供了一个方便的分页插件来进行分页查询。你可以通过在mapper.xml文件中配置相关的SQL语句来结合使用。 首先,你需要在mapper.xml文件中引入MyBatis-Plus的分页标签。在mapper标签的顶部添加以下命名空间声明: ```xml xmlns:mp="http://mybatis.org/schema/mybatis-plus" ``` 然后,在需要分页查询的SQL语句中,使用MyBatis-Plus的分页标签来配置分页参数。例如,使用`<mp:page>`标签指定分页参数: ```xml <select id="selectUsers" resultMap="UserResultMap"> SELECT * FROM users <where> <!-- 查询条件 --> </where> <mp:page current="${pageNum}" size="${pageSize}" total="${total}" reasonable="${reasonable}"/> </select> ``` 在上面的示例中,`${pageNum}`表示当前页码,`${pageSize}`表示每页显示的记录数,`${total}`表示总记录数,`${reasonable}`表示是否启用合理化分页。 最后,你可以在Java代码中使用MyBatis-Plus提供的`Page`对象来进行分页查询。例如: ```java import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; public interface UserMapper extends BaseMapper<User> { IPage<User> selectUsers(Page<User> page, @Param("condition") UserCondition condition); } ``` 在上面的示例中,`Page<User>`表示分页参数类型,`UserCondition`表示查询条件类型。 通过以上配置和代码,你就可以使用MyBatis-Plus分页插件来实现分页查询了。记得在使用分页插件之前,需要在配置文件中开启分页插件的支持。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值