spring3.1.1+mybatis3.1.1, delete, check, change and paging and transaction management _java

Source: Internet
Author: User
Tags stringbuffer

1. [Code]mybatis Global configuration file

<plugins>
< Plugin interceptor = "Com.has.core.page.PaginationInterceptor"/>
</plugins>

2. [Document] Paginationinterceptor.java

@Intercepts ({@Signature (type = Statementhandler. Class, method = "Prepare", args = {Connection. class})}) Class Paginationinterceptor implements Interceptor {@Override public Object intercept (invocation invocation) throws Thro
wable {Statementhandler Statementhandler = (statementhandler) invocation.gettarget ();
Boundsql boundsql = Statementhandler.getboundsql ();
MetaObject Metastatementhandler = Metaobject.forobject (Statementhandler);
Rowbounds rowbounds = (rowbounds) metastatementhandler.getvalue ("Delegate.rowbounds");
if (rowbounds = null | | rowbounds = = rowbounds.default) {return invocation.proceed ();}
Configuration Configuration = (Configuration) metastatementhandler.getvalue ("Delegate.configuration");
Dialect.type databasetype = null; try {databasetype = Dialect.Type.valueOf (Configuration.getvariables (). GetProperty ("dialect"). toUpperCase ()); catch (Exception e) {} if (DatabaseType = = null) {throw new RuntimeException ("The value of the dialect propErty in Configuration.xml are not defined: "+ configuration.getvariables (). GetProperty (" dialect "));
Dialect dialect = null; Switch (databasetype) {case mysql:dialect = new Mysql5dialect (), break, Case oracle:dialect = new Oracledialect (); Bre
AK;
String originalsql = (string) metastatementhandler.getvalue ("Delegate.boundSql.sql"); Metastatementhandler.setvalue ("Delegate.boundSql.sql", Dialect.getlimitstring (Originalsql, Rowbounds.getoffset ()
, Rowbounds.getlimit ()));
Metastatementhandler.setvalue ("Delegate.rowBounds.offset", Rowbounds.no_row_offset);
Metastatementhandler.setvalue ("Delegate.rowBounds.limit", rowbounds.no_row_limit);
return Invocation.proceed (); @Override public Object Plugin (object target) {return Plugin.wrap (target, this);} @Override public void SetProperties (Properties Properties) {
}
}

3. [Document] Dialect.java

/**
* Database dialect definition
*/public
abstract class Dialect {public
static enum Type {
MYSQL, ORACLE
} Public
Abstract String getlimitstring (String sql, int skipresults, int maxresults);

4. [Document] Mysql5dialect.java

/**
* MySQL dialect pagination class */public
class Mysql5dialect extends dialect {
protected static final String Sql_ End_delimiter = ";";
public string getlimitstring (String sql, Boolean Hasoffset) {return
mysql5pagehepler.getlimitstring (SQL,-1,-1) ;
}
public string getlimitstring (string sql, int offset, int limit) {return
mysql5pagehepler.getlimitstring sql, offset, limit);
}
public Boolean supportslimit () {return
true;
}
}

5. [Document] Oracledialect.java

Package com.chyjr.has.core.page.dialect;
/**
* Oracel dialect Paging
* 
/Public
class Oracledialect extends dialect {public
String getlimitstring ( String sql, int offset, int limit) {
//TODO does not implement return
' ";
}
}

6. [Document] Mysql5pagehepler.java

Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern; /** * MySQL Paging tool class/public class Mysql5pagehepler {/** * SQL/public static string getcountstring for total queries (String Queryse lect) {queryselect = Getlinesql (queryselect); int orderindex = Getlastorderinsertpoint (queryselect); int formIndex = GetA
Fterforminsertpoint (Queryselect);
String select = Queryselect.substring (0, Formindex); If the select contains DISTINCT, only the outer layer contains count if (Select.tolowercase (). IndexOf ("select DISTINCT")!=-1 | | queryselect.tolowerc ASE (). IndexOf ("GROUP BY")!=-1} {return to New StringBuffer (Queryselect.length ()). Append ("SELECT COUNT (1) Count from ( "). Append (queryselect.substring (0, Orderindex)). Append (") T "). ToString (); else {return new StringBuffer (Queryselect.length ()). Append ("SELECT COUNT (1) Count"). Append (Queryselect.substring (
Formindex, Orderindex)). ToString (); }/** * Gets the insertion point position of the last order by * @return Returns the position of the last order by insertion point/private static int Getlastorderinsertpoint (String queryselect) {int orderindex = Queryselect.tolowercase (). LastIndexOf ("ORDER by"); if (Orderindex = 1 | |!isbracketcanpar Tnership (Queryselect.substring (Orderindex, Queryselect.length ())) {throw new RuntimeException ("My SQL paging must have an order by
Statement! ");
return orderindex;  /** * Get Paged SQL * * @param offset * offset * @param limit * position * @return Paging SQL/public static string getlimitstring (String queryselect, int offset, int limit) {Queryselect = Getlinesql (queryselect);//String sql = Queryselect.replaceall ("[^\\
s,]+\\. "," "") + "limit" +//Offset + "," + limit;
String sql = queryselect + "Limit" + offset + "," + limit;
return SQL; /** * Transforms the SQL statement into a statement, and each word is separated by 1 spaces * * @param SQL * SQL statement * @return If SQL is null return NULL, return converted SQL/private static String G
Etlinesql (String sql) {return sql.replaceall ("[\ r \ n]", ""). ReplaceAll ("\\s{2,}", "");} /** * Gets the insertion point of the first correct from of SQL * * private static int getafterforminsertpoint (string queryselect) {string regex = "\\s+from\\s
+" ; Pattern PatTern = Pattern.compile (regex, pattern.case_insensitive);
Matcher Matcher = Pattern.matcher (Queryselect);
while (Matcher.find ()) {int fromstartindex = Matcher.start (0);
String Text = queryselect.substring (0, Fromstartindex);
if (isbracketcanpartnership (text)) {return Fromstartindex}}
return 0; /** * Determines whether the bracket "()" matches, and does not determine whether the order is correct * * @param text * To be judged @return if the match returns TRUE, return False/private static Boolean Isbra Cketcanpartnership (String text) {if (Text = null | | (Getindexofcount (Text, ' (')!= getindexofcount (text, ') '))
{return false;}
return true; /** * Gets the number of times a character appears in another string * * @param text * text * @param ch * character/private static int Getindexofcount (String text, Char 
CH) {int count = 0; for (int i = 0; i < text.length (); i++) {count = (Text.charat (i) = ch)? Count + 1:count;}
return count; }
}

2. [Picture] Mybatis.jpg

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.