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

来源:互联网 发布:类似傲风 知乎 编辑:程序博客网 时间:2024/05/16 11:22

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);



0 0
原创粉丝点击