Mybatis分页查询

特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过。如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/mao2080/

1、View层代码

以js为例,主要是参数传递,查询条件必须以search_打头

/**
 * Ajax请求
 * params url 请求的url
 * params params 参数(json类型,如:{userName:'admin', email:'mao2080@sina.com'})
 * params successCallBack 自定义函数-成功时返回
 * params errorCallBack 自定义函数-失败时返回
 */
var ajaxRequest = function(url, params, successCallBack, errorCallBack){
    $.ajax({
        url:url,
        data:params,
        type:"post",
        dataType:"json",
        async:true,
        success:function(res){
            if(res.success || res.code == 200){
                successCallBack(res);
            }else{
                errorCallBack(res);
            }
        },
        error:function(res){
            alert("请求失败...");
        },
    });
};

ajaxRequest("dbController/queryUserList", {
        search_currPage:1,
        search_pageSize:2,
        search_id:"6",
        search_name:"2",
    }, function(res) {
    //do something
    }
}

 

 

2、Controller层代码

 

//注意要继承BaseController
@Controller
@RequestMapping("/dbController")
public class DBController {

    @Autowired
    private CommonService commonService;

    @RequestMapping("/queryUserList")
    @ResponseBody
    public ResObject queryUserList(HttpServletRequest request) {
        try {
            Pagenation data = this.commonService.queryUserList(this.getParameters(request));
            return new ResObject(data);
        } catch (Exception e) {
            loger.error(e);
            return new ResObject(201, "查询失败";
        }
    }

  /**
     * 
     * 描述:获取分页参数
     * @author mao2080@sina.com
     * @created 2017年4月18日 下午4:24:34
     * @since 
     * @param request
     * @return
     */
    protected Map<String, String> getParameters(HttpServletRequest request){
        Map<String, String> paramsMap = new HashMap<String, String>(); 
        Enumeration<?> paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String paramName = (String) paramNames.nextElement();
            if(StringUtils.isBlank(paramName)){
                continue;
            }
            if(paramName.startsWith(SystemConstants.SEARCH_WORD)){
                String[] paramValues = request.getParameterValues(paramName);
                if(paramValues.length == 1) {
                    paramsMap.put(paramName, paramValues[0]);
                }else{
                    paramsMap.put(paramName, "");
                }
            }
        }
        return paramsMap;
    }
}

 

3、Service层接口及实现

/**
 * 
 * 项目名称:---
 * 模块名称:用户模块
 * 功能描述:
 * 创建人: mao2080@sina.com
 * 创建时间: 2017年1月18日 下午6:40:24
 * 修改人: mao2080@sina.com
 * 修改时间: 2017年1月18日 下午6:40:24
 */
public interface CommonService {
    /**
    * 
    * 描述:分页查询
    * @author mao2080@sina.com
    * @created 2017年4月18日 下午4:57:13
    * @since 
    * @param paramsMap 查询条件
    * @return Pagenation
    */
    public Pagenation queryUserList(Map<String, String> paramsMap);
}
/** * * 项目名称:--- * 模块名称:用户模块 * 功能描述:测试模块实现类 * 创建人: mao2080@sina.com * 创建时间: 2017年1月18日 下午6:44:18 * 修改人: mao2080@sina.com * 修改时间: 2017年1月18日 下午6:44:18 */ @Service("commonService") public class CommonServiceImpl implements CommonService { @Autowired private CommonMapper commMapper; /** * * 描述:获取列表 * @author mao2080@sina.com * @created 2017年4月1日 上午10:27:47 * @since * @return Pagenation*/ @Override public Pagenation queryUserList(Map<String, String> paramsMap){ Pagenation page = new Pagenation(paramsMap); page.setRecordTotal(this.commMapper.queryCount(paramsMap)); page.setList(this.commMapper.queryUserList(paramsMap)); return page; }
}
/** * * 项目名称:--- * 模块名称:公共模块 * 功能描述:oracle CRUD例子 * 创建人: mao2080@sina.com * 创建时间:2017年4月1日 上午10:40:33 * 修改人: mao2080@sina.com * 修改时间:2017年4月1日 上午10:40:33 */ public interface CommonMapper { /** * * 描述:查询列表 * @author mao2080@sina.com * @created 2017年4月1日 上午10:40:27 * @since * @return user列表 */ public List<User> queryUserList(Map<String, String> paramsMap);
}

 

4、Mapper.xml配置文件

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
 3 <mapper namespace="com.common.mapper.ormapper.CommonMapper" >
 4   <!-- 用户分页查询-条件拼接 -->
 5   <sql id="query_user_where">
 6       <where>
 7           <if test="search_id != null"> id like '%'||#{search_id,jdbcType=VARCHAR}||'%'</if>
 8           <if test="search_name != null"> and name = #{search_name,jdbcType=VARCHAR}</if>
 9       </where>
10   </sql>
11   <!-- 用户分页查询-查询条数 -->
12   <select id="queryCount" resultType="Integer" parameterType="Map">
13            select count(1) from test
14            <include refid="query_user_where"/>
15   </select>
16   <!-- 用户分页查询-查询记录 -->
17   <select id="queryUserList" parameterType="Map" resultType="com.common.entity.User">
18         ${PAGE_SQL_STA}
19         select * from test
20         <include refid="query_user_where"/>
21         ${PAGE_SQL_END}
22   </select>
23 </mapper>

5、Pagenation对象

package com.common.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Map;

/**
 * 
 * 项目名称:---
 * 模块名称:常用工具
 * 功能描述:分页对象
 * 创建人: mao2080@sina.com
 * 创建时间:2017年4月18日 下午4:02:27
 * 修改人: mao2080@sina.com
 * 修改时间:2017年4月18日 下午4:02:27
 */
public class Pagenation implements Serializable {

    /**序列*/
    private static final long serialVersionUID = 4542617637761955078L;

    /**当前页*/
    private int currPage = 1;
    
    /**每页大小*/
    private int pageSize = 10;
    
    /**总页数*/
    private int pageTotal;
    
    /**总条数*/
    private int recordTotal = 0;
    
    /**前一页*/
    private int prevPage;
    
    /**下一页*/
    private int nextPage;
    
    /**第一页*/
    private int firstPage = 1;
    
    /**最后一页*/
    private int lastPage;
    
    /**每页的内容*/
    private Object list;
    
    /**
     * 
     * 描述:构造函数
     * @author mao2080@sina.com
     * @created 2017年4月18日 下午3:57:01
     * @since
     */
    public Pagenation() {
        super();
    }

    /**
     * 
     * 描述:构造函数
     * @author mao2080@sina.com
     * @created 2017年4月18日 下午3:56:57
     * @since 
     * @param currentPage 当前页
     * @param pageSize 每页大小
     */
    public Pagenation(int currPage, int pageSize) {
        super();
        this.currPage = currPage;
        this.pageSize = pageSize;
    }
    
    /**
     * 
     * 描述:构造函数
     * @author mao2080@sina.com
     * @created 2017年4月18日 下午7:52:27
     * @since 
     * @param paramsMap 条件
     * @throws BusinessException 
     */
    public Pagenation(Map<String, String> paramsMap) {
        super();
        this.currPage = Pagenation.toInteger(paramsMap.get("search_currPage"), 1);
        this.pageSize = Pagenation.toInteger(paramsMap.get("search_pageSize"), this.pageSize);
        paramsMap.put("PAGE_SQL_STA", "select * from ( select temp_.*, rownum rn from (");
        paramsMap.put("PAGE_SQL_END", ") temp_ where rownum<="+this.currPage*this.pageSize+") where rn>"+(this.currPage-1)*this.pageSize);
    }

    /**
     * 
     * 描述:设置总条数,默认为0
     * @author mao2080@sina.com
     * @created 2017年4月18日 下午3:50:28
     * @since 
     * @param recordTotal
     */
    public void setRecordTotal(int recordTotal) {
        this.recordTotal = recordTotal;
        this.calculate();
    }

    /**
     * 
     * 描述:设置分页内容
     * @author mao2080@sina.com
     * @created 2017年4月18日 下午3:50:09
     * @since 
     * @param list 内容
     */
    public void setList(Object list) {
        this.list = list == null?new ArrayList<Object>():list;
    }

    /**
     * 
     * 描述:设置其他参数
     * @author mao2080@sina.com
     * @created 2017年4月18日 下午3:53:54
     * @since
     */
    public void calculate() {
        this.pageTotal = this.recordTotal % this.pageSize > 0 ? this.recordTotal / this.pageSize + 1 : this.recordTotal / this.pageSize;
        this.firstPage = 1;
        this.lastPage = this.pageTotal;
        if (this.currPage > 1) {
            this.prevPage = this.currPage - 1;
        } else {
            this.prevPage = this.firstPage;
        }
        if (this.currPage < this.lastPage) {
            this.nextPage = this.currPage + 1;
        } else {
            this.nextPage = this.lastPage;
        }
    }

    public int getCurrPage() {
        return currPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public int getPageTotal() {
        return pageTotal;
    }

    public int getRecordTotal() {
        return recordTotal;
    }

    public int getPrevPage() {
        return prevPage;
    }

    public int getNextPage() {
        return nextPage;
    }

    public int getFirstPage() {
        return firstPage;
    }

    public int getLastPage() {
        return lastPage;
    }

    public Object getList() {
        return list;
    }
    
    /**
     * 
     * 描述:转Integer(用于不严格转换,比如接收可以默认为空的参数)
     * @author mao2080@sina.com
     * @created 2017年4月18日 下午4:46:20
     * @since 
     * @param object 要转的对象
     * @param defaultVal 默认值
     * @return
     */
    public static Integer toInteger(Object object, Integer defaultVal){
        try {
            return Integer.valueOf(object.toString());
        } catch (Exception e) {
            return defaultVal;
        }
    }

}

 

6、ResObject对象

package com.common.beans;

import java.io.Serializable;
/**
 * 
 * 项目名称:---
 * 模块名称:常用工具
 * 功能描述:返回结果
 * 创建人: mao2080@sina.com
 * 创建时间:2017年3月24日 下午4:21:58
 * 修改人: mao2080@sina.com
 * 修改时间:2017年3月24日 下午4:21:58
 */
public class ResObject implements Serializable{
    
    /**序列号*/
    private static final long serialVersionUID = 589903502110209046L;

    /**返回代码*/
    private int code = 200;
    
    /**返回信息*/
    private String desc;
    
    /**返回数据*/
    private Object data;

    /**
     * 
     * 构建函数
     * @author mao2080@sina.com
     * @created 2017年3月24日 下午4:25:23
     * @since
     */
    public ResObject() {
        
    }
    
    /**
     * 
     * 描述:构造函数
     * @author mao2080@sina.com
     * @created 2017年4月18日 下午3:32:26
     * @since 
     * @param data 数据
     */
    public ResObject(Object data) {
        super();
        this.data = data;
    }/**
     * 
     * 构建函数
     * @author mao2080@sina.com
     * @created 2017年3月24日 下午4:25:35
     * @since 
     * @param code 返回代码
     * @param desc 返回信息
     */
    public ResObject(int code, String desc) {
        super();
        this.code = code;
        this.desc = desc;
    }

    /**
     * 
     * 构建函数
     * @author mao2080@sina.com
     * @created 2017年3月24日 下午4:25:39
     * @since 
     * @param code 返回代码
     * @param desc 返回信息
     * @param data 返回数据
     */
    public ResObject(int code, String desc, Object data) {
        super();
        this.code = code;
        this.desc = desc;
        this.data = data;
    }

    public Object getData() {
        return data;
    }

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

    public int getCode() {
        return code;
    }

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

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

}

 

7、User对象

package com.common.entity;

import java.io.Serializable;

/**
 * 
 * 项目名称:---
 * 模块名称:公共模块
 * 功能描述:用户实体
 * 创建人: mao2080@sina.com
 * 创建时间:2017年3月31日 下午3:34:41
 * 修改人: mao2080@sina.com
 * 修改时间:2017年3月31日 下午3:34:41
 */
public class User implements Serializable{
    
    /**序列*/
    private static final long serialVersionUID = 403630406019480531L;

    /**用户标识*/
    private String id;
    
    /**用户名称*/
    private String name;
    
    /**密码*/
    private String password;
    
    /**邮件*/
    private String email;

    public User() {
        
    }
    
    public User(String id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public User(String id, String name, String password, String email) {
        super();
        this.id = id;
        this.name = name;
        this.password = password;
        this.email = email;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}
 
posted @ 2017-04-20 12:00  mao2080  阅读(604)  评论(0编辑  收藏  举报