1. 程式人生 > >mybatis中的oracle和mysql分頁

mybatis中的oracle和mysql分頁

applicationContext.xml

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="mapperLocations" value="classpath:/com/hyrt/yzzd/mapper/*.xml"/>
        <!-- 指定Mapper檔案位置,不同mapper.xml檔案的sql片段可以相互引用 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>


common_sqlMap.xml檔案(分頁)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="common" >
    <sql id="Oracle_Pagination_Head" >
        <if test="startIndex != null and startIndex != null" >
            <![CDATA[ SELECT * FROM ( SELECT A.*, ROWNUM RN FROM (  ]]>
        </if>
    </sql>
    <sql id="Oracle_Pagination_Tail" >
        <if test="startIndex != null and pageSize != null" >
            <![CDATA[ ) A WHERE ROWNUM <= #{startIndex}+#{pageSize} ) WHERE RN > #{startIndex} ]]>
        </if>
    </sql>
	<sql id="Oracle_Pagination_Tesi" >
        <if test="startIndex != null and pageSize != null" >
            <![CDATA[ ) A WHERE ROWNUM <= #{pageNo}*#{pageSize} ) WHERE RN > (#{pageNo}-1)*#{pageSize} ]]>
        </if>
    </sql>
    <sql id="Mysql_Pagination_Head" >
        <if test="startIndex != null and startIndex != null" >
            <![CDATA[ select * from (  ]]>
        </if>
    </sql>
    <sql id="Mysql_Pagination_Tail" >
        <if test="startIndex != null and startIndex != null" >
            <![CDATA[ ) y where 1=1  LIMIT #{startIndex},#{pageSize} ]]>
        </if>
    </sql>
</mapper>

其它xml檔案使用時,直接頭部,尾部加上head及Tesi即可

 <select id="findByPage" resultMap="BaseResultMapVo" parameterType="com.hyrt.yzzd.comm.utils.Page">
	  	<include refid="common.Oracle_Pagination_Head" />
	    	select t.productid, t.status, y.account, y.username, t.money,t.objname,t.createtime ,t.returntime, t.id from 
	    	(select * from yzzd_t_order order by createtime desc) t left join yzzd_t_user y on t.userid= y.id
	    <include refid="Extend_Page_Where" />
	    <include refid="common.Oracle_Pagination_Tesi" />
   </select>