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);
    
        }
  • 相关阅读:
    关于c++中 get 和getline
    使用参数初始化表来实现对数据成员的初始化
    关于c++中运算符重载
    关于c++的友元函数
    C#控制台程序点击后暂停工作
    hhgame
    记字符编码与转义符的纠缠
    如何向非技术人(程序猿)解释SQL注入?
    记VS2013并行编译导致出错的解决过程
    记32位Oracle客户端登录报12560协议适配器错误的解决办法
  • 原文地址:https://www.cnblogs.com/xbfchder/p/11237325.html
Copyright © 2011-2022 走看看