1. 程式人生 > >關於mybatis中對mysql和Oracle資料庫分頁外掛的使用

關於mybatis中對mysql和Oracle資料庫分頁外掛的使用

首先是Oracle資料庫:在mybatis相對應的mapper.xml檔案裡:

<sql id="OracleDialectPrefix">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Nov 30 16:41:13 CST 2017.
    -->
    <if test="page != null">
      select * from ( select row_.*, rownum rownum_ from ( 
    </if>
  </sql>
  <sql id="OracleDialectSuffix">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Thu Nov 30 16:41:13 CST 2017.
    -->
    <if test="page != null">
      <![CDATA[ ) row_ ) where rownum_ > #{page.startRow} and rownum_ <= #{page.startRow} + #{page.pageSize} ]]>
    </if>
  </sql>
在Oracle資料庫中要有兩條sql包著的分頁語句,然後具體用的時候:
 <select id="selectByExampleForCamera" parameterType="com.vrview.ssm.model.example.DeviceExample" resultMap="ResultMapForCamera">
    <include refid="OracleDialectPrefix" />
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from CFG_DEVICE
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
    <include refid="OracleDialectSuffix" />
  </select>
要在查詢語句一前一後。

2、再看msql的分頁

分頁程式碼:

<sql id="MysqlDialectSuffix">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Wed Jun 14 15:39:27 CST 2017.
    -->
    <if test="page != null">
      <![CDATA[ limit #{page.startRow},#{page.pageSize} ]]>
    </if>
  </sql>
用到語句中:
<select id="selectByExampleWithPolicy" parameterType="cn.vrview.sa.model.example.LevDeviceExample" resultMap="BaseResultWithPolicyMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from LEV_DEVICE
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
    <include refid="MysqlDialectSuffix" />
  </select>
只要放在select語句句末就行。