本文整理匯總了Java中org.apache.ibatis.executor.statement.StatementHandler.getBoundSql方法的典型用法代碼示例。如果您正苦於以下問題:Java StatementHandler.getBoundSql方法的具體用法?Java StatementHandler.getBoundSql怎麽用?Java StatementHandler.getBoundSql使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.ibatis.executor.statement.StatementHandler
的用法示例。
在下文中一共展示了StatementHandler.getBoundSql方法的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 {
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();
}
示例4: 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;
}
示例5: 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;
}
示例6: pagination
import org.apache.ibatis.executor.statement.StatementHandler; //導入方法依賴的package包/類
private void pagination(Invocation invocation, StatementHandler target) throws SQLException {
final MetaObject metaStatementHandler = getMetaObject(target);
final BoundSql boundSql = target.getBoundSql();
Page page = PAGE_THREAD_LOCAL.get();
if (page == null) {
page = findPageParameter(boundSql.getParameterObject());
}
// 如果傳入的參數中有分頁對象且sql語句中有select,才做分頁處理
String sql = boundSql.getSql().toLowerCase();
if (sql.startsWith("select") && page != null) {
// 采用物理分頁後,就不需要mybatis的內存分頁了,所以重置下麵的兩個參數
metaStatementHandler.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET);
metaStatementHandler.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT);
// 設置分頁對象裏的總記錄數和總頁數
Connection connection = (Connection) invocation.getArgs()[0];
MappedStatement mappedStatement = (MappedStatement) metaStatementHandler.getValue("delegate.mappedStatement");
if (page.isCountTotal()) {
int recordsTotal = getTotalCount(sql, connection, mappedStatement, boundSql);
page.setTotalNum(recordsTotal);
}
// 最後重寫sql
String pageSql = buildPageSql(sql, page);
metaStatementHandler.setValue("delegate.boundSql.sql", pageSql);
}
}
示例7: 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);
handler.parameterize(stmt);//fix Issues 322
BatchResult batchResult = batchResultList.get(last);
batchResult.addParameterObject(parameterObject);
} else {
Connection connection = getConnection(ms.getStatementLog());
stmt = handler.prepare(connection);
handler.parameterize(stmt); //fix Issues 322
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;
}
示例8: plugin
import org.apache.ibatis.executor.statement.StatementHandler; //導入方法依賴的package包/類
/**
* 包裝目標類(Plugin.wrap(target, this),即,代理)
*
* @author songkun
* @create 2014年7月5日 下午2:47:10
* @since
* @param target
* @return Object
*/
@Override
public Object plugin(Object target) {
// 能夠走到這裏的target為以下幾種(可以推斷Signature注解該寫什麽):
// (1):org.apache.ibatis.executor.Executor(調用Configuration.newExecutor時,以下同)
// (2):org.apache.ibatis.executor.parameter.ParameterHandler(newParameterHandler)
// (3):org.apache.ibatis.executor.resultset.ResultSetHandler(newResultSetHandler)
// (4):org.apache.ibatis.executor.statement.StatementHandler(newStatementHandler)
// 當目標類是StatementHandler類型時,才可能包裝目標類,否者直接返回目標本身,減少目標被代理的次數
if (target instanceof StatementHandler) {
StatementHandler tmp = (StatementHandler) target;
// 如果是select 語句,則,包裝
// update/insert/delete……不包裝
// 也可以判斷sqlCommandType:target-->delegate-->mappedStatement-->sqlCommandType,delegate是私有屬性
if (tmp.getBoundSql() != null && this.startWithSelect(tmp.getBoundSql().getSql())) {
return Plugin.wrap(target, this);
}
}
return target;
}
示例9: 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();
MetaObject metaStatementHandler = MetaObject.forObject(statementHandler, new DefaultObjectFactory(), new DefaultObjectWrapperFactory());
RowBounds rowBounds = (RowBounds) metaStatementHandler.getValue("delegate.rowBounds");
if ((rowBounds != null) && (rowBounds != RowBounds.DEFAULT)) {
Configuration configuration = (Configuration) metaStatementHandler.getValue("delegate.configuration");
Dialect dialect = DialectParser.parse(configuration);
String sql = (String) metaStatementHandler.getValue("delegate.boundSql.sql");
sql = dialect.addLimitString(sql, rowBounds.getOffset(), rowBounds.getLimit());
metaStatementHandler.setValue("delegate.boundSql.sql", sql);
metaStatementHandler.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET);
metaStatementHandler.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT);
}
log.debug("SQL : " + boundSql.getSql());
return invocation.proceed();
}
示例10: prepareStatement
import org.apache.ibatis.executor.statement.StatementHandler; //導入方法依賴的package包/類
private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
Statement stmt;
BoundSql boundSql = handler.getBoundSql();
String sql = boundSql.getSql();
if (hasStatementFor(sql)) {
stmt = getStatement(sql);
applyTransactionTimeout(stmt);
} else {
Connection connection = getConnection(statementLog);
stmt = handler.prepare(connection, transaction.getTimeout());
putStatement(sql, stmt);
}
handler.parameterize(stmt);
return stmt;
}
示例11: 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);
applyTransactionTimeout(stmt);
handler.parameterize(stmt);//fix Issues 322
BatchResult batchResult = batchResultList.get(last);
batchResult.addParameterObject(parameterObject);
} else {
Connection connection = getConnection(ms.getStatementLog());
stmt = handler.prepare(connection, transaction.getTimeout());
handler.parameterize(stmt); //fix Issues 322
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;
}
示例12: interceptQuery
import org.apache.ibatis.executor.statement.StatementHandler; //導入方法依賴的package包/類
public Object interceptQuery(Invocation invocation) throws Throwable {
/** statement is new Object with different memory address */
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
BoundSql boundSql = statementHandler.getBoundSql();
String sql = boundSql.getSql();
System.out.println(statementHandler.getClass());
System.out.println("sql->" + sql);
ISqlParser iSqlParser = new DruidSqlParser();
iSqlParser.init(sql);
iSqlParser.setTableName("fuck");
String masterColumn = "id";
if (boundSql.getParameterObject() instanceof MapperMethod.ParamMap) {
MapperMethod.ParamMap paramMap = (MapperMethod.ParamMap) boundSql.getParameterObject();
Object masterValue = paramMap.get(masterColumn);
System.out.println("param-> " + masterValue);
}
String sqlResult = iSqlParser.toSql();
System.out.println("sqlResult->" + sqlResult);
Statement statement = (Statement) invocation.getArgs()[0];
ReflectionUtils.setDeclaredFieldValue(boundSql, "sql", sqlResult);
Object h = ReflectionUtils.getFieldValue(statement, "h");
if (h instanceof PreparedStatementLogger) {
PreparedStatementLogger preparedStatementLogger = (PreparedStatementLogger) h;
PreparedStatement preparedStatement = preparedStatementLogger.getPreparedStatement();
}
System.out.println(statement.getClass().getName());
System.out.println("sta-> " + statementHandler);
return invocation.proceed();
}
示例13: prepareStatement
import org.apache.ibatis.executor.statement.StatementHandler; //導入方法依賴的package包/類
private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
Statement stmt;
BoundSql boundSql = handler.getBoundSql();
String sql = boundSql.getSql();
if (hasStatementFor(sql)) {
stmt = getStatement(sql);
} else {
Connection connection = getConnection(statementLog);
stmt = handler.prepare(connection);
putStatement(sql, stmt);
}
handler.parameterize(stmt);
return stmt;
}
示例14: intercept
import org.apache.ibatis.executor.statement.StatementHandler; //導入方法依賴的package包/類
@Override
public Object intercept(Invocation invocation) throws Throwable {
final StatementHandler handler = (StatementHandler) invocation
.getTarget();
final BoundSql boundSql = handler.getBoundSql();
// 쿼리문을 가져온다(이 상태에서의 쿼리는 값이 들어갈 부분에 ?가 있다)
final String sql = boundSql.getSql();
// 쿼리실행시 맵핑되는 파라미터를 구한다
final Object param = handler.getParameterHandler().getParameterObject();
//
String sql2 = "";
if (param == null) {
// 파라미터가 아무것도 없을 경우
sql2 = sql.replaceFirst("\\?", "''");
} else {
// 해당 파라미터의 클래스가 Integer, Long, Float, Double, Map 클래스일 경우
// FIXME: sql2 = bindPrimitiveOrMapParams(boundSql, sql, param);
// 해당 파라미터가 사용자 정의 클래스일 경우
if (null == sql2) {
sql2 = bindCustomObjectParam(boundSql, param, sql);
}
}
//
logger.debug("---------------------------------------------------------------------");
logger.debug("sql : \n{}", sql2);
Object o = invocation.proceed(); // 쿼리 실행
logger.debug("=====================================================================");
//
return o;
}
示例15: intercept
import org.apache.ibatis.executor.statement.StatementHandler; //導入方法依賴的package包/類
@Override
public Object intercept(Invocation invocation) throws Throwable {
String tenant = TenantContextHolder.getTenant();
if(tenant == null || tenant == "") {
System.out.println("tenant 為空,不需要改寫sql語句");
return invocation.proceed();
}
if (invocation.getTarget() instanceof RoutingStatementHandler) {
System.out.println("aaaaaaa");
RoutingStatementHandler statementHandler = (RoutingStatementHandler) invocation
.getTarget();
StatementHandler delegate = (StatementHandler) ReflectHelper
.getFieldValue(statementHandler, "delegate");
BoundSql boundSql = delegate.getBoundSql();
Object obj = boundSql.getParameterObject();
// 通過反射獲取delegate父類BaseStatementHandler的mappedStatement屬性
MappedStatement mappedStatement = (MappedStatement) ReflectHelper
.getFieldValue(delegate, "mappedStatement");
// 攔截到的prepare方法參數是一個Connection對象
Connection connection = (Connection) invocation.getArgs()[0];
// 獲取當前要執行的Sql語句,也就是我們直接在Mapper映射語句中寫的Sql語句
String sql = boundSql.getSql();
// 給當前的page參數對象設置總記錄數
System.out.println("處理之前" + sql);
//對 sql 增加 mycat 注解
sql = "/*!mycat:schema=" + tenant + " */" + sql;
System.out.println("加入處理後:" + sql);
ReflectHelper.setFieldValue(boundSql, "sql", sql);
}
return invocation.proceed();
}