当前位置: 首页>>代码示例>>Java>>正文


Java MappedStatement.getBoundSql方法代码示例

本文整理汇总了Java中org.apache.ibatis.mapping.MappedStatement.getBoundSql方法的典型用法代码示例。如果您正苦于以下问题:Java MappedStatement.getBoundSql方法的具体用法?Java MappedStatement.getBoundSql怎么用?Java MappedStatement.getBoundSql使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.ibatis.mapping.MappedStatement的用法示例。


在下文中一共展示了MappedStatement.getBoundSql方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: intercept

import org.apache.ibatis.mapping.MappedStatement; //导入方法依赖的package包/类
public Object intercept(Invocation invocation) throws Throwable {
    /**
     * 处理 DELETE UPDATE 语句
     */
    MappedStatement ms = (MappedStatement) invocation.getArgs()[0];
    if (ms.getSqlCommandType() == SqlCommandType.DELETE || ms.getSqlCommandType() == SqlCommandType.UPDATE) {
        Executor executor = (Executor) invocation.getTarget();
        Configuration configuration = ms.getConfiguration();
        Object parameter = invocation.getArgs()[1];
        BoundSql boundSql = ms.getBoundSql(parameter);
        Connection connection = executor.getTransaction().getConnection();
        String databaseVersion = connection.getMetaData().getDatabaseProductVersion();
        if (GlobalConfigUtils.getDbType(configuration).equals(DBType.MYSQL)
                && VersionUtils.compare(minMySQLVersion, databaseVersion)) {
            logger.warn("Warn: Your mysql version needs to be greater than '5.6.3' to execute of Sql Explain!");
            return invocation.proceed();
        }
        /**
         * 执行 SQL 分析
         */
        sqlExplain(configuration, ms, boundSql, connection, parameter);
    }
    return invocation.proceed();
}
 
开发者ID:Caratacus,项目名称:mybatis-plus-mini,代码行数:25,代码来源:SqlExplainInterceptor.java

示例2: processIntercept

import org.apache.ibatis.mapping.MappedStatement; //导入方法依赖的package包/类
void processIntercept(final Object[] queryArgs) {
	MappedStatement ms = (MappedStatement) queryArgs[MAPPED_STATEMENT_INDEX];
	Object parameter = queryArgs[PARAMETER_INDEX];
	final RowBounds rowBounds = (RowBounds) queryArgs[ROWBOUNDS_INDEX];
	int offset = rowBounds.getOffset();
	int limit = rowBounds.getLimit();
	if (dialect.supportsLimit() && (offset != RowBounds.NO_ROW_OFFSET || limit != RowBounds.NO_ROW_LIMIT)) {
		BoundSql boundSql = ms.getBoundSql(parameter);
		String sql = boundSql.getSql().trim();
		if (dialect.supportsLimitOffset()) {
			sql = dialect.getLimitString(sql, offset, limit);
			offset = RowBounds.NO_ROW_OFFSET;
		} else {
			sql = dialect.getLimitString(sql, 0, limit);
		}
		limit = RowBounds.NO_ROW_LIMIT;
		queryArgs[ROWBOUNDS_INDEX] = new RowBounds(offset, limit);
		BoundSql newBoundSql = new BoundSql(ms.getConfiguration(), sql, boundSql.getParameterMappings(),
				boundSql.getParameterObject());
		for (ParameterMapping mapping : boundSql.getParameterMappings()) {
			String prop = mapping.getProperty();
			if (boundSql.hasAdditionalParameter(prop)) {
				newBoundSql.setAdditionalParameter(prop, boundSql.getAdditionalParameter(prop));
			}
		}
		MappedStatement newMs = copyFromMappedStatement(ms, new BoundSqlSqlSource(newBoundSql));
		queryArgs[MAPPED_STATEMENT_INDEX] = newMs;
	}
}
 
开发者ID:yi-jun,项目名称:aaden-pay,代码行数:30,代码来源:PagePluging.java

示例3: getNamespaceSql

import org.apache.ibatis.mapping.MappedStatement; //导入方法依赖的package包/类
/**
 * 通过命名空间方式获取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;
}
 
开发者ID:itfsw,项目名称:mybatis-generator-plugin,代码行数:39,代码来源:SqlHelper.java

示例4: intercept

import org.apache.ibatis.mapping.MappedStatement; //导入方法依赖的package包/类
public Object intercept(Invocation invocation) throws Throwable {
    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();
    Object returnValue = null;
    long start = System.currentTimeMillis();
    String sql = getSql(configuration, boundSql, sqlId);
    System.err.println("========================================================");
    System.err.println("https://hpit-bat.github.io/hpit-BAT-home"+"[隐无为]");
    System.err.println("========================================================");
    System.err.println("执行XML方法:"+sqlId);
    System.err.println("执行的完整的sql语句-------------------mysad");
    System.err.println(sql);
   
    returnValue = invocation.proceed();
    long end = System.currentTimeMillis();
    long time = (end - start);
    if (time > 1) {
        System.err.println("执行的sql语句的时间:"+time+"ms");
    }
    return returnValue;
}
 
开发者ID:hpit-BAT,项目名称:mybatis-tool-box,代码行数:28,代码来源:MybatisAutoSql.java

示例5: intercept

import org.apache.ibatis.mapping.MappedStatement; //导入方法依赖的package包/类
@Override
public Object intercept(Invocation invocation) throws Throwable {
	final MappedStatement mappedStatement = (MappedStatement) invocation
			.getArgs()[0];
	// 拦截需要分页的SQL
	if (mappedStatement.getId().matches(SQL_PATTERN)) {
		Object parameter = invocation.getArgs()[1];
		BoundSql boundSql = mappedStatement.getBoundSql(parameter);
		String originalSql = boundSql.getSql().trim();
		if (null == boundSql.getSql() || "".equals(boundSql.getSql()))
			return null;
		// 分页参数--上下文传参
		Page page = null;
		PageContext context = PageContext.getPageContext();
		// map传参每次都将currentPage重置,先判读map再判断context
		if (null != parameter && null == context) {
			page = convertParameter(parameter, page);
		}
		// 分页参数--context参数里的Page传参
		if (null == page) {
			page = context;
		}

		if (null != page) {
			int totalPage = page.getTotal();
			// 得到总记录数
			if (totalPage == 0) {
				Connection connection = mappedStatement.getConfiguration()
						.getEnvironment().getDataSource().getConnection();
				totalPage = BaseParameter.getCount(originalSql, connection,
						mappedStatement, parameter, boundSql, DIALECT);
			}
			// 分页计算
			page.init(totalPage, page.getSize(), page.getLimit());
			invocation.getArgs()[2] = new RowBounds(
					RowBounds.NO_ROW_OFFSET, RowBounds.NO_ROW_LIMIT);
			BoundSql newBoundSql = new BoundSql(
					mappedStatement.getConfiguration(),
					BaseParameter.generatePageSql(originalSql, page,
							DIALECT), boundSql.getParameterMappings(),
					boundSql.getParameterObject());
			MappedStatement newMs = copyFromMappedStatement(
					mappedStatement, new BoundSqlSqlSource(newBoundSql));
			invocation.getArgs()[0] = newMs;
		}
	}
	PageContext.clear();
	return invocation.proceed();
}
 
开发者ID:jiangzongyao,项目名称:kettle_support_kettle8.0,代码行数:50,代码来源:PaginationInterceptor.java

示例6: intercept

import org.apache.ibatis.mapping.MappedStatement; //导入方法依赖的package包/类
public Object intercept(final Invocation invocation) throws Throwable {
       final Executor executor = (Executor) invocation.getTarget();
       final Object[] queryArgs = invocation.getArgs();
       final MappedStatement ms = (MappedStatement)queryArgs[MAPPED_STATEMENT_INDEX];
       final Object parameter = queryArgs[PARAMETER_INDEX];
       final RowBounds rowBounds = (RowBounds)queryArgs[ROWBOUNDS_INDEX];
       final PageBounds pageBounds = new PageBounds(rowBounds);

       if(pageBounds.getOffset() == RowBounds.NO_ROW_OFFSET
               && pageBounds.getLimit() == RowBounds.NO_ROW_LIMIT
               && pageBounds.getOrders().isEmpty()){
           return invocation.proceed();
       }

       final Dialect dialect;
       try {
           Class clazz = Class.forName(dialectClass);
           Constructor constructor = clazz.getConstructor(MappedStatement.class, Object.class, PageBounds.class);
           dialect = (Dialect)constructor.newInstance(new Object[]{ms, parameter, pageBounds});
       } catch (Exception e) {
           throw new ClassNotFoundException("Cannot create dialect instance: "+dialectClass,e);
       }

       final BoundSql boundSql = ms.getBoundSql(parameter);

       queryArgs[MAPPED_STATEMENT_INDEX] = copyFromNewSql(ms,boundSql,dialect.getPageSQL(), dialect.getParameterMappings(), dialect.getParameterObject());
       queryArgs[PARAMETER_INDEX] = dialect.getParameterObject();
       queryArgs[ROWBOUNDS_INDEX] = new RowBounds(RowBounds.NO_ROW_OFFSET,RowBounds.NO_ROW_LIMIT);

       Boolean async = pageBounds.getAsyncTotalCount() == null ? asyncTotalCount : pageBounds.getAsyncTotalCount();
       Future<List> listFuture = call(new Callable<List>() {
           public List call() throws Exception {
               return (List)invocation.proceed();
           }
       }, async);


       if(pageBounds.isContainsTotalCount()){
           Callable<Paginator> countTask = new Callable() {
               public Object call() throws Exception {
                   Integer count;
                   Cache cache = ms.getCache();
                   if(cache != null && ms.isUseCache() && ms.getConfiguration().isCacheEnabled()){
                       CacheKey cacheKey = executor.createCacheKey(ms,parameter,new PageBounds(),copyFromBoundSql(ms,boundSql,dialect.getCountSQL(), boundSql.getParameterMappings(), boundSql.getParameterObject()));
                       count = (Integer)cache.getObject(cacheKey);
                       if(count == null){
                           count = SQLHelp.getCount(ms,executor.getTransaction(),parameter,boundSql,dialect);
                           cache.putObject(cacheKey, count);
                       }
                   }else{
                       count = SQLHelp.getCount(ms,executor.getTransaction(),parameter,boundSql,dialect);
                   }
                   return new Paginator(pageBounds.getPage(), pageBounds.getLimit(), count);
               }
           };
           Future<Paginator> countFutrue = call(countTask, async);
           return new PageList(listFuture.get(),countFutrue.get());
       }

       return listFuture.get();
}
 
开发者ID:AsuraTeam,项目名称:asura,代码行数:62,代码来源:OffsetLimitInterceptor.java

示例7: onInterceptor

import org.apache.ibatis.mapping.MappedStatement; //导入方法依赖的package包/类
@Override
public Object onInterceptor(Invocation invocation) throws Throwable {

    Object[] objects = invocation.getArgs();
    MappedStatement ms = (MappedStatement) objects[0];
    Object parameterObject = objects[1];

    // TypeHandlerRegistry typeHandlerRegistry =
    // ms.getConfiguration().getTypeHandlerRegistry();

    if (ignoreMappedStatementIds.contains(ms.getId())) {
        return null;
    }
    String namespace = ms.getId().substring(0, ms.getId().lastIndexOf(SPIT_POINT));
    //策略配置忽略
    if (ignoreTablesMapperNameSpace.contains(namespace)) {
        return null;
    }

    BoundSql boundSql = ms.getBoundSql(parameterObject);

    Object parameterObject2 = boundSql.getParameterObject();
    System.out.println(parameterObject2);

    //是否需要分库
    boolean requiredShard = isRequiredShard(boundSql.getSql(), ms.getSqlCommandType(),
            namespace);

    if (requiredShard) {
        //先检查是否已经设置
        Object shardFieldValue = getShardFieldValue(ms.getId(), parameterObject);
        if (shardFieldValue == null) {
            logger.error("方法{}无法获取分库字段{}的值", ms.getId(), shardStrategy.shardEntityField());
        } else {
            int dbIndex = shardStrategy.assigned(shardFieldValue);
            //指定数据库分库序列
            DataSourceContextHolder.get().setDbIndex(dbIndex);
        }
    }
    return null;
}
 
开发者ID:warlock-china,项目名称:azeroth,代码行数:42,代码来源:DatabaseRouteHandler.java

示例8: removeCacheByUpdateConditon

import org.apache.ibatis.mapping.MappedStatement; //导入方法依赖的package包/类
/**
 * 按更新的查询条件更新缓存
 * @param executor
 * @param mt
 * @param mapperNameSpace
 * @param args
 */
private void removeCacheByUpdateConditon(Executor executor, MappedStatement mt,
                                         String mapperNameSpace, Object[] args) {
    try {
        Object parameterObject = args[1];
        EntityInfo entityInfo = MybatisMapperParser.getEntityInfoByMapper(mapperNameSpace);
        MappedStatement statement = getQueryIdsMappedStatementForUpdateCache(mt, entityInfo);
        if (statement == null) { return; }

        String querySql = statement.getSqlSource().getBoundSql(parameterObject).getSql();

        List<?> idsResult = null;
        if (PARSE_SQL_ERROR_DEFAULT.equals(querySql)) {
            BoundSql boundSql = mt.getBoundSql(parameterObject);
            querySql = "select " + entityInfo.getIdColumn() + " from "
                    + entityInfo.getTableName() + " WHERE "
                    + boundSql.getSql().split(WHERE_REGEX)[1];
            BoundSql queryBoundSql = new BoundSql(statement.getConfiguration(), querySql,
                    boundSql.getParameterMappings(), parameterObject);

            idsResult = executor.query(statement, parameterObject, RowBounds.DEFAULT,
                    new DefaultResultHandler(), null, queryBoundSql);
        } else {
            idsResult = executor.query(statement, parameterObject, RowBounds.DEFAULT, null);
        }

        if (idsResult != null && !idsResult.isEmpty()) {
            for (Object id : idsResult) {
                String cacheKey = entityInfo.getEntityClass().getSimpleName() + ID_CACHEKEY_JOIN
                        + id.toString();
                getCacheProvider().remove(cacheKey);
            }
            if (logger.isDebugEnabled()) {
                logger.debug(
                        "_autocache_ update Method[{}] executed,remove ralate cache {}.id:[{}]",
                        mt.getId(), entityInfo.getEntityClass().getSimpleName(), idsResult);
            }
        }
    } catch (Exception e) {
        logger.error("_autocache_ update Method[{}] remove ralate cache error", e);
    }
}
 
开发者ID:warlock-china,项目名称:azeroth,代码行数:49,代码来源:CacheHandler.java

示例9: processIntercept

import org.apache.ibatis.mapping.MappedStatement; //导入方法依赖的package包/类
void processIntercept(final Object[] queryArgs) throws Exception
{
	/*
	// 以下代码可根据数据源动态加载方言,用于多数据源的情况下
	Dialect dialect = this.dialect;
	String str = DynamicDataSource.getDbType();
	if(!str.equals("1"))
	{
		String dialectClass = this.properties.getProperty(str);
		try
		{
			System.out.println("dbType是" + str + ",加载动态方言:" + dialectClass);
			dialect = (Dialect) Class.forName(dialectClass).newInstance();
		}
		catch (Exception e)
		{
			throw new RuntimeException("无法初始化方言:" + dialectClass, e);
		}
	}
	*/
	final RowBounds rowBounds = (RowBounds) queryArgs[ROWBOUNDS_INDEX];
	int offset = rowBounds.getOffset();
	int limit = rowBounds.getLimit();
	if (dialect.supportsLimitOffset() && (offset != RowBounds.NO_ROW_OFFSET || limit != RowBounds.NO_ROW_LIMIT))
	{
		MappedStatement ms = (MappedStatement) queryArgs[MAPPED_STATEMENT_INDEX];
		Object parameter = queryArgs[PARAMETER_INDEX];
		BoundSql boundSql = ms.getBoundSql(parameter);
		String sql = boundSql.getSql().trim();
		
		sql = dialect.getLimitString(sql, offset, limit);
		offset = RowBounds.NO_ROW_OFFSET;
		
		limit = RowBounds.NO_ROW_LIMIT;
		queryArgs[ROWBOUNDS_INDEX] = new RowBounds(offset, limit);
		BoundSql newBoundSql = new BoundSql(ms.getConfiguration(), sql, boundSql.getParameterMappings(), boundSql.getParameterObject());
		String prop;
		for(ParameterMapping p : boundSql.getParameterMappings())
		{
			prop = p.getProperty();
			if(boundSql.hasAdditionalParameter(prop))
			{
				newBoundSql.setAdditionalParameter(prop, boundSql.getAdditionalParameter(prop));
			}
		}
		MappedStatement newMs = copyFromMappedStatement(ms, new BoundSqlSqlSource(newBoundSql));
		queryArgs[MAPPED_STATEMENT_INDEX] = newMs;
	}
}
 
开发者ID:skeychen,项目名称:dswork,代码行数:50,代码来源:OffsetLimitInterceptor.java

示例10: setPageParameter

import org.apache.ibatis.mapping.MappedStatement; //导入方法依赖的package包/类
/**
 * 设置分页参数
 *
 * @param parameterObject
 * @param page
 * @return
 */
public Map setPageParameter(MappedStatement ms, Object parameterObject, Page page) {
    BoundSql boundSql = ms.getBoundSql(parameterObject);
    return sqlParser.setPageParameter(ms, parameterObject, boundSql, page);
}
 
开发者ID:PekingGo,项目名称:ipayquery,代码行数:12,代码来源:SqlUtil.java


注:本文中的org.apache.ibatis.mapping.MappedStatement.getBoundSql方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。