Ibatis implementation of physical level paging compatibility multiple databases (reprint)

Source: Internet
Author: User

Recently when looking at Ibatis, want to do with dynamic SQL to do a distribution. Because when you do the project with the Ibator tool to generate no paging features, only some of our common features. So make changes to the generated code. I see a page of MySQL SQL Server Oracle in a book from the Java Master canon. Implemented as follows:
Mysql-->
First of all, MySQL's paging statement SQL code Select * from user where...  Order by ... limit 10,25

  

Based on the above statements we can modify the Ibator generated code.
First modification User_sqlmap.xml
Pre-Modify XML code <selectId= "Ibatorgenerated_selectbyexample" resultmap= "Ibatorgenerated_baseresultmap" parameterclass= "Com.demo.ibat" Is.beans.UserExample " >Select ID, login_name, PASSWORD from user <isParameterPresent> <includeRefid= "User.ibatorgenerated_example_where_clause" /> <isnotnullProperty= "Orderbyclause" >ORDER BY $orderByClause $ </isNotNull> </isParameterPresent> </select>


Modified XML code<selectId= "Ibatorgenerated_selectbyexample" resultmap= "Ibatorgenerated_baseresultmap" parameterclass= "com.demo.ibatis.b" EANs. Userexample ">Select ID, login_name, PASSWORD from user<isParameterPresent><includeRefid= "User.ibatorgenerated_example_where_clause"/><isnotnullProperty= "Orderbyclause">ORDER BY $orderByClause $</isNotNull>[b]<isnotnullProperty= "Limitclausestart">Limit $limitClauseStart $, $limitClauseCount $</isNotNull>[/b]</isParameterPresent></select>


The important thing is that the black font is added to the original XML. Next Modify Userexample.java
Add two fields to the class and the corresponding Getter/setter method java code protected string limitclausestart;//start parameter protected string limitclausecount;//quantity parameter


Next you can invoke the Selectbyexample () method test through the DAO, good above is the MySQL pagination. SQL Server's paging is more difficult because there are no limit keywords in SQL Server.
Look at SQL Server's paging statement, which of course there are many ways SQL Server people distribute. I use one of these here.
The statements are as follows: SQL code SELECT top page size * from TestTable WHERE (ID. SELECT Top Page size * pages ID from table ORDER by ID) order by ID


The next step is to change the same as the MySQL above
modifying XML XML code<selectId= "Ibatorgenerated_selectbyexample" resultmap= "Ibatorgenerated_baseresultmap" parameterclass= "com.demo.ibatis.b" EANs. Userexample ">[b]<isnotnullProperty= "Limitclausestart">Selete Top $limitClauseCount $ * from user where (id not in (SELECT top $limitClauseStart $ id</isNotNull>[/b]<isnullProperty= "Limitclausestart">Selete *</isNull>From the user from user<isParameterPresent><includeRefid= "User.ibatorgenerated_example_where_clause"/><isnotnullProperty= "Orderbyclause">ORDER BY $orderByClause $ [b]<isnotnullProperty= "Limitclausestart">) Order BY $orderByClause $</isNotNull>[/b]</isNotNull></isParameterPresent></select>


The next one is Oracle, and it's pretty much the same here.
Grammar:
select* from (select RowNum tid,user.* from (SELECT * to User where id>1 order by id desc) user where where rownum<3 5 Where tid>10;
modifying XML, XML code <selectId= "Ibatorgenerated_selectbyexample" resultmap= "Ibatorgenerated_baseresultmap" parameterclass= "Com.demo.ibat" Is.beans.UserExample " >[b] <isnotnullProperty= "Limitclausestart" >select* from (select RowNum tid,user.* from ( </isNotNull>[/b] SELECT * FROM user <isParameterPresent> <includeRefid= "User.ibatorgenerated_example_where_clause" /> <isnotnullProperty= "Orderbyclause" >ORDER BY $orderByClause $ </isNotNull>[b] <isnotnullProperty= "Limitclausestart" ><! [[CDATA] user where where rownum< $limitClauseCount $+ $limitClauseStart $+1) where tid> $limitClauseStart $;] > </isNotNull>[/b] </isParameterPresent> </select>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.