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 );
}
}
}
...
}