|
以前用的是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代码感觉没什么区别啊,为什么后者能实现分页功能 |
|
![]() |
|
![]() |
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中使用分页关键字来进行分页
|
![]() |
limit #{start}, #{stop}
|
![]() |
大神,那照目前运行来看,是不是说在接口参数中设置了RowBounds属性,xml代码就会自动进行分页操作,还是需要在配置文件中进行配置? |
![]() 40分 |
我上面已经说了RowBounds可以实现分页,他用的是游标分页,数据量大的时候效率低,如果楼主没多大的数据量可以用RowBounds。如果数据量大了还是在xml里面的sql语句上用分页关键字去做 |

