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);
    
        }
  • 相关阅读:
    android ART hook
    Bind Enum to ListControl
    注意WPF中绑定使用的是引用类型
    Android开发第2篇
    Android开发第1篇
    Extension method for type
    DB2实用命令记录
    TDD三大定律
    【InstallShield】 为什么卸载后有的文件没有删掉
    GAC write failed when upgrade with InstallShield
  • 原文地址:https://www.cnblogs.com/xbfchder/p/11237325.html
Copyright © 2011-2022 走看看