Java Code Examples for org.apache.ibatis.plugin.Invocation#proceed()
The following examples show how to use
org.apache.ibatis.plugin.Invocation#proceed() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PaginationPlugin.java From easyooo-framework with Apache License 2.0 | 6 votes |
@Override public Object intercept(Invocation invocation) throws Throwable { Object[] args = invocation.getArgs(); // no paging if(!isPaging(args)){ return invocation.proceed(); } // process for paging InvocationContext context = getInvocationContext(invocation); InvocationContext newContext = processIntercept(context); swapParameter(newContext, args); Object result = invocation.proceed(); if(result != null && result instanceof List){ newContext.getPagination().setRecords((List<?>)result); } return result; }
Example 2
Source File: MySqlPagingPlugin.java From mybatis-test with Apache License 2.0 | 6 votes |
@Override public Object intercept(Invocation invocation) throws Throwable { Object[] args = invocation.getArgs(); RowBounds rb = (RowBounds) args[ROW_BOUNDS_INDEX]; if (rb == RowBounds.DEFAULT) { return invocation.proceed(); } args[ROW_BOUNDS_INDEX] = RowBounds.DEFAULT; MappedStatement ms = (MappedStatement) args[MAPPED_STATEMENT_INDEX]; BoundSql boundSql = ms.getBoundSql(args[PARAMETER_INDEX]); System.out.println(); String sql = boundSql.getSql(); String limit = String.format("LIMIT %d,%d", rb.getOffset(), rb.getLimit()); sql = sql + " " + limit; SqlSource sqlSource = new StaticSqlSource(ms.getConfiguration(), sql, boundSql.getParameterMappings()); Field field = MappedStatement.class.getDeclaredField("sqlSource"); field.setAccessible(true); field.set(ms, sqlSource); return invocation.proceed(); }
Example 3
Source File: PaginationInterceptor.java From Mario with Apache License 2.0 | 5 votes |
@Override public Object intercept(Invocation invocation) throws Throwable { StatementHandler statementHandler = (StatementHandler) invocation.getTarget(); BoundSql boundSql = statementHandler.getBoundSql(); String sql = boundSql.getSql(); if (StringUtils.isBlank(sql)) { return invocation.proceed(); } //select sql do if (sql.matches(SQL_SELECT_REGEX) && !Pattern.matches(SQL_COUNT_REGEX, sql)) { Object obj = FieldUtils.readField(statementHandler, "delegate", true); // 反射获取 RowBounds 对象。 RowBounds rowBounds = (RowBounds) FieldUtils.readField(obj, "rowBounds", true); // 分页参数存在且不为默认值时进行分页SQL构造 if (rowBounds != null && rowBounds != RowBounds.DEFAULT) { FieldUtils.writeField(boundSql, "sql", newSql(sql, rowBounds), true); // 一定要还原否则将无法得到下一组数据(第一次的数据被缓存了) FieldUtils.writeField(rowBounds, "offset", RowBounds.NO_ROW_OFFSET, true); FieldUtils.writeField(rowBounds, "limit", RowBounds.NO_ROW_LIMIT, true); } } return invocation.proceed(); }
Example 4
Source File: QueryLoggingInterceptor.java From sinavi-jfw with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} * <p> * {@code StatementHandler}へのクエリ実行要求をインターセプトし、 SQLのロギング機構を起動します。 * </p> */ @Override public Object intercept(Invocation invocation) throws Throwable { QueryInformation queryInformation = new QueryInformation((StatementHandler)invocation.getTarget()); Object result; queryLogger.log(queryInformation); result = invocation.proceed(); return result; }
Example 5
Source File: PageInterceptor.java From Zebra with Apache License 2.0 | 5 votes |
@Override public Object intercept(Invocation invocation) throws Throwable { Object[] args = invocation.getArgs(); Object rowBound = args[2]; MappedStatement ms = (MappedStatement) args[0]; if (rowBound != null) { RowBounds rb = (RowBounds) rowBound; // without pagination if (rb.getOffset() == RowBounds.NO_ROW_OFFSET && rb.getLimit() == RowBounds.NO_ROW_LIMIT) { return invocation.proceed(); } else { BoundSql boundSql = ms.getBoundSql(args[1]); if (rowBound instanceof PageModel) { // physical pagination with PageModel PageModel pageModel = (PageModel) rowBound; Object count = queryCount(invocation, args, ms, boundSql); Object records = queryLimit(invocation, args, ms, boundSql, pageModel); pageModel.setRecordCount((Integer) ((List<?>) count).get(0)); pageModel.setRecords((List<?>) records); return null; } else { // physical pagination with RowBounds return queryLimit(invocation, args, ms, boundSql, rb); } } } else { // without pagination return invocation.proceed(); } }
Example 6
Source File: MyPlugin.java From blog with BSD 2-Clause "Simplified" License | 5 votes |
/** * 代替拦截对象方法内容 */ @Override public Object intercept(Invocation invocation) throws Throwable { System.err.println("======before==============="); Object obj = invocation.proceed(); System.err.println("======after==============="); return obj; }
Example 7
Source File: MybatisInterceptor.java From DDMQ with Apache License 2.0 | 5 votes |
public Object intercept(Invocation invocation) throws Throwable { Object returnValue; if (showDetailSql || showCostTime) { MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; Object parameter = null; if (invocation.getArgs().length > 1) { parameter = invocation.getArgs()[1]; } String sqlId = mappedStatement.getId(); BoundSql boundSql = mappedStatement.getBoundSql(parameter); Configuration configuration = mappedStatement.getConfiguration(); long start = System.currentTimeMillis(); returnValue = invocation.proceed(); long end = System.currentTimeMillis(); long time = (end - start); String sql = getSql(configuration, boundSql, sqlId, time); if (slowSqlMs != 0 && time > slowSqlMs) { log.warn(sql); } else { log.info(sql); } } else { returnValue = invocation.proceed(); } return returnValue; }
Example 8
Source File: MybatisInterceptor.java From cicada with MIT License | 5 votes |
public Object intercept(final Invocation invocation) throws Throwable { final MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; Object returnValue = null; final long start = System.currentTimeMillis(); returnValue = invocation.proceed(); final long end = System.currentTimeMillis(); final String sqlId = mappedStatement.getId(); final int lastIndex = sqlId.lastIndexOf('.'); final String className = sqlId.substring(0, lastIndex); final String methodName = sqlId.substring(lastIndex + 1); Tracer.getInstance().addBinaryAnnotation(className, methodName, (int) (end - start)); return returnValue; }
Example 9
Source File: DynamicDataSourcePlugin.java From EasyReport with Apache License 2.0 | 5 votes |
@Override public Object intercept(final Invocation invocation) throws Throwable { final boolean synchronizationActive = TransactionSynchronizationManager.isSynchronizationActive(); if (!synchronizationActive) { final Object[] objects = invocation.getArgs(); final MappedStatement mappedStatement = (MappedStatement)objects[0]; final DynamicDataSourceType dynamicDataSourceType = this.getDynamicDataSourceType(mappedStatement); cacheMap.put(mappedStatement.getId(), dynamicDataSourceType); DynamicDataSourceHolder.putDataSource(dynamicDataSourceType); log.info("SQL [{}] from datasource [{}],SqlCommandType [{}]", mappedStatement.getId(), dynamicDataSourceType.name(), mappedStatement.getSqlCommandType().name()); } return invocation.proceed(); }
Example 10
Source File: ExamplePlugin.java From mybatis-test with Apache License 2.0 | 5 votes |
@Override public Object intercept(Invocation invocation) throws Throwable { Method method = invocation.getMethod(); Object[] args = invocation.getArgs(); System.out.println(method + " - " + args); return invocation.proceed(); }
Example 11
Source File: LogSqlExecutionTimePlugin.java From flowable-engine with Apache License 2.0 | 5 votes |
@Override public Object intercept(Invocation invocation) throws Throwable { long startTime = System.currentTimeMillis(); Object retVal = invocation.proceed(); long endTime = System.currentTimeMillis(); MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; LOGGER.debug("SQL Statement {} took {}ms", mappedStatement.getId(), endTime-startTime); return retVal; }
Example 12
Source File: BindingLogPlugin32.java From pinpoint with Apache License 2.0 | 5 votes |
@Override public Object intercept(Invocation invocation) throws Throwable { if (internalLogger.isTraceEnabled()) { internalLogger.trace("target:" + invocation.getTarget() + " method:" + invocation.getMethod() + " args:" + Arrays.toString(invocation.getArgs())); } try { return invocation.proceed(); } finally { bindingLog(invocation); } }
Example 13
Source File: CatMybatisPlugin.java From radar with Apache License 2.0 | 4 votes |
@Override public Object intercept(Invocation invocation) throws Throwable { String jdbcUrl = "Notsupported Url"; String method = "Notsupported Method"; String sql = "Notsupported SQL"; String classMethod = "Notsupported Class Method"; try { MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; DataSource ds = mappedStatement.getConfiguration().getEnvironment().getDataSource(); if (ds instanceof com.mchange.v2.c3p0.ComboPooledDataSource) { com.mchange.v2.c3p0.ComboPooledDataSource c3p0Ds = (com.mchange.v2.c3p0.ComboPooledDataSource) ds; jdbcUrl = c3p0Ds.getJdbcUrl(); } else if (ds instanceof org.apache.tomcat.jdbc.pool.DataSource) { org.apache.tomcat.jdbc.pool.DataSource tDs = (org.apache.tomcat.jdbc.pool.DataSource) ds; jdbcUrl = tDs.getUrl(); } else if (ds instanceof com.alibaba.druid.pool.DruidDataSource) { com.alibaba.druid.pool.DruidDataSource dDs = (com.alibaba.druid.pool.DruidDataSource) ds; jdbcUrl = dDs.getUrl(); } else { jdbcUrl = dbUrl; } // 得到 类名-方法 String[] strArr = mappedStatement.getId().split("\\."); classMethod = strArr[strArr.length - 2] + "." + strArr[strArr.length - 1]; // 得到sql语句 Object parameter = null; if (invocation.getArgs().length > 1) { parameter = invocation.getArgs()[1]; } BoundSql boundSql = mappedStatement.getBoundSql(parameter); Configuration configuration = mappedStatement.getConfiguration(); sql = showSql(configuration, boundSql); } catch (Exception ex) { } if (isFullLog() && sql.toLowerCase().indexOf("select") == -1 && sql.toLowerCase().indexOf("instance") != -1 && sql.toLowerCase().indexOf("update instance set heart_time=now()") == -1) { log.info("sql5_is_{}", sql); } Transaction t = Tracer.newTransaction("SQL", classMethod); method = sql.substring(0, sql.indexOf(" ")); Tracer.logEvent("SQL.Method", method); Tracer.logEvent("SQL.Database", jdbcUrl); Tracer.logEvent("SQL.Statement", method, Transaction.SUCCESS, sql.length() > 1000 ? sql.substring(0, 1000) : sql); Object returnObj = null; try { returnObj = invocation.proceed(); t.setStatus(Transaction.SUCCESS); } catch (Exception e) { if (sql.indexOf("soa_lock") != -1 && sql.indexOf("insert") != -1) { t.setStatus(Transaction.SUCCESS); } else { t.setStatus(e); Tracer.logError(e); throw e; } } finally { t.complete(); } return returnObj; }
Example 14
Source File: ExamplePlugin.java From mybaties with Apache License 2.0 | 4 votes |
public Object intercept(Invocation invocation) throws Throwable { return invocation.proceed(); }
Example 15
Source File: PagePluging.java From aaden-pay with Apache License 2.0 | 4 votes |
public Object intercept(Invocation invocation) throws Throwable { processIntercept(invocation.getArgs()); return invocation.proceed(); }
Example 16
Source File: ResultTypePlugin.java From mybatis-jpa with Apache License 2.0 | 4 votes |
@Override public Object intercept(Invocation invocation) throws Throwable { if (invocation.getTarget() instanceof DefaultResultSetHandler) { DefaultResultSetHandler resultSetHandler = (DefaultResultSetHandler) invocation.getTarget(); Object[] args = invocation.getArgs(); Statement stmt = (Statement) args[0]; MappedStatement mappedStatement = (MappedStatement) FieldReflectUtil .getFieldValue(resultSetHandler, "mappedStatement"); List<ResultMap> resultMaps = mappedStatement.getResultMaps(); if (resultMaps != null && !resultMaps.isEmpty()) { ResultMap resultMap = resultMaps.get(0); if (resultMap.getResultMappings() == null || resultMap.getResultMappings().isEmpty()) { if (resultMap.getType().isAnnotationPresent(Entity.class) || resultMap.getType() .isAnnotationPresent(Table.class)) { ResultMapParser parser = ResultMapParserHolder .getInstance(mappedStatement.getConfiguration()); ResultMap newResultMap = parser .reloadResultMap(mappedStatement.getResource(), resultMap.getId(), resultMap.getType()); List<ResultMap> newResultMaps = new ArrayList<>(); newResultMaps.add(newResultMap); FieldReflectUtil.setFieldValue(mappedStatement, "resultMaps", newResultMaps); // modify the resultMaps FieldReflectUtil.setFieldValue(resultSetHandler, "mappedStatement", mappedStatement); } } } // return resultSetHandler.handleResultSets(stmt); } return invocation.proceed(); }
Example 17
Source File: CatMybatisPlugin.java From pmq with Apache License 2.0 | 4 votes |
@Override public Object intercept(Invocation invocation) throws Throwable { String sql = ""; String classMethod = "Notsupported Class Method"; if (soaConfig.getCatSql() == 1) { try { //String jdbcUrl = "Notsupported Url"; //String method = "Notsupported Method"; // DataSource ds = null; MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; // ds = // mappedStatement.getConfiguration().getEnvironment().getDataSource(); // if (ds instanceof com.mchange.v2.c3p0.ComboPooledDataSource) // { // com.mchange.v2.c3p0.ComboPooledDataSource c3p0Ds = // (com.mchange.v2.c3p0.ComboPooledDataSource) ds; // jdbcUrl = c3p0Ds.getJdbcUrl(); // } else if (ds instanceof // org.apache.tomcat.jdbc.pool.DataSource) // { // org.apache.tomcat.jdbc.pool.DataSource tDs = // (org.apache.tomcat.jdbc.pool.DataSource) ds; // jdbcUrl = tDs.getUrl(); // } else if (ds instanceof // com.alibaba.druid.pool.DruidDataSource) // { // @SuppressWarnings("resource") // com.alibaba.druid.pool.DruidDataSource dDs = // (com.alibaba.druid.pool.DruidDataSource) ds; // jdbcUrl = dDs.getUrl(); // } else if (ds instanceof DynamicDataSource) { // com.alibaba.druid.pool.DruidDataSource dDs = // (com.alibaba.druid.pool.DruidDataSource) ((DynamicDataSource) // ds) // .getDataSource(); // jdbcUrl = dDs.getUrl(); // } else { // jdbcUrl = dbUrl; // } // 得到 类名-方法 String[] strArr = mappedStatement.getId().split("\\."); classMethod = strArr[strArr.length - 2] + "." + strArr[strArr.length - 1]; // 得到sql语句 Object parameter = null; if (invocation.getArgs().length > 1) { parameter = invocation.getArgs()[1]; } BoundSql boundSql = mappedStatement.getBoundSql(parameter); Configuration configuration = mappedStatement.getConfiguration(); sql = showSql(configuration, boundSql); if(classMethod.equals(soaConfig.getCatSqlKey())){ log.info("the sql is:"+sql); } } catch (Exception ex) { } } Transaction t = null; if (soaConfig.getCatSql() == 1) { t = Tracer.newTransaction("SQL", classMethod); } // method = sql.substring(0, sql.indexOf(" ")); // Tracer.logEvent("SQL.Method", method); // Tracer.logEvent("SQL.Database", jdbcUrl); // Tracer.logEvent("SQL.Statement", method, Transaction.SUCCESS, // sql.length() > 1000 ? sql.substring(0, 1000) : sql); Object returnObj = null; try { returnObj = invocation.proceed(); if (t != null) { t.setStatus(Transaction.SUCCESS); } } catch (Exception e) { if (t != null) { t.addData("sql", sql); t.setStatus(e); } throw e; } finally { if (t != null) { t.complete(); } } return returnObj; }
Example 18
Source File: OffsetLimitInterceptor.java From AsuraFramework with Apache License 2.0 | 4 votes |
public Object intercept(final Invocation invocation) throws Throwable { final Executor executor = (Executor) invocation.getTarget(); final Object[] queryArgs = invocation.getArgs(); final MappedStatement ms = (MappedStatement)queryArgs[MAPPED_STATEMENT_INDEX]; final Object parameter = queryArgs[PARAMETER_INDEX]; final RowBounds rowBounds = (RowBounds)queryArgs[ROWBOUNDS_INDEX]; final PageBounds pageBounds = new PageBounds(rowBounds); if(pageBounds.getOffset() == RowBounds.NO_ROW_OFFSET && pageBounds.getLimit() == RowBounds.NO_ROW_LIMIT && pageBounds.getOrders().isEmpty()){ return invocation.proceed(); } final Dialect dialect; try { Class clazz = Class.forName(dialectClass); Constructor constructor = clazz.getConstructor(MappedStatement.class, Object.class, PageBounds.class); dialect = (Dialect)constructor.newInstance(new Object[]{ms, parameter, pageBounds}); } catch (Exception e) { throw new ClassNotFoundException("Cannot create dialect instance: "+dialectClass,e); } final BoundSql boundSql = ms.getBoundSql(parameter); queryArgs[MAPPED_STATEMENT_INDEX] = copyFromNewSql(ms,boundSql,dialect.getPageSQL(), dialect.getParameterMappings(), dialect.getParameterObject()); queryArgs[PARAMETER_INDEX] = dialect.getParameterObject(); queryArgs[ROWBOUNDS_INDEX] = new RowBounds(RowBounds.NO_ROW_OFFSET,RowBounds.NO_ROW_LIMIT); Boolean async = pageBounds.getAsyncTotalCount() == null ? asyncTotalCount : pageBounds.getAsyncTotalCount(); Future<List> listFuture = call(new Callable<List>() { public List call() throws Exception { return (List)invocation.proceed(); } }, async); if(pageBounds.isContainsTotalCount()){ Callable<Paginator> countTask = new Callable() { public Object call() throws Exception { Integer count; Cache cache = ms.getCache(); if(cache != null && ms.isUseCache() && ms.getConfiguration().isCacheEnabled()){ CacheKey cacheKey = executor.createCacheKey(ms,parameter,new PageBounds(),copyFromBoundSql(ms,boundSql,dialect.getCountSQL(), boundSql.getParameterMappings(), boundSql.getParameterObject())); count = (Integer)cache.getObject(cacheKey); if(count == null){ count = SQLHelp.getCount(ms,executor.getTransaction(),parameter,boundSql,dialect); cache.putObject(cacheKey, count); } }else{ count = SQLHelp.getCount(ms,executor.getTransaction(),parameter,boundSql,dialect); } return new Paginator(pageBounds.getPage(), pageBounds.getLimit(), count); } }; Future<Paginator> countFutrue = call(countTask, async); return new PageList(listFuture.get(),countFutrue.get()); } return listFuture.get(); }
Example 19
Source File: PagePlugin.java From cms with Apache License 2.0 | 4 votes |
@SuppressWarnings("unchecked") public Object intercept(Invocation invocation) throws Throwable { if (invocation.getTarget() instanceof RoutingStatementHandler) { RoutingStatementHandler statementHandler = (RoutingStatementHandler) invocation.getTarget(); BaseStatementHandler delegate = (BaseStatementHandler) ReflectHelper.getValueByFieldName(statementHandler, "delegate"); MappedStatement mappedStatement = (MappedStatement) ReflectHelper.getValueByFieldName(delegate, "mappedStatement"); if (mappedStatement.getId().matches(pageSqlId)) { BoundSql boundSql = delegate.getBoundSql(); Object parameterObject = boundSql.getParameterObject(); if (parameterObject == null) { throw new NullPointerException("parameterObject is null."); } else { Connection connection = (Connection) invocation.getArgs()[0]; String sql = boundSql.getSql(); String countSql="select count(0) from (" + sql + ") tmp_count "; /*if ("mysql".equals(dialect)) { } else if ("oracle".equals(dialect)) { }*/ PreparedStatement countStmt = connection.prepareStatement(countSql); BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), countSql, boundSql.getParameterMappings(), parameterObject); setParameters(countStmt, mappedStatement, countBS, parameterObject); ResultSet rs = countStmt.executeQuery(); int count = 0; if (rs.next()) { count = rs.getInt(1); } rs.close(); countStmt.close(); Page page = null; if (parameterObject instanceof Page) { page = (Page) parameterObject; page.setTotalResult(count); } else if (parameterObject instanceof Map) { Map<String, Object> map = (Map<String, Object>) parameterObject; page = (Page) map.get("page"); if (page == null) page = new Page(); page.setTotalResult(count); } else { Field pageField = ReflectHelper.getFieldByFieldName(parameterObject, "page"); if (pageField != null) { page = (Page) ReflectHelper.getValueByFieldName(parameterObject, "page"); if (page == null) page = new Page(); page.setTotalResult(count); ReflectHelper.setValueByFieldName(parameterObject, "page", page); } else { throw new NoSuchFieldException(parameterObject.getClass().getName()); } } String pageSql = generatePageSql(sql, page); ReflectHelper.setValueByFieldName(boundSql, "sql", pageSql); } } } // Object result = invocation.proceed(); //执行请求方法,并将所得结果保存到result中 // if (result instanceof ArrayList) { // ArrayList resultList = (ArrayList) result; // for (int i = 0; i < resultList.size(); i++) { // if (resultList.get(i) instanceof Map) { // Map resultMap = (Map) resultList.get(i); // resultMap.put("CERT_NO", "这个是加密结果"); //取出相应的字段进行加密 // } // } // } return invocation.proceed(); }
Example 20
Source File: PaginationInterceptor.java From Shop-for-JavaWeb with MIT License | 4 votes |
@Override public Object intercept(Invocation invocation) throws Throwable { final MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; // //拦截需要分页的SQL //// if (mappedStatement.getId().matches(_SQL_PATTERN)) { // if (StringUtils.indexOfIgnoreCase(mappedStatement.getId(), _SQL_PATTERN) != -1) { Object parameter = invocation.getArgs()[1]; BoundSql boundSql = mappedStatement.getBoundSql(parameter); Object parameterObject = boundSql.getParameterObject(); //获取分页参数对象 Page<Object> page = null; if (parameterObject != null) { page = convertParameter(parameterObject, page); } //如果设置了分页对象,则进行分页 if (page != null && page.getPageSize() != -1) { if (StringUtils.isBlank(boundSql.getSql())){ return null; } String originalSql = boundSql.getSql().trim(); //得到总记录数 page.setCount(SQLHelper.getCount(originalSql, null, mappedStatement, parameterObject, boundSql, log)); //分页查询 本地化对象 修改数据库注意修改实现 String pageSql = SQLHelper.generatePageSql(originalSql, page, DIALECT); // if (log.isDebugEnabled()) { // log.debug("PAGE SQL:" + StringUtils.replace(pageSql, "\n", "")); // } invocation.getArgs()[2] = new RowBounds(RowBounds.NO_ROW_OFFSET, RowBounds.NO_ROW_LIMIT); BoundSql newBoundSql = new BoundSql(mappedStatement.getConfiguration(), pageSql, boundSql.getParameterMappings(), boundSql.getParameterObject()); MappedStatement newMs = copyFromMappedStatement(mappedStatement, new BoundSqlSqlSource(newBoundSql)); invocation.getArgs()[0] = newMs; } // } return invocation.proceed(); }