zoukankan      html  css  js  c++  java
  • mybatis 之 分页及模糊查询

    1. mybatis中分页有3种方式来实现,通过sql语句(两种传参方式)来实现,通过mybatis 的 Rowbounds 来实现。
    2. 通过(自定义类型)传参 来实现分页:

      映射文件:

      <select id="findListBypage" parameterType="cn.wh.util.PageUtil" resultType="Role">
              select * from t_role limit #{index},#{size}
          </select>

      测试代码:

      /**
           * 通过自定义类型来传参 实现分页功能 需要新建一个类型
           */
          @Test
          public void testPage1(){
              PageUtil pu = new PageUtil();
              pu.setIndex(3);
              pu.setSize(3);
              List<Role> list = session.selectList("cn.wh.mapper.RoleMapper.findListBypage", pu);
              for(Role r:list){
                  System.out.println(r.getName());
              }
          }
    3. 通过map传参实现:

      映射文件:

      <select id="findListBypage" parameterType="map " resultType="Role">
              select * from t_role limit #{index},#{size}
          </select>

      测试代码:

      /**
           * 可以通过map来传参 这样可以不用新建新的类型
           */
          @Test
          public void testPage2(){
              Map<String,Integer> map = new HashMap<String,Integer>();
              map.put("index", 0);
              map.put("size", 3);
              List<Role> list = session.selectList("cn.wh.mapper.RoleMapper.findListBypage", map);
              for(Role r:list){
                  System.out.println(r.getName());
              }
          }
    4. 通过RowBounds来实现分页:

      映射文件:

      <select id="findAll" resultType="Role">
              select * from t_role
          </select>

      测试代码:

      /**
           * 使用rowBounds来实现分页
           */
          @Test
          public void testPage3(){
              //第一个参数 是index,开始下标
              //第二个参数 是size,每页显示记录数
              RowBounds bounds = new RowBounds(3, 3);
              List<Role> list = session.selectList("cn.wh.mapper.RoleMapper.findAll", null,bounds);
              for(Role r:list){
                  System.out.println(r.getName());
              }
          }

      注意:通常情况下使用 Map 传参来实现分页

    5. 模糊查询
      映射文件:
      <select id="selectLike" parameterType="string" resultType="Role">
              select *from t_role where name like #{name}
          </select>

      测试代码:

      /**
           * 模糊查询
           */
          @Test
          public void testLike1(){
              List<Role> list = session.selectList("cn.wh.mapper.RoleMapper.selectLike","%会员");
              for(Role r:list){
                  System.out.println(r.getName());
              }
          }
    6. 第二种方式:
      <select id="selectLike1" parameterType="string" resultType="Role">
              select *from t_role where name like concat(#{name},'%');
          </select>

      测试代码:

      /**
           * 模糊查询
           */
          @Test
          public void testLike2(){
              List<Role> list = session.selectList("cn.wh.mapper.RoleMapper.selectLike1","");
              for(Role r:list){
                  System.out.println(r.getName());
              }
          }

      注意:通常使用第二种方式实现模糊查询:

  • 相关阅读:
    Redhat VNCServer
    Petshop4 的相关文章、下载地址和相关问题
    .Net 2.0 中用 ICallbackEventHandler实现 Ajax无刷新操作
    Silverlight 结合WCF Duplex Service聊天程序出炉
    vsftp的虚拟用户管理
    MySql 内存表使用
    linux 上远程控制
    理解WCF Session笔记
    转载-磁盘管理
    rhel 5 3G以上内存解决方案
  • 原文地址:https://www.cnblogs.com/forever2h/p/6796095.html
Copyright © 2011-2022 走看看