码迷,mamicode.com
首页 > 其他好文 > 详细

Mybatis-ThreadLocal分页

时间:2015-05-26 10:29:39      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

mybatis-分页显示数据 

转载原文  http://www.cnblogs.com/yangw/p/3315398.html

分页用到的两个实体类

package com.yangwei.shop.entity;

 

/**

 * 注意  getsetremove 方法与一般的实体类的不同

*/

 

public classSystemContext {

 

    private static ThreadLocal<Integer> pageSize = new ThreadLocal<Integer>();

    private static ThreadLocal<Integer> pageIndex = new ThreadLocal<Integer>();

    private static ThreadLocal<Integer> pageOffset = new ThreadLocal<Integer>();

    /**

     * 升序还是降序

     */

    private static ThreadLocal<String> order = new ThreadLocal<String>();

    /**

     * 根据那个字段排序

     */

    private static ThreadLocal<String> sort = new ThreadLocal<String>();

   

   

    public static String getOrder() {

        return order.get();

    }

 

    public static void setOrder(String _order) {

        order.set(_order);

    }

   

    public static void removeOrder() {

        order.remove();

    }

 

    public static String getSort() {

        return sort.get();

    }

 

    public static void setSort(String _sort) {

        sort.set(_sort);

    }

   

    public static void removeSort() {

        sort.remove();

    }

 

    public static int getPageOffset() {

        return pageOffset.get();

    }

 

    public static void setPageOffset(int _pageOffset) {

        pageOffset.set(_pageOffset);

    }

   

    public static void removePageOffset() {

        pageOffset.remove();

    }

 

    public static void setPageSize(int _pageSize) {

        pageSize.set(_pageSize);

    }

   

    public static int getPageSize() {

        return pageSize.get();

    }

   

    public static void removePageSize() {

        pageSize.remove();

    }

   

    public static void setPageIndex(int _pageIndex) {

        pageIndex.set(_pageIndex);

    }

   

    public static int getPageIndex() {

        return pageIndex.get();

    }

   

    public static void removePageIndex() {

        pageIndex.remove();

    }

}

package com.yangwei.shop.entity;

 

import java.util.List;

 

public class Pager<E> {

    /**

     * 第几页

     */

    private int pageIndex;

    /**

     * 每页显示多少条

     */

    private int pageSize;

    /**

     * 分页的开始值

     */

    private int pageOffset;

    /**

     * 总共多少条记录

     */

    private int totalRecord;

    /**

     * 总共多少页

     */

    private int totalPage;

    /**

     * 放置具体数据的列表

     */

    private List<E> datas;

   

    public int getPageIndex() {

        return pageIndex;

    }

    public void setPageIndex(int pageIndex) {

        this.pageIndex = pageIndex;

    }

    public int getPageSize() {

        return pageSize;

    }

    public void setPageSize(int pageSize) {

        this.pageSize = pageSize;

    }

    public int getTotalRecord() {

        return totalRecord;

    }

    public void setTotalRecord(int totalRecord) {

        this.totalRecord = totalRecord;

    }

    public int getTotalPage() {

        return totalPage;

    }

    public void setTotalPage(int totalPage) {

        this.totalPage = totalPage;

    }

    public List<E> getDatas() {

        return datas;

    }

    public void setDatas(List<E> datas) {

        this.datas = datas;

    }

    public int getPageOffset() {

        return pageOffset;

    }

    public void setPageOffset(int pageOffset) {

        this.pageOffset = pageOffset;

    }

   

}

接下来就是真正的分页了,  mybatis中mapper.xml配置文件中的两条查询语句如下

<!--多个参数的形式,需要使用map来存储-->

    <select id="find" parameterType="map" resultType="User">

        <!--只要是#{}这种形式的,都会以?形式替代,对应字符串形式,都会加上引号-->

        <!--只要是${}这种形式的,不会用?形式替代,传过来什么,就什么替代-->

        select * from t_user where

            ( username like #{name} or nickname like #{name} )

            order by ${sort} ${order}

        limit #{pageOffset},#{pageSize}

       

        <!--

        对应的sql便是

        select * from t_user where (username like ? or nickname like ?)

            order by id desc limit ? ?

        -->

    </select>

    <select id="count" parameterType="map" resultType="int">

        select count(*) from t_user where

            ( username like #{name} or nickname like #{name} )

       

    </select>

java代码实现分页

/**

     * 分页显示

     */

    public static Pager<User> testFind(String name){

        int pageOffset=SystemContext.getPageOffset();

        int pageSize=SystemContext.getPageSize();

        String order=SystemContext.getOrder();

        String sort=SystemContext.getSort();

        Pager<User> pages=new Pager<User>();

        SqlSession session=null;

       

        try {

            session = MyBatisUtil.createSession();

            Map<String,Object> map=new HashMap<String,Object>();

            //使用like模糊查询的时候,注意百分号加在这里

            map.put("name", "%"+name+"%");

            map.put("pageOffset", pageOffset);

            map.put("pageSize", pageSize);

            map.put("order", order);

            map.put("sort", sort);

            List<User> users=session.selectList(User.class.getName()+".find", map);

       

            pages.setDatas(users);

            pages.setPageOffset(pageOffset);

            pages.setPageSize(pageSize);

           

            int count=session.selectOne(User.class.getName()+".count", map);

            pages.setTotalRecord(count);

            

            //所有的数据都存到了Pager对象中

        } finally{

            MyBatisUtil.closeSession(session);

        }

        return pages;

       

    }

测试上面分页代码

       SystemContext.setPageOffset(0);

            SystemContext.setPageSize(15);

            SystemContext.setOrder("desc");

            SystemContext.setSort("id");

           

            Pager<User> ps = testFind("zhang");

            System.out.println(ps.getTotalRecord());

            for(User u: ps.getDatas()){

                System.out.println(u.getNickname());

            }

 

Mybatis-ThreadLocal分页

标签:

原文地址:http://www.cnblogs.com/cangqiongbingchen/p/4529825.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!