Improvements to the ibatis paging function (2)

Source: Internet
Author: User

Tell handleresults not to paging (the SQL statement we assembled already sets the query result as a paging result). Here we introduce the database dialect interface dialect similar to hibenate. The Code is as follows:

Package com. Aladdin. Dao. dialect;

Public interface dialect {

Public Boolean supportslimit ();

Public String getlimitstring (string SQL, Boolean hasoffset );

Public String getlimitstring (string SQL, int offset, int limit );
}

 

The following is the MySQL Implementation of the dialect interface:

Package com. Aladdin. Dao. dialect;

Public class mysqldialect implements dialect {

Protected static final string SQL _end_delimiter = ";";

Public String getlimitstring (string SQL, Boolean hasoffset ){
Return new stringbuffer (SQL. Length () + 20). append (TRIM (SQL). append (
Hasoffset? "Limit ?,? ":" Limit? ")
. Append (SQL _end_delimiter). tostring ();
}

Public String getlimitstring (string SQL, int offset, int limit ){
SQL = trim (SQL );
Stringbuffer sb = new stringbuffer (SQL. Length () + 20 );
SB. append (SQL );
If (Offset> 0 ){
SB. append ("Limit"). append (offset). append (','). append (Limit)
. Append (SQL _end_delimiter );
} Else {
SB. append ("Limit"). append (Limit). append (SQL _end_delimiter );
}
Return sb. tostring ();
}

Public Boolean supportslimit (){
Return true;
}

Private string trim (string SQL ){
SQL = SQL. Trim ();
If (SQL. endswith (SQL _end_delimiter )){
SQL = SQL. substring (0, SQL. Length ()-1
-SQL _end_delimiter.length ());
}
Return SQL;
}

}

The next step is to inject limitsqlexecutor into ibatis. We use ibatis through spring, so we execute the injection in our Dao base class, the Code is as follows:

Package com. Aladdin. Dao. ibatis;

Import java. Io. serializable;
Import java. util. List;

Import org. springframework. Orm. objectretrievalfailureexception;
Import org. springframework. Orm. ibatis. Support. sqlmapclientdaosupport;

Import com. Aladdin. Dao. ibatis. Ext. limitsqlexecutor;
Import com. Aladdin. domain. baseobject;
Import com. Aladdin. util. reflectutil;
Import com. ibatis. sqlmap. Client. sqlmapclient;
Import com.ibatis.sqlmap.engine.exe cution. sqlexecutor;
Import com. ibatis. sqlmap. Engine. impl. extendedsqlmapclient;

Public abstract class basedaoibatis extends sqlmapclientdaosupport {

Private sqlexecutor;

Public sqlexecutor getsqlexecutor (){
Return sqlexecutor;
}

Public void setsqlexecutor (sqlexecutor ){
This. sqlexecutor = sqlexecutor;
}

Public void setenablelimit (Boolean enablelimit ){
If (sqlexecutor instanceof limitsqlexecutor ){
(Limitsqlexecutor) sqlexecutor). setenablelimit (enablelimit );
}
}

Public void initialize () throws exception {
If (sqlexecutor! = NULL ){
Sqlmapclient = getsqlmapclienttemplate ()
. Getsqlmapclient ();
If (sqlmapclient instanceof extendedsqlmapclient ){
Reflectutil. setfieldvalue (extendedsqlmapclient) sqlmapclient)
. Getdelegate (), "sqlexecutor", sqlexecutor. Class,
Sqlexecutor );
}
}
}

...

}

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.