zoukankan      html  css  js  c++  java
  • mybatis的学习4______分页的实现

    分页的实现此处有两种思路:

    1. 使用SQL的 limit ?,?  

    2.使用RowBounds

    方式一 (步骤:):

    1.dao层中UserMapper接口的编写:

       //分页的sql实现
        List<User> getUserLimit(Map<String,Integer> map);

    2.dao层中UserMapper接口的userMapper.xml编写

     <select id="getUserLimit" parameterType="Map" resultType="User">
            select * from user limit #{indexStart},#{pageSize}
        </select>

    3.项目测试类的编写:

       @Test
        public void testSqlLimit(){
    
            SqlSessionFactory sqlSessionFactory = MybatisUtils.getSqlSessionFactory();
    
            SqlSession sqlSession = sqlSessionFactory.openSession();
    
    
            int currentPage=1;
            int pageSize=2;
            Map<String, Integer> map = new HashMap<String,Integer>();
            map.put("indexStart",(currentPage-1)*pageSize);
            map.put("pageSize",pageSize);
    
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            List<User> userLimit = mapper.getUserLimit(map);
            System.out.println(userLimit);
        }

    方式二(步骤):

    1.dao层中UserMapper接口的编写:

        //分页的代码实现
        List<User> getUserByRowBounds();

    2.dao层中UserMapper接口的userMapper.xml编写

      <select id="getUserByRowBounds" resultType="User">
            select * from user
        </select>

    3.测试类:

    @Test
        public void testRowBounds(){
            SqlSessionFactory sqlSessionFactory = MybatisUtils.getSqlSessionFactory();
            SqlSession sqlSession = sqlSessionFactory.openSession();
    
            int currentPage=2;
            int pageSize=2;
            RowBounds rowBounds = new RowBounds((currentPage - 1) * pageSize, pageSize);
            //传进2参数
            //不能用getMapper
            List<Object> user = sqlSession.selectList("com.xbf.dao.UserMapper.getUserByRowBounds", null, rowBounds);
            System.out.println(user);
    
        }
  • 相关阅读:
    最常见VC++编译错误信息集合
    网站运营最全总结
    KdPrint/DbgPrint and UNICODE_STRING/ANSI_STRING
    poj 2155 matrix
    【hdu2955】 Robberies 01背包
    【hdu4570】Multi-bit Trie 区间DP
    2014 SCAU_ACM 暑期集训
    qpython 读入数据问题: EOF error with input / raw_input
    【转】Python version 2.7 required, which was not found in the registry
    华农正方系统 登录地址
  • 原文地址:https://www.cnblogs.com/xbfchder/p/11237325.html
Copyright © 2011-2022 走看看