@Mock public int computeX(Invocation inv, int a, int b) { inputValues.add(a); inputValues.add(b); int x = inv.proceed(); outputValues.add(x); return x; }
@Mock public static void validatePublicVoidNoArg( Invocation invocation, boolean isStatic, List<Throwable> errors) { FrameworkMethod it = invocation.getInvokedInstance(); if (!isStatic && it.getMethod().getParameterTypes().length > 0) { it.validatePublicVoid(false, errors); return; } invocation.proceed(); }
@Mock public int computeX(Invocation inv, int a, int b) { Integer x; try { x = inv.proceed(); return x; } finally { // Statements to be executed on exit would be here. //noinspection UnusedAssignment x = a + b; values.add(x); } }
@SuppressWarnings({"rawtypes", "unchecked"}) @Override public Object intercept(Invocation invocation) throws Throwable { final String name = invocation.getMethod().getName(); final Object target = invocation.getTarget(); if (target instanceof StatementHandler) { pagination(invocation, (StatementHandler) target); } else if (target instanceof Executor) { autoMap(invocation, name); } else if (target instanceof ResultSetHandler) { Object result = invocation.proceed(); if (result instanceof List) { Page page = PAGE_THREAD_LOCAL.get(); if (page != null) { page.addAll((List) result); PAGE_THREAD_LOCAL.remove(); return page; } } return result; } return invocation.proceed(); }
public Object doPage(Invocation invocation) throws Throwable { log.debug("进入Executor"); final Object[] args = invocation.getArgs(); RowBounds rowBounds = (RowBounds) args[2]; PageParam pageParam = null; // PageContextHolder.get(); if (args[1] instanceof MapperMethod.ParamMap<?>) { MapperMethod.ParamMap<?> map = (MapperMethod.ParamMap<?>) args[1]; for (String key : map.keySet()) { if (key.startsWith("param")) { Object obj = map.get(key); if (obj instanceof PageParam) { pageParam = (PageParam) obj; } else if (args[1] instanceof MapperMethod.ParamMap<?>) { args[1] = obj; } } } } // 无分页参数 if (pageParam == null) { log.debug("默认查询"); return invocation.proceed(); } log.debug("分页查询"); PageWrapper page = new PageWrapper(); MappedStatement ms = (MappedStatement) args[0]; args[2] = RowBounds.DEFAULT; // 无需分页 if (pageParam.getPageSize() == 0) { log.debug("全量查询"); Object result = invocation.proceed(); page.addAll((List) result); page.setPageNo(1); page.setPageSize(page.size()); page.setTotalPage(1); page.setTotalCount(page.size()); return page; } SqlSource sqlSource = ((MappedStatement) args[0]).getSqlSource(); dialect = DialectFactory.buildDialect(ms.getConfiguration()); // 需总数 args[0] = getMappedStatement(ms, sqlSource, args[1], Const.CACHE_KEY_COUNT, MappingEnum.INT); Object resultCount = invocation.proceed(); int totalCount = ((Integer) ((List) resultCount).get(0)); log.debug("总条数查询为 {}", totalCount); page.setTotalCount(totalCount); page.setPageNo(pageParam.getPageNo()); page.setPageSize(pageParam.getPageSize()); // 提升效率:count为0不再分页查 if (page.getTotalCount() == 0) { return page; } // 需分页 // 便捷查询:pageSize<=0 仅查询count if (pageParam.getPageSize() > 0) { args[0] = getMappedStatement(ms, sqlSource, args[1], Const.CACHE_KEY_PAGE, MappingEnum.DEFAULT); args[1] = setParameter((MappedStatement) args[0], args[1], pageParam, dialect); Object result = invocation.proceed(); page.addAll((List) result); log.debug("分页查询结束 {}", pageParam); } return page; }
@Mock public String getConfig(Invocation inv) { String config = inv.proceed(); assertNull(config); return "test"; }
@Mock public int computeX(Invocation inv, int a, int b) { values.add(a + b); return inv.proceed(); }