mysql mybatis 分頁查詢語句怎么寫
1、親Mybatis是自己寫Sql語句啊,和Hibernate不一樣。
2、如何知道上面的,你還要知道MySql有一個分頁語句叫limit,如:limit(1,10);前面一個參數是起始未知,后面一個是查詢多少個。3、Oracle的分頁方法是嵌套子查詢,需要用到rownum這個屬性Sql Server是Top。
分頁例子:Oracle select * from (select emp.*,rownum rn from emp where rownum<9) where rn>3;MySql select * from emp limit startIndex,maxNum。
跪求大家幫忙,用MyBatis如何寫分頁操作,求相關代碼 謝謝 速度-百
MyBatis是自帶分頁的,只要記得使用MyBatis的sqlsession就可以了。
int offset = 100;int limit = 25;RowBounds rowBounds = new RowBounds(offset, limit);List
如何使用 mybatis 實現分頁
1、親Mybatis是自己寫Sql語句啊,和Hibernate不一樣。
2、如何知道上面的,你還要知道MySql有一個分頁語句叫limit,如:limit(1,10);前面一個參數是起始未知,后面一個是查詢多少個。
3、Oracle的分頁方法是嵌套子查詢,需要用到rownum這個屬性
Sql Server是Top。
分頁例子:
Oracle select * from (select emp.*,rownum rn from emp where rownum3;
MySql select * from emp limit startIndex,maxNum
mybatis怎么實現分頁查詢
Java代碼 package page; public interface Dialect { public boolean supportsLimit(); public String getLimitString(String sql, boolean hasOffset); public String getLimitString(String sql, int offset, int limit); public boolean supportsLimitOffset(); } Java代碼 package page; public class MySQLDialect implements Dialect { protected static final String SQL_END_DELIMITER = ";"; public String getLimitString(String sql, boolean hasOffset) { return new StringBuffer(*() + 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(*() + 20); *(sql); if (offset > 0) { *(" limit ").append(offset).append(',').append(limit) .append(SQL_END_DELIMITER); } else { *(" limit ").append(limit).append(SQL_END_DELIMITER); } return *ng(); } public boolean supportsLimit() { return true; } private String trim(String sql) { sql = *(); if (*th(SQL_END_DELIMITER)) { sql = *ing(0, *() - 1 - SQL_END_*()); } return sql; } public boolean supportsLimitOffset() { // TODO Auto-generated method stub return true; } } Java代碼 package page; import *ties; import *or; import *ql; import *Statement; import *r; import *rce; import *eptor; import *epts; import *tion; import *; import *ure; import *Handler; import *nds; @Intercepts({ @Signature(type = *, method = "query", args = { *, *, *, * }) }) public class OffsetLimitInterceptor implements Interceptor { static int MAPPED_STATEMENT_INDEX = 0; static int PARAMETER_INDEX = 1; static int ROWBOUNDS_INDEX = 2; static int RESULT_HANDLER_INDEX = 3; Dialect dialect; public Object intercept(Invocation invocation) throws Throwable { processIntercept(*s()); return *d(); } void processIntercept(final Object[] queryArgs) { // queryArgs = query(MappedStatement ms, Object parameter, RowBounds // rowBounds, ResultHandler resultHandler) MappedStatement ms = (MappedStatement) queryArgs[MAPPED_STATEMENT_INDEX]; Object parameter = queryArgs[PARAMETER_INDEX]; final RowBounds rowBounds = (RowBounds) queryArgs[ROWBOUNDS_INDEX]; int offset = *set(); int limit = *it(); if (*tsLimit() && (offset != *_ROW_OFFSET || limit != *_ROW_LIMIT)) { BoundSql boundSql = *ndSql(parameter); String sql = *().trim(); if (*tsLimitOffset()) { sql = *itString(sql, offset, limit); offset = *_ROW_OFFSET; } else { sql = *itString(sql, 0, limit); } limit = *_ROW_LIMIT; queryArgs[ROWBOUNDS_INDEX] = new RowBounds(offset, limit); BoundSql newBoundSql = new BoundSql(*figuration(), sql, *ameterMappings(), *ameterObject()); MappedStatement newMs = copyFromMappedStatement(ms, new BoundSqlSqlSource(newBoundSql)); queryArgs[MAPPED_STATEMENT_INDEX] = newMs; } } private MappedStatement copyFromMappedStatement(MappedStatement ms, SqlSource newSqlSource) { Builder builder = new *r(*figuration(), *(), newSqlSource, *CommandType()); *ce(*ource()); *ize(*chSize()); *entType(*tementType()); *erator(*Generator()); *perty(*Property()); *t(*eout()); *terMap(*ameterMap()); *Maps(*ultMaps()); *(*he()); MappedStatement newMs = *(); return newMs; } public Object plugin(Object target) { return *(target, this); } public void setProperties(Properties properties) { String dialectClass = new PropertiesHelper(properties) .getRequiredString("dialectClass"); try { dialect = (Dialect) *e(。
轉載請注明出處華閱文章網 » mybatis分頁語句