@Override
	public Object intercept(Invocation invocation) throws Throwable {

		StatementHandler statementHandler = (StatementHandler) invocation
				.getTarget();
		BoundSql boundSql = statementHandler.getBoundSql();
		MetaObject metaStatementHandler = MetaObject
				.forObject(statementHandler);
		Object parameterObject = boundSql.getParameterObject();

		if (parameterObject == null) {
			return invocation.proceed();
		}

		Object p = null;

		if (parameterObject instanceof Map) {
			@SuppressWarnings("unchecked")
			Map<String, Object> map = (Map<String, Object>) parameterObject;
			p = map.get("page");
		}

		if (p == null || !(p instanceof PageBean)) {
			return invocation.proceed();
		}

		@SuppressWarnings("unchecked")
		PageBean<Object> page = (PageBean<Object>) p;

		int count = getCountRecord(statementHandler, metaStatementHandler);

		if (log.isDebugEnabled()) {
			log.debug("Record Count is " + count);
		}

		page.setTotalRecord(count);
		page.doPaging();

		RowBounds rowBounds = new RowBounds(page.getPageSize()
				* (page.getCurrentPage() - 1), page.getPageSize());

		String originalSql = boundSql.getSql().trim();
		metaStatementHandler.setValue("delegate.boundSql.sql", dialect
				.getPageSQL(originalSql, rowBounds.getOffset(),
						rowBounds.getLimit()));
		metaStatementHandler.setValue("delegate.rowBounds.offset",
				RowBounds.NO_ROW_OFFSET);
		metaStatementHandler.setValue("delegate.rowBounds.limit",
				RowBounds.NO_ROW_LIMIT);

		if (log.isDebugEnabled()) {
			log.debug("Pagination SQL : " + boundSql.getSql());
		}

		return invocation.proceed();
	}