本文整理匯總了Java中org.apache.ibatis.executor.statement.StatementHandler類的典型用法代碼示例。如果您正苦於以下問題:Java StatementHandler類的具體用法?Java StatementHandler怎麽用?Java StatementHandler使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
StatementHandler類屬於org.apache.ibatis.executor.statement包,在下文中一共展示了StatementHandler類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: intercept
import org.apache.ibatis.executor.statement.StatementHandler; //導入依賴的package包/類
/**
* 清空緩存
*/
@Override
public Object intercept(Invocation invocation) throws Throwable {
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
BoundSql boundSql = statementHandler.getBoundSql();
String sql = boundSql.getSql().trim().toLowerCase();
sql=StringUtils.deleteExcessBlank(sql);
String tableName=null;
String[] words=sql.split(" ");
if(ArrayUtils.isNotEmpty(words)){
if(sql.contains("delete")&&words[0].equals("delete")){
tableName=words[2];
}else if(sql.contains("update")&&words[0].equals("update")){
tableName=words[1];
}
if(tableName!=null){
cacheOperator.clean(tableName);
}
}
return invocation.proceed();
}
示例2: intercept
import org.apache.ibatis.executor.statement.StatementHandler; //導入依賴的package包/類
@Override
public Object intercept(Invocation invocation) throws Throwable {
RoutingStatementHandler handler = (RoutingStatementHandler) invocation.getTarget();
StatementHandler delegate = (StatementHandler) ReflectUtil.getFieldValue(handler, "delegate");
BoundSql boundSql = delegate.getBoundSql();
Object obj = boundSql.getParameterObject();
Page page = seekPage(obj);
if (page != null) {
MappedStatement mappedStatement = (MappedStatement) ReflectUtil.getFieldValue(delegate, "mappedStatement");
Connection connection = (Connection) invocation.getArgs()[0];
String sql = boundSql.getSql();
this.setTotalRecord(page, mappedStatement, connection);
String pageSql = this.getPageSql(page, sql);
ReflectUtil.setFieldValue(boundSql, "sql", pageSql);
}
return invocation.proceed();
}
示例3: intercept
import org.apache.ibatis.executor.statement.StatementHandler; //導入依賴的package包/類
@Override
public Object intercept(Invocation invocation) throws Throwable {
// TODO Auto-generated method stub
if (invocation.getTarget() instanceof RoutingStatementHandler) {
StatementHandler statementHandler = (RoutingStatementHandler) invocation.getTarget();
MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler);
MappedStatement mappedStatement = (MappedStatement) metaStatementHandler.getValue("delegate.mappedStatement");
String mapperId = mappedStatement.getId();
String mapperClassName = mapperId.substring(0, mapperId.lastIndexOf("."));
Class<?> mapperClass = Class.forName(mapperClassName);
if (isDaoMapper(mapperClass)&&mapperId.endsWith(".insertUseReturnGeneratedKeys")) {
BaseStatementHandler baseStatementHandler = (BaseStatementHandler) metaStatementHandler.getValue("delegate");
if ("query".equals(invocation.getMethod().getName())) {
return handPreparedStatementHandlerQuery(invocation, baseStatementHandler);
} else if ("prepare".equals(invocation.getMethod().getName())) {
return handPreparedStatementHandlerPrepare(invocation, baseStatementHandler);
}
}
}
return invocation.proceed();
}
示例4: intercept
import org.apache.ibatis.executor.statement.StatementHandler; //導入依賴的package包/類
/**
* 攔截後要執行的方法
*/
@Override
public Object intercept(Invocation invocation) throws Throwable {
Page<?> page = PageThreadLocal.getThreadLocalPage();
if (page == null) {
return invocation.proceed();
}
PageThreadLocal.removeThreadLocalPage();
RoutingStatementHandler handler = (RoutingStatementHandler) invocation.getTarget();
// 通過反射獲取到當前RoutingStatementHandler對象的delegate屬性
StatementHandler delegate = (StatementHandler) ReflectUtil.getFieldValue(handler, "delegate");
BoundSql boundSql = delegate.getBoundSql();
MappedStatement mappedStatement = (MappedStatement) ReflectUtil.getFieldValue(delegate, "mappedStatement");
// 獲取當前要執行的Sql語句,也就是我們直接在Mapper映射語句中寫的Sql語句
String sql = boundSql.getSql();
// 是否查詢總頁數和總數據 默認為TRUE
if (page.getTotalFlag()) {
// 給當前的page參數對象設置總記錄數
this.setTotalRecord(page, mappedStatement, boundSql, sql);
}
String pageSql = this.getPageSql(sql, page);
// 利用反射設置當前BoundSql對應的sql屬性為我們建立好的分頁Sql語句
ReflectUtil.setFieldValue(boundSql, "sql", pageSql);
return invocation.proceed();
}
示例5: doQuery
import org.apache.ibatis.executor.statement.StatementHandler; //導入依賴的package包/類
@Override
public <E> List<E> doQuery(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql)
throws SQLException {
Statement stmt = null;
try {
flushStatements();
Configuration configuration = ms.getConfiguration();
StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameterObject, rowBounds, resultHandler, boundSql);
Connection connection = getConnection(ms.getStatementLog());
stmt = handler.prepare(connection, transaction.getTimeout());
handler.parameterize(stmt);
return handler.<E>query(stmt, resultHandler);
} finally {
closeStatement(stmt);
}
}
示例6: plugin
import org.apache.ibatis.executor.statement.StatementHandler; //導入依賴的package包/類
@Override
public Object plugin(Object target) {
if (target instanceof StatementHandler) {
StatementHandler statementHandler = (StatementHandler) target;
MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler);
String sql = statementHandler.getBoundSql().getSql();
Pager pager = Pager.getAndReset();
if (pager != null && sql.trim().toLowerCase().startsWith("select")) {
String newSql = EasyOrmSqlBuilder.getInstance()
.getActiveDatabase().getDialect()
.doPaging(sql, pager.pageIndex(), pager.pageSize());
metaStatementHandler.setValue("delegate.boundSql.sql", newSql);
}
}
return Plugin.wrap(target, this);
}
示例7: intercept
import org.apache.ibatis.executor.statement.StatementHandler; //導入依賴的package包/類
public Object intercept(Invocation invocation) throws Throwable {
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
MappedStatement mappedStatement = null;
if (statementHandler instanceof RoutingStatementHandler) {
StatementHandler delegate = (StatementHandler) ReflectionUtils.getFieldValue(statementHandler, "delegate");
mappedStatement = (MappedStatement) ReflectionUtils.getFieldValue(delegate, "mappedStatement");
} else {
mappedStatement = (MappedStatement) ReflectionUtils.getFieldValue(statementHandler, "mappedStatement");
}
Object parm = statementHandler.getParameterHandler().getParameterObject();
String sql = statementHandler.getBoundSql().getSql();
String mapperId = mappedStatement.getId();
if (ShardHolder.getInstance().hasShard(mapperId)) {
ShardObject shardObject = ShardHolder.getInstance().getShardObject(mapperId);
String targetSql = ShardParser.getInstance().parse(sql, parm, shardObject);
if (!sql.equals(targetSql)) {
ReflectionUtils.setFieldValue(statementHandler.getBoundSql(), "sql", targetSql);
}
}
return invocation.proceed();
}
示例8: doQueryCursor
import org.apache.ibatis.executor.statement.StatementHandler; //導入依賴的package包/類
@Override
protected <E> Cursor<E> doQueryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds, BoundSql boundSql) throws SQLException {
Configuration configuration = ms.getConfiguration();
StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, null, boundSql);
Statement stmt = prepareStatement(handler, ms.getStatementLog());
return handler.<E>queryCursor(stmt);
}
示例9: getQueryCount
import org.apache.ibatis.executor.statement.StatementHandler; //導入依賴的package包/類
/**
* 獲取 SQL 查詢結果的記錄數。
*/
private static int getQueryCount(final Configuration configuration, final StatementHandler statementHandler)
throws SQLException {
long start = System.currentTimeMillis();
String originSql = statementHandler.getBoundSql().getSql();
String countSql = SqlUtils.generateCountSql(originSql);
logger.debug("Count SQL: {}", countSql);
try (Connection connection = configuration.getEnvironment().getDataSource().getConnection();
PreparedStatement countStmt = connection.prepareStatement(countSql)) {
// 使用 parameterHandler 設置 count 語句中的參數
statementHandler.getParameterHandler().setParameters(countStmt);
ResultSet rs = countStmt.executeQuery();
int count = 0;
if (rs.next()) {
count = rs.getInt(1);
}
return count;
} finally {
long elapsed = System.currentTimeMillis() - start;
logger.debug("Query page count elapsed {} ms.", elapsed);
}
}
示例10: intercept
import org.apache.ibatis.executor.statement.StatementHandler; //導入依賴的package包/類
@Override
public Object intercept(Invocation invocation) throws Throwable {
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
if(statementHandler instanceof RoutingStatementHandler){
RoutingStatementHandler routeStatementHandler = (RoutingStatementHandler)statementHandler;
MetaObject metaObject = SystemMetaObject.forObject(routeStatementHandler);
int offset = Integer.valueOf(String.valueOf(metaObject.getValue("delegate.rowBounds.offset")));
int limit = Integer.valueOf(String.valueOf(metaObject.getValue("delegate.rowBounds.limit")));
if(offset == RowBounds.NO_ROW_OFFSET && limit == RowBounds.NO_ROW_LIMIT){
return invocation.proceed();
}
String sql = String.valueOf(metaObject.getValue("delegate.boundSql.sql")).trim();
if(StringUtils.isBlank(sql) || !sql.toLowerCase().startsWith("select")){
return invocation.proceed();
}
String pagedSql = this.buildPagedSql(sql, offset, limit);
metaObject.setValue("delegate.boundSql.sql", pagedSql);
metaObject.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET);
metaObject.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT);
}else{
return invocation.proceed();
}
return invocation.proceed();
}
示例11: prepareStatement
import org.apache.ibatis.executor.statement.StatementHandler; //導入依賴的package包/類
private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
Statement stmt;
//得到綁定的SQL語句
BoundSql boundSql = handler.getBoundSql();
String sql = boundSql.getSql();
//如果緩存中已經有了,直接得到Statement
if (hasStatementFor(sql)) {
stmt = getStatement(sql);
} else {
//如果緩存沒有找到,則和SimpleExecutor處理完全一樣,然後加入緩存
Connection connection = getConnection(statementLog);
stmt = handler.prepare(connection);
putStatement(sql, stmt);
}
handler.parameterize(stmt);
return stmt;
}
示例12: doUpdate
import org.apache.ibatis.executor.statement.StatementHandler; //導入依賴的package包/類
@Override
public int doUpdate(MappedStatement ms, Object parameterObject) throws SQLException {
final Configuration configuration = ms.getConfiguration();
final StatementHandler handler = configuration.newStatementHandler(this, ms, parameterObject, RowBounds.DEFAULT, null, null);
final BoundSql boundSql = handler.getBoundSql();
final String sql = boundSql.getSql();
final Statement stmt;
if (sql.equals(currentSql) && ms.equals(currentStatement)) {
int last = statementList.size() - 1;
stmt = statementList.get(last);
BatchResult batchResult = batchResultList.get(last);
batchResult.addParameterObject(parameterObject);
} else {
Connection connection = getConnection(ms.getStatementLog());
stmt = handler.prepare(connection);
currentSql = sql;
currentStatement = ms;
statementList.add(stmt);
batchResultList.add(new BatchResult(ms, sql, parameterObject));
}
handler.parameterize(stmt);
handler.batch(stmt);
return BATCH_UPDATE_RETURN_VALUE;
}
示例13: doQuery
import org.apache.ibatis.executor.statement.StatementHandler; //導入依賴的package包/類
@Override
public <E> List<E> doQuery(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql)
throws SQLException {
Statement stmt = null;
try {
flushStatements();
Configuration configuration = ms.getConfiguration();
StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameterObject, rowBounds, resultHandler, boundSql);
Connection connection = getConnection(ms.getStatementLog());
stmt = handler.prepare(connection);
handler.parameterize(stmt);
return handler.<E>query(stmt, resultHandler);
} finally {
closeStatement(stmt);
}
}
示例14: doUpdate
import org.apache.ibatis.executor.statement.StatementHandler; //導入依賴的package包/類
@Override
public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {
Statement stmt = null;
try {
Configuration configuration = ms.getConfiguration();
//新建一個StatementHandler
//這裏看到ResultHandler傳入的是null
StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null, null);
//準備語句
stmt = prepareStatement(handler, ms.getStatementLog());
//StatementHandler.update
return handler.update(stmt);
} finally {
closeStatement(stmt);
}
}
示例15: doQuery
import org.apache.ibatis.executor.statement.StatementHandler; //導入依賴的package包/類
@Override
public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
Statement stmt = null;
try {
Configuration configuration = ms.getConfiguration();
//新建一個StatementHandler
//這裏看到ResultHandler傳入了
StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
//準備語句
stmt = prepareStatement(handler, ms.getStatementLog());
//StatementHandler.query
return handler.<E>query(stmt, resultHandler);
} finally {
closeStatement(stmt);
}
}