mybatis的分页如何实现的

abc395015721 2014-07-17 04:15:31
以前用的是hibernate来对数据库进行处理,现在换了架构了,改用mybatis,我感觉用起来还是挺顺手的,现在正在理解他的实现过程,现在遇到分页这个难题了,就大神帮忙解释下
接口中的方法是这样的
 List<T> selectByExampleWithBLOBs(FilterExample example);

List<T> selectByExampleWithRowbounds(FilterExample example, RowBounds rowBounds);
xml中的代码是这样的
  <select id="selectByExample" resultMap="BaseResultMap" parameterType="com.excenergy.edba.filter.FilterExample" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Wed May 21 13:11:55 CST 2014.
-->
select
<if test="distinct" >
distinct
</if>
<include refid="Base_Column_List" />
from LOGIN_LOG
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null" >
order by ${orderByClause}
</if>
</select>
  <select resultMap="BaseResultMap" parameterType="com.excenergy.edba.filter.FilterExample" id="selectByExampleWithRowbounds" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Wed May 21 13:11:55 CST 2014.
-->
select
<if test="distinct" >
distinct
</if>
<include refid="Base_Column_List" />
from LOGIN_LOG
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null" >
order by ${orderByClause}
</if>
</select>
<sql id="Example_Where_Clause" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Wed May 21 13:11:55 CST 2014.
-->
<where >
<foreach collection="oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
<trim prefix="(" suffix=")" prefixOverrides="and" >
<foreach collection="criteria.criteria" item="criterion" >
<choose >
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
这两段xml代码感觉没什么区别啊,为什么后者能实现分页功能
...全文
15960 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_34199677 2016-11-11
  • 打赏
  • 举报
回复
[quote=引用 7 楼 qq_15804787 的回复:] 咱俩的问题一样啊 你解决了没? 能不能给我说下咋解决
qq_34199677 2016-11-11
  • 打赏
  • 举报
回复
我跟你的一样啊 7楼
薄荷初夏 2016-04-25
  • 打赏
  • 举报
回复
大神,我加了一个 <select id="selectPageByExample" resultMap="BaseResultMap" parameterType="cn.it.exam.pojo.ExUserExample" > select <if test="distinct" > distinct </if> <include refid="Base_Column_List" /> from ex_user <if test="_parameter != null" > <include refid="Example_Where_Clause" /> </if> <if test="orderByClause != null" > order by ${orderByClause} </if> limit #{beginLine},#{endLine} </select> 为什么报下面的错啊 HTTP Status 500 - Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'distinct' not found. Available parameters are [0, param1, param2, param3, endLine, beginLine]
丶Nelson 2014-07-18
  • 打赏
  • 举报
回复
suciver 2014-07-18
  • 打赏
  • 举报
回复
引用 4 楼 abc395015721 的回复:
[quote=引用 2 楼 suciver 的回复:] xml里面并没有使用分页的sql,只是后者所调用的List<T> selectByExampleWithRowbounds(FilterExample example, RowBounds rowBounds);这里面的RowBounds启了作用,这个RowBounds在内部的实现其实是用jdbc的ResultSet的游标分页,效率不高,也就是当RowBounds的offset和limit有赋值时,mybatis内部在得到jdbc的ResultSet的对象rs时用rs的游标定位到offset的位置,只处理limit条记录。并没有在sql中的语句中进行分页,也就是说其实他查询的还是符合条件的全部数据,只是利用游标进行定位了,这样的方式不建议使用,一旦数据量大的时候,使用游标分页是极费性能的。最好的还是在xml里面的sql中使用分页关键字来进行分页
大神,那照目前运行来看,是不是说在接口参数中设置了RowBounds属性,xml代码就会自动进行分页操作,还是需要在配置文件中进行配置?[/quote] 我上面已经说了RowBounds可以实现分页,他用的是游标分页,数据量大的时候效率低,如果楼主没多大的数据量可以用RowBounds。如果数据量大了还是在xml里面的sql语句上用分页关键字去做
abc395015721 2014-07-18
  • 打赏
  • 举报
回复
引用 2 楼 suciver 的回复:
xml里面并没有使用分页的sql,只是后者所调用的List<T> selectByExampleWithRowbounds(FilterExample example, RowBounds rowBounds);这里面的RowBounds启了作用,这个RowBounds在内部的实现其实是用jdbc的ResultSet的游标分页,效率不高,也就是当RowBounds的offset和limit有赋值时,mybatis内部在得到jdbc的ResultSet的对象rs时用rs的游标定位到offset的位置,只处理limit条记录。并没有在sql中的语句中进行分页,也就是说其实他查询的还是符合条件的全部数据,只是利用游标进行定位了,这样的方式不建议使用,一旦数据量大的时候,使用游标分页是极费性能的。最好的还是在xml里面的sql中使用分页关键字来进行分页
大神,那照目前运行来看,是不是说在接口参数中设置了RowBounds属性,xml代码就会自动进行分页操作,还是需要在配置文件中进行配置?
Joyce-Luo 2014-07-17
  • 打赏
  • 举报
回复
limit #{start}, #{stop}
suciver 2014-07-17
  • 打赏
  • 举报
回复
xml里面并没有使用分页的sql,只是后者所调用的List<T> selectByExampleWithRowbounds(FilterExample example, RowBounds rowBounds);这里面的RowBounds启了作用,这个RowBounds在内部的实现其实是用jdbc的ResultSet的游标分页,效率不高,也就是当RowBounds的offset和limit有赋值时,mybatis内部在得到jdbc的ResultSet的对象rs时用rs的游标定位到offset的位置,只处理limit条记录。并没有在sql中的语句中进行分页,也就是说其实他查询的还是符合条件的全部数据,只是利用游标进行定位了,这样的方式不建议使用,一旦数据量大的时候,使用游标分页是极费性能的。最好的还是在xml里面的sql中使用分页关键字来进行分页
gaofuqi 2014-07-17
  • 打赏
  • 举报
回复
这xml配置没有分页的功能,会不会有专门的拦截器处理分页了,仔细看一下整个工程的代码。

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧