mybatis-gen自动生成Mapper中加入分页 (MySQL和Oracle)

MySQL较为简单,由于有limit关键字可用,需要定义的是分页起始偏移offset和分页大小size

这里参考:http://www.cnblogs.com/AloneSword/p/3412236.html

在Example类中添加offset和size两个变量,及get/set方法。

再在Mapper中的selectByExample最后添加

    <if test="offset !=0 or size!=0">  
      limit #{offset},#{size}  
    </if>

例如:TestMapper.xml

<select id="selectByExample" resultMap="BaseResultMap" parameterType="com.example.TestExample" >
    select
    <if test="distinct" >
      distinct
    </if>
    <include refid="Base_Column_List" />
    from TEST
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null" >
      order by ${orderByClause}
    </if>
    <if test="offset !=0 or size!=0">  
      limit #{offset},#{size}  
    </if>
</select>


Oracle的就麻烦一点,这里使用Oracle自带的行号rownum,需要定义最大行号maxrow和最小行号minrow

参考Oracle分页语句:http://blog.163.com/yongqi0408@126/blog/static/4251263220087432522770/

在Example类中添加maxrow和minrow两个变量,及get/set方法。

再修改Mapper中的selectByExample如下:

<select id="selectByExample" resultMap="BaseResultMap" parameterType="com.example.TestExample" >
    select * from (
    select t1.*, rownum r from (
    select
    <if test="distinct" >
      distinct
    </if>
    <include refid="Base_Column_List" />
    from TEST
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null" >
      order by ${orderByClause}
    </if>
    ) t1
    <if test="maxrow != 0">
    where rownum &lt;= ${maxrow}
    </if>  
    ) t2
    where r > ${minrow}
</select>

使用方法:

	TestExample example = new TestExample();
	example.createCriteria();
	example.setOrderByClause("TS DESC"); // TS是表中的时间戳列,这里按照时间降序排列
	example.setMaxrow(maxrow);
	example.setMinrow(minrow);
	testMapper.selectByExample(example);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值