org.apache.ibatis.executor.statement.StatementHandler Java Examples

The following examples show how to use org.apache.ibatis.executor.statement.StatementHandler. 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: 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 #2
Source File: SimpleExecutor.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Override
public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
  Statement stmt = null;
  try {
    Configuration configuration = ms.getConfiguration();
    //新建一个StatementHandler
    //这里看到ResultHandler传入了
    StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
    //准备语句
    stmt = prepareStatement(handler, ms.getStatementLog());
    //StatementHandler.query
    return handler.<E>query(stmt, resultHandler);
  } finally {
    closeStatement(stmt);
  }
}
 
Example #3
Source File: PageInterceptor.java    From dubbo-mock with Apache License 2.0 6 votes vote down vote up
/**
 * 拦截后要执行的方法
 */
@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();
}
 
Example #4
Source File: SimpleExecutor.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Override
public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {
  Statement stmt = null;
  try {
    Configuration configuration = ms.getConfiguration();
    //新建一个StatementHandler
    //这里看到ResultHandler传入的是null
    StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null, null);
    //准备语句
    stmt = prepareStatement(handler, ms.getStatementLog());
    //StatementHandler.update
    return handler.update(stmt);
  } finally {
    closeStatement(stmt);
  }
}
 
Example #5
Source File: BatchExecutor.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Override
public <E> List<E> doQuery(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql)
    throws SQLException {
  Statement stmt = null;
  try {
    flushStatements();
    Configuration configuration = ms.getConfiguration();
    StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameterObject, rowBounds, resultHandler, boundSql);
    Connection connection = getConnection(ms.getStatementLog());
    stmt = handler.prepare(connection);
    handler.parameterize(stmt);
    return handler.<E>query(stmt, resultHandler);
  } finally {
    closeStatement(stmt);
  }
}
 
Example #6
Source File: BatchExecutor.java    From mybaties 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 #7
Source File: ReuseExecutor.java    From mybaties 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 #8
Source File: RecordInterceptor.java    From Aooms with Apache License 2.0 6 votes vote down vote up
public Object intercept(Invocation invocation) throws Throwable {
    //StatementHandler statementHandler = (StatementHandler)realTarget(invocation.getTarget());
    //MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
    //Object parameterObject = metaObject.getValue("delegate.boundSql.parameterObject");
    //MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");

    StatementHandler target = MetaObjectAssistant.getTarget(invocation, StatementHandler.class);
    MetaObject metaObject = MetaObjectAssistant.getMetaObject(target);
    Object parameterObject = MetaObjectAssistant.getParameterObject(metaObject);
    if(parameterObject == null) return invocation.proceed();

    // parameterObject is not a vo , skip RecordInterceptor
    if(parameterObject.getClass() != Record.class){
        return invocation.proceed();
    }

    // vo oper process
    IRecordOper recordOper = recordOperRouting.route(metaObject);
    recordOper.process();

    Object value = invocation.proceed();
    return value;
}
 
Example #9
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 #10
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 #11
Source File: BatchExecutor.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Override
public <E> List<E> doQuery(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql)
    throws SQLException {
  Statement stmt = null;
  try {
    flushStatements();
    Configuration configuration = ms.getConfiguration();
    StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameterObject, rowBounds, resultHandler, boundSql);
    Connection connection = getConnection(ms.getStatementLog());
    stmt = handler.prepare(connection);
    handler.parameterize(stmt);
    return handler.<E>query(stmt, resultHandler);
  } finally {
    closeStatement(stmt);
  }
}
 
Example #12
Source File: SimpleExecutor.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Override
public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {
  Statement stmt = null;
  try {
    Configuration configuration = ms.getConfiguration();
    //新建一个StatementHandler
    //这里看到ResultHandler传入的是null
    StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null, null);
    //准备语句
    stmt = prepareStatement(handler, ms.getStatementLog());
    //StatementHandler.update
    return handler.update(stmt);
  } finally {
    closeStatement(stmt);
  }
}
 
Example #13
Source File: SimpleExecutor.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Override
public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
  Statement stmt = null;
  try {
    Configuration configuration = ms.getConfiguration();
    //新建一个StatementHandler
    //这里看到ResultHandler传入了
    StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
    //准备语句
    stmt = prepareStatement(handler, ms.getStatementLog());
    //StatementHandler.query
    return handler.<E>query(stmt, resultHandler);
  } finally {
    closeStatement(stmt);
  }
}
 
Example #14
Source File: DataPermissionInterceptor.java    From FEBS-Cloud with Apache License 2.0 6 votes vote down vote up
@Override
public Object intercept(Invocation invocation) throws Throwable {
    StatementHandler statementHandler = PluginUtils.realTarget(invocation.getTarget());
    MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
    this.sqlParser(metaObject);
    MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");

    BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql");
    Object paramObj = boundSql.getParameterObject();
    // 数据权限只针对查询语句
    if (SqlCommandType.SELECT == mappedStatement.getSqlCommandType()) {
        DataPermission dataPermission = getDataPermission(mappedStatement);
        if (shouldFilter(mappedStatement, dataPermission)) {
            String id = mappedStatement.getId();
            log.info("\n 数据权限过滤 method -> {}", id);
            String originSql = boundSql.getSql();
            String dataPermissionSql = dataPermissionSql(originSql, dataPermission);
            metaObject.setValue("delegate.boundSql.sql", dataPermissionSql);
            log.info("\n originSql -> {} \n dataPermissionSql: {}", originSql, dataPermissionSql);
        }
    }
    return invocation.proceed();
}
 
Example #15
Source File: PageInterceptor.java    From QuickProject 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();
	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();
}
 
Example #16
Source File: ReuseExecutor.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Override
public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {
  Configuration configuration = ms.getConfiguration();
  //和SimpleExecutor一样,新建一个StatementHandler
  //这里看到ResultHandler传入的是null
  StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null, null);
  //准备语句
  Statement stmt = prepareStatement(handler, ms.getStatementLog());
  return handler.update(stmt);
}
 
Example #17
Source File: ReuseExecutor.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Override
public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
  Configuration configuration = ms.getConfiguration();
  StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
  Statement stmt = prepareStatement(handler, ms.getStatementLog());
  return handler.<E>query(stmt, resultHandler);
}
 
Example #18
Source File: RWPlugin.java    From spring-boot-mybatis-rw with Apache License 2.0 5 votes vote down vote up
public Object plugin(Object target) {
	if (target instanceof StatementHandler) {  
           return Plugin.wrap(target, this);  
       } else {  
           return target;  
       }  
}
 
Example #19
Source File: SimpleExecutor.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
  Statement stmt;
  Connection connection = getConnection(statementLog);
  //调用StatementHandler.prepare
  stmt = handler.prepare(connection);
  //调用StatementHandler.parameterize
  handler.parameterize(stmt);
  return stmt;
}
 
Example #20
Source File: EasyMybatisPaginationPlugin.java    From EasyEE with MIT License 5 votes vote down vote up
public Object plugin(Object target) {
	if (target instanceof StatementHandler) {
		return Plugin.wrap(target, this);
	} else if (target instanceof Executor) {
		return Plugin.wrap(target, this);
	} else {
		return target;
	}
}
 
Example #21
Source File: EasyMybatisPaginationPlugin.java    From EasyEE with MIT License 5 votes vote down vote up
public Object plugin(Object target) {
	if (target instanceof StatementHandler) {
		return Plugin.wrap(target, this);
	} else if (target instanceof Executor) {
		return Plugin.wrap(target, this);
	} else {
		return target;
	}
}
 
Example #22
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 #23
Source File: QueryInformation.java    From sinavi-jfw with Apache License 2.0 5 votes vote down vote up
/**
 * コンストラクタです。
 * MyBatisの{@link StatementHandler}の解析に失敗した場合に{@link InternalException}をスローします。
 * @param statementHandler {@link StatementHandler}
 */
public QueryInformation(StatementHandler statementHandler) {
    try {
        this.typeHandlerRegistry = new TypeHandlerRegistry();
        this.statementHandler = getConcreteStatementHandler(statementHandler);
        this.mappedStatement = getMappedStatement(this.statementHandler);
        this.boundSql = this.statementHandler.getBoundSql();
        this.parameterObject = this.boundSql.getParameterObject();
        this.parameterMappingList = boundSql.getParameterMappings();
        this.sqlCommandType = mappedStatement.getSqlCommandType();
        this.statementType = mappedStatement.getStatementType();
        this.metaObject = parameterObject == null ? null : MetaObject.forObject(parameterObject, new DefaultObjectFactory(), new DefaultObjectWrapperFactory());

        switch (statementType) {
            case STATEMENT:
                this.query = boundSql.getSql();
                break;
            case PREPARED:
                this.preparedQuery = boundSql.getSql();
                break;
            case CALLABLE:
                this.callableQuery = boundSql.getSql();
                break;
            default:
                throw new InternalException(QueryInformation.class, "");
        }
    } catch (Exception e) {
        throw new InternalException(QueryInformation.class, "E-JDBC-MYBATIS#0002", e);
    }
}
 
Example #24
Source File: QueryLoggingInterceptor.java    From sinavi-jfw with Apache License 2.0 5 votes vote down vote up
/**
 * {@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 #25
Source File: ReuseExecutor.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Override
public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {
  Configuration configuration = ms.getConfiguration();
  //和SimpleExecutor一样,新建一个StatementHandler
  //这里看到ResultHandler传入的是null
  StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null, null);
  //准备语句
  Statement stmt = prepareStatement(handler, ms.getStatementLog());
  return handler.update(stmt);
}
 
Example #26
Source File: ReuseExecutor.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Override
public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
  Configuration configuration = ms.getConfiguration();
  StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
  Statement stmt = prepareStatement(handler, ms.getStatementLog());
  return handler.<E>query(stmt, resultHandler);
}
 
Example #27
Source File: SimpleExecutor.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
  Statement stmt;
  Connection connection = getConnection(statementLog);
  //调用StatementHandler.prepare
  stmt = handler.prepare(connection);
  //调用StatementHandler.parameterize
  handler.parameterize(stmt);
  return stmt;
}
 
Example #28
Source File: PageInterceptor.java    From everyone-java-blog with Apache License 2.0 5 votes vote down vote up
@Override
public Object plugin(Object target) {
    if (target instanceof StatementHandler) {
        return Plugin.wrap(target, this);
    } else if (target instanceof ResultSetHandler) {
        return Plugin.wrap(target, this);
    } else {
        return target;
    }
}
 
Example #29
Source File: DataScopeInterceptor.java    From smaker with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 生成拦截对象的代理
 *
 * @param target 目标对象
 * @return 代理对象
 */
@Override
public Object plugin(Object target) {
	if (target instanceof StatementHandler) {
		return Plugin.wrap(target, this);
	}
	return target;
}
 
Example #30
Source File: PerformanceInterceptor.java    From seppb with MIT License 5 votes vote down vote up
@Override
public Object intercept(Invocation invocation) throws Throwable {
	Object target = invocation.getTarget();
	Stopwatch started = Stopwatch.createStarted();
	StatementHandler statementHandler = (StatementHandler) target;
	Object obj = invocation.proceed();
	long endTime = started.stop().elapsed(TimeUnit.MILLISECONDS);
	if (properties.getSlowTime() <= endTime) {
		BoundSql boundSql = statementHandler.getBoundSql();
		String sql = boundSql.getSql();
           mysqlSlowQuery.info("执行耗时:{} 毫秒[slowSql]{} ", endTime, sql);
	}
	return obj;
}