mybatis分页查询

1.分页的自定义实现(以采用ssm框架,前端使用layui的项目实现为例)

MenuMapper.xml中sql查询语句需要传入开始值和查询数量

  
  
  
  

MenuMapper.java

    /**
     * 获取总菜单个数
     *
     * @return
     */
    int countMenu();
    /**
     * 获取所有菜单
     *
     * @param offset 开始值
     * @param limit  查询数据数量
     * @return
     */
    List listMenu(@Param("offset")int offset, @Param("limit")int limit);

PageBean.java列表数据封装,查询总数以及分页数据

import java.util.List;

/**
 * 作用:分页数据
 */
public class PageBean {

    private List data;

    private int count;

    public PageBean(List data, int count) {
        this.data = data;
        this.count = count;
    }

    public List getData() {
        return data;
    }

    public void setData(List data) {
        this.data = data;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}

service接口略过,MenuServiceImpl.java将查询结果封装到PageBean中

public PageBean getMenuList(Integer page, Integer limit){
    int offset = (page - 1) * limit;
	List menuList = augeMenuMapper.listMenu(offset, limit);
	int count = augeMenuMapper.countMenu();
	PageBean pageBean = new PageBean(menuList, count);
	return pageBean;
}

LayuiResponseJSONBean.java和jsp的交互json封装

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.springboot.first.domain.enums.ClientResponseStatusEnum;

/**
 * 为LayUI
 * Created by FENGCUIJIE on 2017/7/13.
 */
public class LayuiResponseJSONBean {
    /**
     * 错误消息
     */
    private String msg;

    /**
     * 返回代码
     */
    private String code;
    /**
     * 返回数据集合
     */
    private Object data;

    private int count;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public static LayuiResponseJSONBean getSuccessClientResponseJSONBean() {
        LayuiResponseJSONBean clientResponseJSONBean = new LayuiResponseJSONBean();
        clientResponseJSONBean.setCode("0");
        clientResponseJSONBean.setMsg("");
        return clientResponseJSONBean;
    }

    public static LayuiResponseJSONBean getFailedClientResponseJSONBean() {
        LayuiResponseJSONBean clientResponseJSONBean = new LayuiResponseJSONBean();
        clientResponseJSONBean.setCode(ClientResponseStatusEnum.CLIENT_RESPONSE_FAILED.getStatusCode());
        clientResponseJSONBean.setMsg(ClientResponseStatusEnum.CLIENT_RESPONSE_FAILED.getMessage());
        return clientResponseJSONBean;
    }
    public void setCodeAndMessage(ClientResponseStatusEnum enums) {
        setCode(enums.getStatusCode());
        setMsg(enums.getMessage());
    }

    public final String toJSONString() {
        return JSON.toJSONString(this, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
    }
}

ClientResponseStatusEnum.java

package com.xiaodou.hestia.entity.enums;

/**
 * @author tao
 */
public enum ClientResponseStatusEnum {
    CLIENT_RESPONSE_SUCCESS("SUCCESS", "成功"),
    CLIENT_RESPONSE_FAILED("FAILED", "失败"),
    public String getStatusCode() {
        return statusCode;
    }
    public void setStatusCode(String statusCode) {
        this.statusCode = statusCode;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    private String statusCode;
    private String message;
    ClientResponseStatusEnum(String statusCode, String message) {
        this.statusCode = statusCode;
        this.message = message;
    }
}

MenuController.java

    /**
     * 分页获取菜单列表
     *
     * @param page  当前页
     * @param limit 每页大小
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE + CHARSET)
    @ResponseBody
    public String listMenu(Integer page, Integer limit) {
        LayuiResponseJSONBean layuiResponseJSONBean = new LayuiResponseJSONBean();
        page = page == null ? 1 : page;
        limit = limit == null ? 10 : limit;
        try {
            PageBean pageBean = menuServiceImpl.getMenuList(page, limit);
            layuiResponseJSONBean = LayuiResponseJSONBean.getSuccessClientResponseJSONBean();
            layuiResponseJSONBean.setData(pageBean.getData());
            layuiResponseJSONBean.setCount(pageBean.getCount());
        } catch (Exception e) {
            e.printStackTrace();
            layuiResponseJSONBean = LayuiResponseJSONBean.getFailedClientResponseJSONBean();
            logger.error("获取菜单列表失败:" + e.getMessage());
        }
        return JSONObject.toJSONString(layuiResponseJSONBean);

    }

menu.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    菜单管理
    
    
    



序号 菜单名称 菜单路径 菜单图标 父级菜单 菜单描述 创建时间 更新时间 操作

2.通过分页插件的实现:Mybatis-Plus(本例为springboot项目,前端依然使用layui)

Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

添加Mybatis-Plus的maven依赖

		
			com.baomidou
			mybatis-plus
			2.1.9
		

spring注入mybatis配置分页插件 MybatisPlusConfig.java

import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableTransactionManagement
@Configuration
@MapperScan("com.springboot.first.mapper.*")
public class MybatisPlusConfig {
    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

UserMapper.xml中按照全部查询方式检索即可

    

UserMapper.java中参数为Pageination

    List getUserList(Pagination page);

UserServiceImpl.java调用翻页方法,需要page.setRecords回传给页面

    public Page getUserList(Page page) {
        return page.setRecords(userMapper.getUserList(page));
    }

UserController.java中获取分页后的数据

    @RequestMapping("/toUserList")
    public String toUserList(){
        return "userList";
    }
    @RequestMapping("/getUserList")
    @ResponseBody
    public String getUserList(Integer page,Integer limit){
        LayuiResponseJSONBean layuiResponseJSONBean;
        page = page == null ? 1 : page;
        limit = limit == null ? 10 : limit;
        Page pages = new Page(page,limit);
        Page userList = userService.getUserList(pages);
        List list = userList.getRecords();
        //总数
        Integer total = userList.getTotal();
        layuiResponseJSONBean = LayuiResponseJSONBean.getSuccessClientResponseJSONBean();
        layuiResponseJSONBean.setCount(total);
        layuiResponseJSONBean.setData(list);
        return layuiResponseJSONBean.toJSONString();
    }

userList.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    
    
    用户列表
    


序号 登陆名 密码 用户昵称 年龄 性别

请求列表:

mybatis分页查询_第1张图片

控制台打印sql如下:

mybatis分页查询_第2张图片

你可能感兴趣的:(框架,java)