Java Code Examples for org.apache.ibatis.mapping.MappedStatement#getConfiguration()
The following examples show how to use
org.apache.ibatis.mapping.MappedStatement#getConfiguration() .
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: BatchExecutor.java From mybatis with Apache License 2.0 | 6 votes |
@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 2
Source File: PagePluging.java From aaden-pay with Apache License 2.0 | 6 votes |
private MappedStatement copyFromMappedStatement(MappedStatement ms, SqlSource newSqlSource) { Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), newSqlSource, ms.getSqlCommandType()); builder.resource(ms.getResource()); builder.fetchSize(ms.getFetchSize()); builder.statementType(ms.getStatementType()); builder.keyGenerator(ms.getKeyGenerator()); // builder.keyProperty((ms.getKeyProperty())); builder.keyProperty(arrayToStr(ms.getKeyProperties())); // setStatementTimeout() builder.timeout(ms.getTimeout()); // setStatementResultMap() builder.parameterMap(ms.getParameterMap()); // setStatementResultMap() builder.resultMaps(ms.getResultMaps()); builder.resultSetType(ms.getResultSetType()); // setStatementCache() builder.cache(ms.getCache()); builder.flushCacheRequired(ms.isFlushCacheRequired()); builder.useCache(ms.isUseCache()); return builder.build(); }
Example 3
Source File: BatchExecutor.java From mybaties with Apache License 2.0 | 6 votes |
@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 4
Source File: BatchExecutor.java From mybatis with Apache License 2.0 | 6 votes |
@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 5
Source File: SimpleExecutor.java From mybatis with Apache License 2.0 | 6 votes |
@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 6
Source File: ExecutorUtil.java From Mybatis-PageHelper with MIT License | 6 votes |
/** * 分页查询 * * @param dialect * @param executor * @param ms * @param parameter * @param rowBounds * @param resultHandler * @param boundSql * @param cacheKey * @param <E> * @return * @throws SQLException */ public static <E> List<E> pageQuery(Dialect dialect, Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql, CacheKey cacheKey) throws SQLException { //判断是否需要进行分页查询 if (dialect.beforePage(ms, parameter, rowBounds)) { //生成分页的缓存 key CacheKey pageKey = cacheKey; //处理参数对象 parameter = dialect.processParameterObject(ms, parameter, boundSql, pageKey); //调用方言获取分页 sql String pageSql = dialect.getPageSql(ms, boundSql, parameter, rowBounds, pageKey); BoundSql pageBoundSql = new BoundSql(ms.getConfiguration(), pageSql, boundSql.getParameterMappings(), parameter); Map<String, Object> additionalParameters = getAdditionalParameter(boundSql); //设置动态参数 for (String key : additionalParameters.keySet()) { pageBoundSql.setAdditionalParameter(key, additionalParameters.get(key)); } //执行分页查询 return executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, pageKey, pageBoundSql); } else { //不执行分页的情况下,也不执行内存分页 return executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, cacheKey, boundSql); } }
Example 7
Source File: MyBatisUtils.java From platform with Apache License 2.0 | 6 votes |
/** * 复制BoundSql * * @param mappedStatement ${@link MappedStatement} * @param boundSql ${@link BoundSql} * @param sql SQL * @param parameterMappings 参数映射 * @param parameter 参数 * @return {@link BoundSql} */ public static BoundSql copyFromBoundSql(MappedStatement mappedStatement, BoundSql boundSql, String sql, Object parameter) { List<ParameterMapping> parameterMappings = new ArrayList<>(boundSql.getParameterMappings()); BoundSql newBoundSql = new BoundSql(mappedStatement.getConfiguration(), sql, parameterMappings, parameter); for (ParameterMapping mapping : boundSql.getParameterMappings()) { String prop = mapping.getProperty(); if (boundSql.hasAdditionalParameter(prop)) { newBoundSql.setAdditionalParameter(prop, boundSql.getAdditionalParameter(prop)); } } return newBoundSql; }
Example 8
Source File: BaseStatementHandler.java From mybatis with Apache License 2.0 | 6 votes |
protected BaseStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) { this.configuration = mappedStatement.getConfiguration(); this.executor = executor; this.mappedStatement = mappedStatement; this.rowBounds = rowBounds; this.typeHandlerRegistry = configuration.getTypeHandlerRegistry(); this.objectFactory = configuration.getObjectFactory(); if (boundSql == null) { // issue #435, get the key before calculating the statement generateKeys(parameterObject); boundSql = mappedStatement.getBoundSql(parameterObject); } this.boundSql = boundSql; //生成parameterHandler this.parameterHandler = configuration.newParameterHandler(mappedStatement, parameterObject, boundSql); //生成resultSetHandler this.resultSetHandler = configuration.newResultSetHandler(executor, mappedStatement, rowBounds, parameterHandler, resultHandler, boundSql); }
Example 9
Source File: SimpleExecutor.java From mybaties with Apache License 2.0 | 6 votes |
@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 10
Source File: SundialInterceptor.java From Milkomeda with MIT License | 6 votes |
private void updateSql(String sql, Invocation invocation, MappedStatement ms, Object[] args, BoundSql boundSql) { BoundSql boundSqlNew = new BoundSql(ms.getConfiguration(), sql, boundSql.getParameterMappings(), boundSql.getParameterObject()); MappedStatement mappedStatement = copyFrom(ms, new BoundSqlSqlSource(boundSqlNew)); // 替换映射的语句 args[0] = mappedStatement; // 针对查询方式的参数替换 if (ms.getSqlCommandType() == SqlCommandType.SELECT) { Executor executor = (Executor) invocation.getTarget(); Object parameter = args[1]; RowBounds rowBounds = (RowBounds) args[2]; // 6个参数时(因为分页插件的原因导致问题,需要修改对应的类型值) if (args.length == 6) { args[4] = executor.createCacheKey(ms, parameter, rowBounds, boundSql); args[5] = boundSqlNew; } } }
Example 11
Source File: PaginationHandler.java From azeroth with Apache License 2.0 | 6 votes |
@SuppressWarnings("rawtypes") private Long executeQueryCount(Executor executor, MappedStatement countMs, Object parameter, BoundSql boundSql, RowBounds rowBounds, ResultHandler resultHandler) throws IllegalAccessException, SQLException { CacheKey countKey = executor.createCacheKey(countMs, parameter, RowBounds.DEFAULT, boundSql); String orignSql = boundSql.getSql().replaceAll(";$", ""); // count sql String countSql = PageSqlUtils.getCountSql(orignSql); BoundSql countBoundSql = new BoundSql(countMs.getConfiguration(), countSql, boundSql.getParameterMappings(), parameter); // 执行 count 查询 Object countResultList = executor.query(countMs, parameter, RowBounds.DEFAULT, resultHandler, countKey, countBoundSql); Long count = (Long) ((List) countResultList).get(0); return count; }
Example 12
Source File: SimpleExecutor.java From mybatis with Apache License 2.0 | 6 votes |
@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: 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 14
Source File: ReuseExecutor.java From mybaties with Apache License 2.0 | 5 votes |
@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 15
Source File: DefaultResultSetHandler.java From mybaties with Apache License 2.0 | 5 votes |
public DefaultResultSetHandler(Executor executor, MappedStatement mappedStatement, ParameterHandler parameterHandler, ResultHandler resultHandler, BoundSql boundSql, RowBounds rowBounds) { this.executor = executor; this.configuration = mappedStatement.getConfiguration(); this.mappedStatement = mappedStatement; this.rowBounds = rowBounds; this.parameterHandler = parameterHandler; this.boundSql = boundSql; this.typeHandlerRegistry = configuration.getTypeHandlerRegistry(); this.objectFactory = configuration.getObjectFactory(); this.resultHandler = resultHandler; }
Example 16
Source File: SQLHelper.java From Shop-for-JavaWeb with MIT License | 5 votes |
/** * 对SQL参数(?)设值,参考org.apache.ibatis.executor.parameter.DefaultParameterHandler * * @param ps 表示预编译的 SQL 语句的对象。 * @param mappedStatement MappedStatement * @param boundSql SQL * @param parameterObject 参数对象 * @throws java.sql.SQLException 数据库异常 */ @SuppressWarnings("unchecked") public static void setParameters(PreparedStatement ps, MappedStatement mappedStatement, BoundSql boundSql, Object parameterObject) throws SQLException { ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId()); List<ParameterMapping> parameterMappings = boundSql.getParameterMappings(); if (parameterMappings != null) { Configuration configuration = mappedStatement.getConfiguration(); TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry(); MetaObject metaObject = parameterObject == null ? null : configuration.newMetaObject(parameterObject); for (int i = 0; i < parameterMappings.size(); i++) { ParameterMapping parameterMapping = parameterMappings.get(i); if (parameterMapping.getMode() != ParameterMode.OUT) { Object value; String propertyName = parameterMapping.getProperty(); PropertyTokenizer prop = new PropertyTokenizer(propertyName); if (parameterObject == null) { value = null; } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) { value = parameterObject; } else if (boundSql.hasAdditionalParameter(propertyName)) { value = boundSql.getAdditionalParameter(propertyName); } else if (propertyName.startsWith(ForEachSqlNode.ITEM_PREFIX) && boundSql.hasAdditionalParameter(prop.getName())) { value = boundSql.getAdditionalParameter(prop.getName()); if (value != null) { value = configuration.newMetaObject(value).getValue(propertyName.substring(prop.getName().length())); } } else { value = metaObject == null ? null : metaObject.getValue(propertyName); } @SuppressWarnings("rawtypes") TypeHandler typeHandler = parameterMapping.getTypeHandler(); if (typeHandler == null) { throw new ExecutorException("There was no TypeHandler found for parameter " + propertyName + " of statement " + mappedStatement.getId()); } typeHandler.setParameter(ps, i + 1, value, parameterMapping.getJdbcType()); } } } }
Example 17
Source File: SqlLogInterceptor.java From paascloud-master with Apache License 2.0 | 5 votes |
/** * Intercept object. * * @param invocation the invocation * * @return the object * * @throws Throwable the throwable */ @Override public Object intercept(Invocation invocation) throws Throwable { long start = System.currentTimeMillis(); MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; Object parameter = null; if (invocation.getArgs().length > 1) { parameter = invocation.getArgs()[1]; } BoundSql boundSql = mappedStatement.getBoundSql(parameter); Configuration configuration = mappedStatement.getConfiguration(); String sql = boundSql.getSql().replaceAll("[\\s]+", " "); List<String> paramList = getParamList(configuration, boundSql); Object proceed = invocation.proceed(); int result = 0; if (proceed instanceof ArrayList) { ArrayList resultList = (ArrayList) proceed; result = resultList.size(); } if (proceed instanceof Integer) { result = (Integer) proceed; } if (enableSqlLogInterceptor) { long end = System.currentTimeMillis(); long time = end - start; Boolean flag = (Boolean) ThreadLocalMap.get(NotDisplaySqlAspect.DISPLAY_SQL); if (time >= noticeTime * GlobalConstant.Number.THOUSAND_INT) { log.error("执行超过{}秒,sql={}", noticeTime, sql); log.error("result={}, time={}ms, params={}", result, time, paramList); return proceed; } if (flag == null || Objects.equals(flag, true)) { log.info("sql={}", sql); log.info("result={},time={}ms, params={}", result, time, paramList); } } return proceed; }
Example 18
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(); }
Example 19
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 20
Source File: AutoMapperInterceptor.java From mybatis.flying with Apache License 2.0 | 4 votes |
@SuppressWarnings("unchecked") private void setParameters(PreparedStatement ps, MappedStatement mappedStatement, BoundSql boundSql, Object parameterObject) throws SQLException { ErrorContext.instance().activity(SETTING_PARAMETERS).object(mappedStatement.getParameterMap().getId()); List<ParameterMapping> parameterMappings = boundSql.getParameterMappings(); if (parameterMappings != null) { Configuration configuration = mappedStatement.getConfiguration(); TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry(); MetaObject metaObject = parameterObject == null ? null : configuration.newMetaObject(parameterObject); for (int i = 0; i < parameterMappings.size(); i++) { ParameterMapping parameterMapping = parameterMappings.get(i); if (parameterMapping.getMode() != ParameterMode.OUT) { Object value; String propertyName = parameterMapping.getProperty(); PropertyTokenizer prop = new PropertyTokenizer(propertyName); if (parameterObject == null) { value = null; } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) { value = parameterObject; } else if (boundSql.hasAdditionalParameter(propertyName)) { value = boundSql.getAdditionalParameter(propertyName); } else if (propertyName.startsWith(ForEachSqlNode.ITEM_PREFIX) && boundSql.hasAdditionalParameter(prop.getName())) { value = boundSql.getAdditionalParameter(prop.getName()); if (value != null) { value = configuration.newMetaObject(value) .getValue(propertyName.substring(prop.getName().length())); } } else { value = metaObject == null ? null : metaObject.getValue(propertyName); } TypeHandler<Object> typeHandler = (TypeHandler<Object>) parameterMapping.getTypeHandler(); if (typeHandler == null) { throw new AutoMapperException( new StringBuffer(AutoMapperExceptionEnum.NO_TYPE_HANDLER_SUITABLE.toString()) .append(propertyName).append(" of statement ").append(mappedStatement.getId()) .toString()); } typeHandler.setParameter(ps, i + 1, value, parameterMapping.getJdbcType()); } } } }