Java Code Examples for org.apache.ibatis.mapping.BoundSql#getSql()

The following examples show how to use org.apache.ibatis.mapping.BoundSql#getSql() . 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: CheckSQLInterceptor.java    From java-tutorial with MIT License 6 votes vote down vote up
@Override
public Object intercept(Invocation invocation) throws Throwable {

    //获取方法的第0个参数,也就是MappedStatement。@Signature注解中的args中的顺序
    MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
    //获取sql命令操作类型
    SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
    final Object[] queryArgs = invocation.getArgs();
    final Object parameter = queryArgs[1];

    BoundSql boundSql = mappedStatement.getBoundSql(parameter);
    String sql = boundSql.getSql();

    if (SqlCommandType.DELETE.equals(sqlCommandType)) {
        //格式化sql
        sql = sql.replace("\n", "");
        if (!sql.toLowerCase().contains(SQL_DELETE_WHERE)) {
            sql = sql.replace(" ", "");
            logger.warn("删除语句中没有where条件,sql为:{}", sql);
            throw new SQLException("删除语句中没有where条件");
        }
        return invocation.proceed();
    }

    return null;
}
 
Example 2
Source File: BatchExecutor.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 3
Source File: PaginationInterceptor.java    From joyqueue with Apache License 2.0 6 votes vote down vote up
@Override
public Object intercept(Invocation invocation) throws Throwable {
    StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
    BoundSql boundSql = statementHandler.getBoundSql();
    if (log.isDebugEnabled()) {
        log.debug("raw SQL : " + boundSql.getSql());
    }
    if (boundSql.getSql() == null || boundSql.getSql().isEmpty() || boundSql.getSql().contains(" limit ")) {
        return invocation.proceed();
    }
    MetaObject metaStatementHandler = null;
    RowBounds rowBounds = (RowBounds) metaStatementHandler.getValue("delegate.rowBounds");
    if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
        return invocation.proceed();
    }
    String originalSql = (String) metaStatementHandler.getValue("delegate.boundSql.sql");
    metaStatementHandler.setValue("delegate.boundSql.sql",
            getLimitString(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();
}
 
Example 4
Source File: ReuseExecutor.java    From mybatis with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 5
Source File: BindingLogPlugin32.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
private void bindingLog(Invocation invocation) throws SQLException {

        Object[] args = invocation.getArgs();
        MappedStatement ms = (MappedStatement) args[0];
        Object parameterObject = args[1];
        StatementType statementType = ms.getStatementType();
        if (StatementType.PREPARED == statementType || StatementType.CALLABLE == statementType) {
            Log statementLog = ms.getStatementLog();
            if (isDebugEnable(statementLog)) {
                BoundSql boundSql = ms.getBoundSql(parameterObject);

                String sql = boundSql.getSql();
                List<String> parameterList = getParameters(ms, parameterObject, boundSql);
                debug(statementLog, "==> BindingLog: " + bindLogFormatter.format(sql, parameterList));
            }
        }
    }
 
Example 6
Source File: SqlHelper.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 通过命名空间方式获取sql
 * @param session
 * @param namespace
 * @param params
 * @return
 */
public static String getNamespaceSql(SqlSession session, String namespace, Object params) {
    Configuration configuration = session.getConfiguration();
    MappedStatement mappedStatement = configuration.getMappedStatement(namespace);
    TypeHandlerRegistry typeHandlerRegistry = mappedStatement.getConfiguration().getTypeHandlerRegistry();
    BoundSql boundSql = mappedStatement.getBoundSql(params);
    List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
    String sql = boundSql.getSql();
    if (parameterMappings != null) {
        for (int i = 0; i < parameterMappings.size(); i++) {
            ParameterMapping parameterMapping = parameterMappings.get(i);
            if (parameterMapping.getMode() != ParameterMode.OUT) {
                Object value;
                String propertyName = parameterMapping.getProperty();
                if (boundSql.hasAdditionalParameter(propertyName)) {
                    value = boundSql.getAdditionalParameter(propertyName);
                } else if (params == null) {
                    value = null;
                } else if (typeHandlerRegistry.hasTypeHandler(params.getClass())) {
                    value = params;
                } else {
                    MetaObject metaObject = configuration.newMetaObject(params);
                    value = metaObject.getValue(propertyName);
                }
                JdbcType jdbcType = parameterMapping.getJdbcType();
                if (value == null && jdbcType == null) jdbcType = configuration.getJdbcTypeForNull();
                sql = replaceParameter(sql, value, jdbcType, parameterMapping.getJavaType());
            }
        }
    }
    return sql;
}
 
Example 7
Source File: DataScopeInterceptor.java    From pig with MIT License 5 votes vote down vote up
@Override
public Object intercept(Invocation invocation) throws Throwable {
    StatementHandler statementHandler = (StatementHandler) PluginUtils.realTarget(invocation.getTarget());
    MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
    this.sqlParser(metaObject);
    // 先判断是不是SELECT操作
    MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
    if (!SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType())) {
        return invocation.proceed();
    }

    BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql");
    String originalSql = boundSql.getSql();
    Object parameterObject = boundSql.getParameterObject();

    //查找参数中包含DataScope类型的参数
    DataScope dataScope = findDataScopeObject(parameterObject);

    if (dataScope == null) {
        return invocation.proceed();
    } else {
        String scopeName = dataScope.getScopeName();
        List<Integer> deptIds = dataScope.getDeptIds();
        if(StrUtil.isNotBlank(scopeName) && CollectionUtil.isNotEmpty(deptIds)){
            String join = CollectionUtil.join(deptIds, ",");
            originalSql = "select * from (" + originalSql + ") temp_data_scope where temp_data_scope." + scopeName + " in (" + join + ")";
            metaObject.setValue("delegate.boundSql.sql", originalSql);
        }
        return invocation.proceed();
    }
}
 
Example 8
Source File: AbstractHelperDialect.java    From Mybatis-PageHelper with MIT License 5 votes vote down vote up
@Override
public String getPageSql(MappedStatement ms, BoundSql boundSql, Object parameterObject, RowBounds rowBounds, CacheKey pageKey) {
    String sql = boundSql.getSql();
    Page page = getLocalPage();
    //支持 order by
    String orderBy = page.getOrderBy();
    if (StringUtil.isNotEmpty(orderBy)) {
        pageKey.update(orderBy);
        sql = OrderByParser.converToOrderBySql(sql, orderBy);
    }
    if (page.isOrderByOnly()) {
        return sql;
    }
    return getPageSql(sql, page, pageKey);
}
 
Example 9
Source File: SqlServerDialect.java    From Mybatis-PageHelper with MIT License 5 votes vote down vote up
@Override
public String getCountSql(MappedStatement ms, BoundSql boundSql, Object parameterObject, RowBounds rowBounds, CacheKey countKey) {
    String sql = boundSql.getSql();
    String cacheSql = CACHE_COUNTSQL.get(sql);
    if (cacheSql != null) {
        return cacheSql;
    } else {
        cacheSql = sql;
    }
    cacheSql = replaceSql.replace(cacheSql);
    cacheSql = countSqlParser.getSmartCountSql(cacheSql);
    cacheSql = replaceSql.restore(cacheSql);
    CACHE_COUNTSQL.put(sql, cacheSql);
    return cacheSql;
}
 
Example 10
Source File: PaginationInterceptor.java    From Mario with Apache License 2.0 5 votes vote down vote up
@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 11
Source File: NullEnhancement.java    From mybatis-boost with MIT License 5 votes vote down vote up
@Override
public void replace(Connection connection, MetaObject metaObject, MappedStatement mappedStatement, BoundSql boundSql) {
    if (mappedStatement.getSqlCommandType() == SqlCommandType.SELECT ||
            mappedStatement.getSqlCommandType() == SqlCommandType.DELETE) {
        String sql = boundSql.getSql();

        Matcher matcher = SqlUtils.PATTERN_PLACEHOLDER.matcher(sql);
        Iterator<ParameterMapping> iterator = boundSql.getParameterMappings().iterator();
        MetaObject parameterMetaObject = MyBatisUtils.getMetaObject(boundSql.getParameterObject());
        boolean isUpperCase = Character.isUpperCase(sql.charAt(0));

        int offset = 0;
        StringBuilder sqlBuilder = new StringBuilder();
        while (matcher.find() & iterator.hasNext()) {
            try {
                if (parameterMetaObject.getValue(iterator.next().getProperty()) != null) continue;
            } catch (Exception ignored) {
                continue;
            }
            iterator.remove();

            String substring = sql.substring(offset, matcher.end());
            int before = substring.length();
            substring = substring.replaceFirst(" ?!= *\\?$| ?<> *\\?$",
                    isUpperCase ? " IS NOT NULL" : " is not null");
            if (substring.length() == before) {
                substring = substring.replaceFirst(" ?= *\\?$", isUpperCase ? " IS NULL" : " is null");
            }
            sqlBuilder.append(substring);
            offset = matcher.end();
        }
        sqlBuilder.append(sql, offset, sql.length());
        metaObject.setValue("delegate.boundSql.sql", sqlBuilder.toString());
    }
}
 
Example 12
Source File: TableEnhancement.java    From mybatis-boost with MIT License 5 votes vote down vote up
@Override
public void replace(Connection connection, MetaObject metaObject, MappedStatement mappedStatement, BoundSql boundSql) {
    String sql = boundSql.getSql();
    if (sql.contains("#t")) {
        Class<?> entityType = MapperUtils.getEntityTypeFromMapper
                (mappedStatement.getId().substring(0, mappedStatement.getId().lastIndexOf('.')));
        metaObject.setValue("delegate.boundSql.sql", sql.replace("#t",
                EntityUtils.getTableName(entityType, configuration.getNameAdaptor())));
    }
}
 
Example 13
Source File: DataPermissionInterceptor.java    From DataPermissionHelper with Apache License 2.0 5 votes vote down vote up
@Override
public Object intercept(Invocation invocation) throws Throwable {
    Object[] args = invocation.getArgs();
    MappedStatement mappedStatement = (MappedStatement) args[0];
    Object parameter = args[1];
    //从当前线程获取需要进行数据权限控制的业务
    DataPermission dataPermission = DPHelper.getLocalDataPermissions();
    //判断有没有进行数据权限控制,是不是最高权限的管理员(这里指的是数据权限的白名单用户)
    if (dataPermission != null && dataPermission.getAdmin() == false) {
        BoundSql boundSql = mappedStatement.getBoundSql(parameter);
        String sql = boundSql.getSql();
        //获得方法类型
        Select select = (Select) CCJSqlParserUtil.parse(sql);
        select.getSelectBody().accept(new SelectVisitorImpl());
        //判断当前sql是否被修改
        if (DPHelper.getChangeTable()) {
            //访问各个visitor
            //TODO:解析动态sql会失败
            BoundSql newBoundSql = new BoundSql(mappedStatement.getConfiguration(), select.toString(), boundSql
                    .getParameterMappings(), parameter);
            String newMsId = mappedStatement.getId() + DATA_PERMISSION;
            MappedStatement newMs = copyFromMappedStatement(mappedStatement, new BoundSqlSqlSource(newBoundSql), newMsId);
            args[0] = newMs;
            DPHelper.clearChangeTable();
        }
    }
    return invocation.proceed();
}
 
Example 14
Source File: SqlServerRowBoundsDialect.java    From Mybatis-PageHelper with MIT License 5 votes vote down vote up
@Override
public String getCountSql(MappedStatement ms, BoundSql boundSql, Object parameterObject, RowBounds rowBounds, CacheKey countKey) {
    String sql = boundSql.getSql();
    sql = replaceSql.replace(sql);
    sql = countSqlParser.getSmartCountSql(sql);
    sql = replaceSql.restore(sql);
    return sql;
}
 
Example 15
Source File: DataScopeInterceptor.java    From MeetingFilm with Apache License 2.0 5 votes vote down vote up
@Override
public Object intercept(Invocation invocation) throws Throwable {
    StatementHandler statementHandler = (StatementHandler) PluginUtils.realTarget(invocation.getTarget());
    MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler);
    MappedStatement mappedStatement = (MappedStatement) metaStatementHandler.getValue("delegate.mappedStatement");

    if (!SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType())) {
        return invocation.proceed();
    }

    BoundSql boundSql = (BoundSql) metaStatementHandler.getValue("delegate.boundSql");
    String originalSql = boundSql.getSql();
    Object parameterObject = boundSql.getParameterObject();

    //查找参数中包含DataScope类型的参数
    DataScope dataScope = findDataScopeObject(parameterObject);

    if (dataScope == null) {
        return invocation.proceed();
    } else {
        String scopeName = dataScope.getScopeName();
        List<Integer> deptIds = dataScope.getDeptIds();
        String join = CollectionKit.join(deptIds, ",");
        originalSql = "select * from (" + originalSql + ") temp_data_scope where temp_data_scope." + scopeName + " in (" + join + ")";
        metaStatementHandler.setValue("delegate.boundSql.sql", originalSql);
        return invocation.proceed();
    }
}
 
Example 16
Source File: DataScopeInterceptor.java    From smaker with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Object intercept(Invocation invocation) throws Throwable {
	StatementHandler statementHandler = (StatementHandler) PluginUtils.realTarget(invocation.getTarget());
	MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
	this.sqlParser(metaObject);
	// 先判断是不是SELECT操作
	MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
	if (!SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType())) {
		return invocation.proceed();
	}

	BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql");
	String originalSql = boundSql.getSql();
	Object parameterObject = boundSql.getParameterObject();

	//查找参数中包含DataScope类型的参数
	DataScope dataScope = findDataScopeObject(parameterObject);

	if (dataScope == null) {
		return invocation.proceed();
	} else {
		String scopeName = dataScope.getScopeName();
		List<Integer> deptIds = dataScope.getDeptIds();
		if (StrUtil.isNotBlank(scopeName) && CollectionUtil.isNotEmpty(deptIds)) {
			String join = CollectionUtil.join(deptIds, ",");
			originalSql = "select * from (" + originalSql + ") temp_data_scope where temp_data_scope." + scopeName + " in (" + join + ")";
			metaObject.setValue("delegate.boundSql.sql", originalSql);
		}
		return invocation.proceed();
	}
}
 
Example 17
Source File: LikeParameterEscapeHandler.java    From sqlhelper with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void inbound(HandlerContext ctx) throws Throwable {
    ExecutorInvocation executorInvocation = (ExecutorInvocation) ctx.getPipeline().getTarget();
    MappedStatement mappedStatement = executorInvocation.getMappedStatement();

    if (!MybatisUtils.isQueryStatement(mappedStatement) || !MybatisUtils.isPreparedStatement(mappedStatement) || !isEnableLikeEscape()) {
        Pipelines.skipHandler(ctx, true);
        return;
    }

    // notice: the sqlcontext, sqlrequest is not null
    SqlRequestContext sqlContext = SqlRequestContextHolder.getInstance().get();
    LikeEscaper likeEscaper = getLikeEscaper(mappedStatement, sqlContext.getRequest(), executorInvocation);
    if (Objects.isNull(likeEscaper)) {
        logger.warn("Can't find a suitable LikeEscaper for the sql request: {}, statement id: {}", sqlContext.getRequest(), mappedStatement.getId());
        Pipelines.skipHandler(ctx, true);
        return;
    }
    sqlContext.getRequest().setLikeEscaper(likeEscaper);
    BoundSql boundSql = executorInvocation.getBoundSql();
    String sql = boundSql.getSql();
    Pair<List<Integer>, List<Integer>> pair = LikeEscapers.findEscapedSlots(sql);
    if (Emptys.isEmpty(pair.getKey()) && Emptys.isEmpty(pair.getValue())) {
        Pipelines.skipHandler(ctx, true);
        return;
    }

    String newSql = LikeEscapers.insertLikeEscapeDeclares(sql, pair.getValue(), likeEscaper);
    sqlContext.getRequest().setLikeParameterIndexes(pair.getKey());
    if (logger.isDebugEnabled()) {
        SqlDmlFormatter sqlDmlFormatter = new SqlDmlFormatter();
        logger.debug("After like escape, the sql {} become: {}", sqlDmlFormatter.format(sql), sqlDmlFormatter.format(newSql));
    }
    // rebuild a BoundSql
    boundSql = MybatisUtils.rebuildBoundSql(newSql, mappedStatement.getConfiguration(), boundSql);
    executorInvocation.setBoundSql(boundSql);
    sqlContext.set(MybatisSqlRequestContextKeys.LIKE_ESCAPE_PARAMETERS_INDEXES, pair.getKey());
    sqlContext.set(MybatisSqlRequestContextKeys.LIKE_ESCAPER, likeEscaper);
    Pipelines.inbound(ctx);
}
 
Example 18
Source File: PageStatementInterceptor.java    From joyqueue with Apache License 2.0 4 votes vote down vote up
@Override
public Object intercept(Invocation invocation) throws Throwable {
    StatementHandler handler = (StatementHandler) invocation.getTarget();

    // 获取MappedStatement,Configuration对象
    MetaObject metaObject =
            MetaObject.forObject(handler, new DefaultObjectFactory(), new DefaultObjectWrapperFactory(), new DefaultReflectorFactory());
    MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
    String statement = mappedStatement.getId();
    if (!isPageSql(statement,metaObject.getValue("boundSql.parameterObject"))) {
        return invocation.proceed();
    }

    Configuration configuration = (Configuration) metaObject.getValue("delegate.configuration");
    Executor executor = (Executor) metaObject.getValue("delegate.executor");

    // 获取分页参数
    BoundSql boundSql = handler.getBoundSql();
    QPageQuery pageQuery = (QPageQuery) boundSql.getParameterObject();
    String countStatement = buildCountStatement(statement);
    List<Integer> counts = executor.query(configuration.
            getMappedStatement(countStatement), pageQuery, RowBounds.DEFAULT, null);

    int count = 0;
    if (counts != null && !counts.isEmpty()) {
        count = counts.get(0) == null ? 0 : counts.get(0);
    }

    if (pageQuery.getPagination() == null) {
        pageQuery.setPagination(new Pagination());
    }
    pageQuery.getPagination().setTotalRecord(count);

    String sql = boundSql.getSql();
    if (logger.isDebugEnabled()) {
        logger.debug("raw SQL : " + sql);
    }

    if (sql == null || sql.isEmpty() || sql.contains(" limit ")) {
        return invocation.proceed();
    }

    String originalSql = (String) metaObject.getValue("delegate.boundSql.sql");
    metaObject.setValue("delegate.boundSql.sql",
            getLimitString(originalSql, pageQuery.getPagination().getStart(),
                    pageQuery.getPagination().getSize()));
    metaObject.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET);
    metaObject.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT);

    if (logger.isDebugEnabled()) {
        logger.debug("pagination SQL : " + sql);
    }
    return invocation.proceed();
}
 
Example 19
Source File: SQLRouterInterceptor.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public Object intercept(Invocation invocation) throws Throwable {
	Object result = null;

	if (invocation.getTarget() instanceof RoutingStatementHandler) {
		RoutingStatementHandler statementHandler = (RoutingStatementHandler) invocation.getTarget();
		StatementHandler delegate = (StatementHandler) ReflectionUtil.getFieldValue(statementHandler, "delegate");
		BoundSql boundSql = delegate.getBoundSql();

		// 拦截到的prepare方法参数是一个Connection对象
		Connection connection = (Connection) invocation.getArgs()[0];
		String sql = boundSql.getSql();

		// 1、 如果前端有指定路由则,优先采用指定的路由
		if (RouterContext.containsKey(RouterConsts.ROUTER_KEY) && !hasAnnotation(sql)) {
			RouterInfo routerInfo = RouterContext.get(RouterConsts.ROUTER_KEY);
			sql = new StringBuilder(routerInfo.getSqlrouter()).append(sql).toString();

			// 利用反射设置当前BoundSql对应的sql属性为我们建立好的分页Sql语句
			ReflectionUtil.setFieldValue(boundSql, "sql", sql);
		}

		// 2、如果没有路由信息,对只读事物添加读取从库的路由
		if (!hasAnnotation(sql) && connection.isReadOnly()) {

			// 获取当前要执行的Sql语句,也就是我们直接在Mapper映射语句中写的Sql语句
			sql = msHint.genRouteInfo(MasterSlaveHint.SLAVE, sql);

			// 利用反射设置当前BoundSql对应的sql属性为我们建立好的分页Sql语句
			ReflectionUtil.setFieldValue(boundSql, "sql", sql);
		}

		result = invocation.proceed();

		// if (isDML(sql) &&
		// !RouterContext.containsKey(RouterConsts.ROUTER_KEY)) {
		// 如果更改执行的是DML语句,则设置后续的CRUD路由到master
		// String appName = PropertiesUtil.get("app.name");
		// //TODO这里可能需要针对应用来进行主从路由,存在一条链路路由到多个应用的不同DB中
		// RouterContext.put(RouterConsts.ROUTER_KEY,
		// msHint.getRouteMasterHint());
		// }

	} else {
		result = invocation.proceed();
	}

	return result;
}
 
Example 20
Source File: PaginationInterceptor.java    From platform with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Object intercept(Invocation invocation) throws Throwable {
    Executor executor = MyBatisUtils.getRealTarget(invocation.getTarget());
    Object[] args = invocation.getArgs();
    MappedStatement mappedStatement = (MappedStatement) args[MAPPED_STATEMENT_INDEX];

    // 查询操作或者存储过程,无需分页
    if (SqlCommandType.SELECT != mappedStatement.getSqlCommandType()
            || StatementType.CALLABLE == mappedStatement.getStatementType()) {
        return invocation.proceed();
    }

    Object parameterObject = invocation.getArgs()[PARAMETER_INDEX];

    // 不包含PageableRequest或者Pageable参数时,无需分页
    PageableRequest<?> pageableRequest = MyBatisUtils.findPageableRequest(parameterObject).orElse(null);
    if (null == pageableRequest || pageableRequest.getPageable() == null ||
            pageableRequest.getPageable().getPageSize() < 0) {
        return invocation.proceed();
    }

    Connection connection = executor.getTransaction().getConnection();

    DbType dbType = this.dbType == null ? JdbcUtils.getDbType(connection) : this.dbType;
    DbDialect dialect = Optional.ofNullable(this.dbDialect).orElseGet(() -> JdbcUtils.getDialect(dbType));

    // 针对定义了rowBounds,做为mapper接口方法的参数
    BoundSql boundSql = mappedStatement.getBoundSql(parameterObject);
    String originalSql = boundSql.getSql();

    // 查询总记录数
    long total = 0;
    if (pageableRequest.isQueryTotalCount()) {
        String countSql = dialect.buildCountSql(boundSql.getSql());

        total = this.queryTotal(countSql, mappedStatement, boundSql, connection);

        if (total <= 0) {
            return null;
        }
    }

    Pageable pageable = pageableRequest.getPageable();
    String buildSql = concatOrderBy(originalSql, pageable);
    String paginationSql = dialect.buildPaginationSql(buildSql, pageable.getOffset(), pageable.getPageSize());

    BoundSql newBs = MyBatisUtils.copyFromBoundSql(mappedStatement, boundSql, paginationSql, parameterObject);
    MappedStatement newMs = MyBatisUtils.copyFromMappedStatement(mappedStatement, new BoundSqlSqlSource(newBs));
    args[MAPPED_STATEMENT_INDEX] = newMs;
    args[ROWBOUNDS_INDEX] = RowBounds.DEFAULT;

    Object result = invocation.proceed();
    pageableRequest.setTotal(total);
    pageableRequest.setRecords((List) result);
    return result;
}