mybatis中的oracle和mysql分页

本文介绍了一种在MyBatis中实现Oracle和MySQL数据库分页查询的方法,通过定义通用的SQL片段简化了分页查询的过程。具体展示了如何在applicationContext.xml中配置SqlSessionFactory,并在common_sqlMap.xml文件中定义分页相关的SQL片段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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>



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值