zoukankan      html  css  js  c++  java
  • JAVA入门[10]-mybatis分页查询

    1.添加分页插件

    在mybatis-generator-config.xml添加plugin节点:

    <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin>

    2.在maven面板重新运行mybatis-generator:generate自动生成了分页相关的内容。

    ProductMapper.java添加了分页查询方法:

    List<Product> selectByExampleWithRowbounds(ProductExample example, RowBounds rowBounds);

    ProductMapper.xml添加了SelectByExampleWithRowbounds节点:

    <select id="selectByExampleWithRowbounds" parameterType="com.data.pojo.ProductExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
    distinct
    </if>
      <include refid="Base_Column_List" />
    from product
    <if test="_parameter != null">
        <include refid="Example_Where_Clause" />
      </if>
      <if test="orderByClause != null">
    order by ${orderByClause}
    </if>
    </select>

    3.测试

    @ContextConfiguration(locations = "classpath:spring/applicationContext.xml")
    @RunWith(SpringJUnit4ClassRunner.class)
    public class ProductDaoTests {
        @Resource
        ProductMapper productMapper;
    
        @Test
        public void test_selectByPrimaryKey(){
            Product product=productMapper.selectByPrimaryKey(1);
            System.out.println(product.getName());
        }
    
        @Test
        public void test_page(){
            int id=2;
            int pageIndex=1,pageSize=3;
            RowBounds rowBounds=new RowBounds((pageIndex-1)*pageSize,pageSize);
    
            List<Product> products=productMapper.selectByExampleWithRowbounds(new ProductExample(),rowBounds);
            if(products==null){
                System.out.println("查询结果为空");
            }else {
                System.out.println("第"+pageIndex+"页,每页大小"+pageSize);
                for(Product p:products){
                    System.out.println("id="+p.getId()+" name="+p.getName());
                }
            }
        }
    }

     源码:http://pan.baidu.com/s/1bpJ2pJp

  • 相关阅读:
    [luoguP2221] [HAOI2012]高速公路(线段树)
    SICP:对数步数内迭代计算幂的函数
    python__tkinter之listbox&button
    C陷阱与缺陷 之 各种知识技巧
    ACM && Find Minimum in Rotated Sorted Array
    windows编程一些小知识
    Linux_C pthread 关于多线程一个简单的程序
    ACM&贪心算法
    Linux_C socket 数据报之client, server.c
    Linux_C socket 数据报之一些辅助函数
  • 原文地址:https://www.cnblogs.com/janes/p/6473626.html
Copyright © 2011-2022 走看看