pagehelper的简单使用

pagehelper:是mybatis的一个分页插件,可以帮你实现实用简单的代码实现分页查询
具体见官网:pagehelper官网
实现:
当然第一步我们应该导入相应的包

    <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.2</version>
        </dependency>

第二步:
配置mybatis的核心配置文件,在mybatis的核心配置文件中配置插件:

    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <property name="helperDialect" value="mysql"/>
        </plugin>
    </plugins>

UserMapper.java

//   查询所有用户
   List<User> queryAllUser();

UserMapper.xml:

    <select id="queryAllUser" resultType="user">
        select * from neuedu_user
    </select>

UserService.java

   //分页查询
    HigherResponse<Object> pageQueryUser(Integer pageNum,Integer pageSize);

UserServiceImpl.java

@Override
    public HigherResponse<Object> pageQueryUser(Integer pageNum, Integer pageSize) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        //调用查询所有用户的方法
       List<User> queryAllUser = userMapper.queryAllUser();
       PageInfo<User> userPageInfo = new PageInfo(queryAllUser);
       if (null ==userPageInfo){
           return HigherResponse.getReResponseFailed("查询数据失败");
       }else {
           return HigherResponse.getReResponseSuccess(userPageInfo);
       }
    }

UserController.java

    //分页查询所有用户
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public HigherResponse<Object> pageController(@RequestParam(required = true,defaultValue = "1") Integer pageNum,@RequestParam(required = true,defaultValue = "3") Integer pageSize){
        return userService.pageQueryUser(pageNum,pageSize);
    }

over!!!

你可能感兴趣的:(mybatis)