• Mybatis的分页查询


    Mybatis的分页查询

    (1)无条件的分页

    mapper文件配置和Java代码实现

    <!-- 传入的参数类型为map,此时无需使用map.get("key")去获得实际值,只需填入key值便可 -->
        <select id="findByPage" parameterType="map" resultMap="studentMap">
            select id,name,age,sex from student
            limit #{start},#{end}
        </select>
    /*
         * 无条件分页查询
         */
        public List<Student> findByPage(int start,int end)
        {
            SqlSession sqlSession = null;
            try{
                sqlSession = MyBatisUtil.getSqlSession();
                
                Map<String,Object> param = new LinkedHashMap<String,Object>();
                param.put("start",start);
                param.put("end",end);
                
                List<Student> stuList;
                stuList = sqlSession.selectList(Student.class.getName()+".findByPage", param);
                
                System.out.println("添加查询成功");
                
                return stuList;
            }catch(Exception e){
                e.printStackTrace();
                throw e;
            }finally{
                MyBatisUtil.closeSqlSession();
            }
        }

    (2)有条件的分页

    mapper文件配置和Java代码实现

    <select id="findByPageAndRequest" parameterType="map" resultMap="studentMap">
            select id,name,age,sex from student
            where name like #{name}
            limit #{start},#{end}
        </select>
    /*
         * 有条件分页查询
         */
        public List<Student> findByPageAndRequest(String name,int start,int end)
        {
            SqlSession sqlSession = null;
            try{
                sqlSession = MyBatisUtil.getSqlSession();
                Map<String,Object> params = new LinkedHashMap<String,Object>();
                //当sql的条件有模糊匹配时,参数需前后带上%
                params.put("name", "%"+name+"%");
                params.put("start", start);
                params.put("end", end);
                
                List<Student> stuList;
                stuList = sqlSession.selectList(Student.class.getName()
                        +".findByPageAndRequest", params);
                
                System.out.println("添加查询成功");        
                return stuList;
            }catch(Exception e){
                e.printStackTrace();
                throw e;
            }finally{
                MyBatisUtil.closeSqlSession();
            }
        }
  • 相关阅读:
    shell脚本编程-重定向
    web安全-剖析_基础构架剖析
    shell脚本编程-函数
    shell脚本编程-循环
    web安全-入侵基础
    shell脚本编程-检查和测试
    shell脚本编程-特殊字符
    shell脚本编程-计算方式
    python例子-抓取网站IP列表
    linux问题-APR not Found
  • 原文地址:https://www.cnblogs.com/linhongwenBlog/p/12772586.html
Copyright © 2020-2023  润新知