Example usage for org.apache.ibatis.reflection MetaObject setValue

List of usage examples for org.apache.ibatis.reflection MetaObject setValue

Introduction

In this page you can find the example usage for org.apache.ibatis.reflection MetaObject setValue.

Prototype

public void setValue(String name, Object value) 

Source Link

Usage

From source file:SqlUtil.java

License:Open Source License

/**
 * ?sqlSource/*from   w  w  w. ja  v a  2s. c o  m*/
 *
 * @param configuration
 * @param newSqlSource
 * @return
 */
private BoundSqlSqlSource getPageSqlSource(Configuration configuration, BoundSqlSqlSource newSqlSource) {
    String sql = newSqlSource.getBoundSql().getSql();
    //sql
    MetaObject sqlObject = forObject(newSqlSource);
    sqlObject.setValue("boundSql.sql", sqlParser.getPageSql(sql));
    //?
    List<ParameterMapping> newParameterMappings = new ArrayList<ParameterMapping>();
    newParameterMappings.addAll(newSqlSource.getBoundSql().getParameterMappings());
    newParameterMappings
            .add(new ParameterMapping.Builder(configuration, PAGEPARAMETER_FIRST, Integer.class).build());
    newParameterMappings
            .add(new ParameterMapping.Builder(configuration, PAGEPARAMETER_SECOND, Integer.class).build());
    sqlObject.setValue("boundSql.parameterMappings", newParameterMappings);
    return newSqlSource;
}

From source file:SqlUtil.java

License:Open Source License

/**
 * ?countsqlSource/*from  w ww .j  av a  2  s  .  c o  m*/
 *
 * @param newSqlSource
 * @return
 */
private BoundSqlSqlSource getCountSqlSource(BoundSqlSqlSource newSqlSource) {
    String sql = newSqlSource.getBoundSql().getSql();
    MetaObject sqlObject = forObject(newSqlSource);
    sqlObject.setValue("boundSql.sql", sqlParser.getCountSql(sql));
    return newSqlSource;
}

From source file:cn.com.bricks.mybatis.rbac.DynamicRbacInterceptor.java

@Override
public Object intercept(Invocation invocation) throws Throwable {
    StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
    MetaObject metaStatementHandler = MetaObject.forObject(statementHandler, DEFAULT_OBJECT_FACTORY,
            DEFAULT_OBJECT_WRAPPER_FACTORY, DEFAULT_REFLECTOR_FACTORY);
    MappedStatement mappedStatement = (MappedStatement) metaStatementHandler
            .getValue("delegate.mappedStatement");

    // ?sql/* ww w. j av a 2s .  com*/
    BoundSql bsql = statementHandler.getBoundSql();
    // ?sql
    TextSqlNode sqlNode = new TextSqlNode(bsql.getSql());
    BoundSql nbsql = getBoundSql(mappedStatement.getConfiguration(), bsql.getParameterObject(), sqlNode);
    // ?sql?
    metaStatementHandler.setValue("delegate.boundSql.sql", nbsql.getSql());
    return invocation.proceed();
}

From source file:cn.myblog.uitl.MybatisPageHelper.PageHelper.java

License:Open Source License

@Override
public Object intercept(Invocation invocation) throws Throwable {
    final Object[] args = invocation.getArgs();
    RowBounds rowBounds = (RowBounds) args[2];
    if (localPage.get() == null && rowBounds == RowBounds.DEFAULT) {
        return invocation.proceed();
    } else {/*  w  w  w.ja v a  2 s . co m*/
        //RowBounds-?Mybatis
        args[2] = RowBounds.DEFAULT;
        MappedStatement ms = (MappedStatement) args[0];
        Object parameterObject = args[1];
        BoundSql boundSql = ms.getBoundSql(parameterObject);

        //?
        Page page = localPage.get();
        //??
        localPage.remove();

        if (page == null) {
            if (offsetAsPageNum) {
                page = new Page(rowBounds.getOffset(), rowBounds.getLimit(),
                        rowBoundsWithCount ? Page.SQL_COUNT : Page.NO_SQL_COUNT);
            } else {
                page = new Page(rowBounds, rowBoundsWithCount ? Page.SQL_COUNT : Page.NO_SQL_COUNT);
            }
        }
        MappedStatement qs = newMappedStatement(ms, new BoundSqlSqlSource(boundSql));
        //?MappedStatement?qs?
        args[0] = qs;
        MetaObject msObject = SystemMetaObject.forObject(qs);
        String sql = (String) msObject.getValue("sqlSource.boundSql.sql");
        //?total??count
        if (page.getTotal() > Page.NO_SQL_COUNT) {
            //count - ?sql
            msObject.setValue("sqlSource.boundSql.sql", getCountSql(sql));
            //
            Object result = invocation.proceed();
            int totalCount = (Integer) ((List) result).get(0);
            page.setTotal(totalCount);
            int totalPage = totalCount / page.getPageSize() + ((totalCount % page.getPageSize() == 0) ? 0 : 1);
            page.setPages(totalPage);
            //sql - ?sql
            msObject.setValue("sqlSource.boundSql.sql", getPageSql(sql, page));
            //??
            msObject.setValue("resultMaps", ms.getResultMaps());
            //
            result = invocation.proceed();
            //?
            page.addAll((List) result);
            //
            return page;
        } else {
            //sql - ?sql
            msObject.setValue("sqlSource.boundSql.sql", getPageSql(sql, page));
            //??
            msObject.setValue("resultMaps", ms.getResultMaps());
            //
            Object result = invocation.proceed();
            //?
            page.addAll((List) result);
            //
            return page;
        }
    }
}

From source file:cn.org.awcp.core.mybatis.mapper.MapperTemplate.java

License:Open Source License

/**
 * /*from www  .  j  a  v a  2 s.  c om*/
 * 
 * @param ms
 * @param entityClass
 */
protected void setResultType(MappedStatement ms, Class<?> entityClass) {
    ResultMap resultMap = ms.getResultMaps().get(0);
    MetaObject metaObject = forObject(resultMap);
    metaObject.setValue("type", entityClass);
}

From source file:cn.org.awcp.core.mybatis.mapper.MapperTemplate.java

License:Open Source License

/**
 * ?SqlSource/*from   w w w .  ja va 2  s.c om*/
 * 
 * @param ms
 * @param sqlSource
 */
protected void setSqlSource(MappedStatement ms, SqlSource sqlSource) {
    MetaObject msObject = forObject(ms);
    msObject.setValue("sqlSource", sqlSource);
}

From source file:cn.org.awcp.core.mybatis.mapper.MapperTemplate.java

License:Open Source License

/**
 * SelectKey - ?mysqlOracle?/* w  ww.  j  a v a 2s .  c om*/
 * 
 * @param ms
 * @param column
 */
protected void newSelectKeyMappedStatement(MappedStatement ms, EntityHelper.EntityColumn column) {
    String keyId = ms.getId() + SelectKeyGenerator.SELECT_KEY_SUFFIX;
    if (ms.getConfiguration().hasKeyGenerator(keyId)) {
        return;
    }
    Class<?> entityClass = getSelectReturnType(ms);
    // defaults
    Configuration configuration = ms.getConfiguration();
    KeyGenerator keyGenerator = null;
    Boolean executeBefore = getBEFORE();
    String IDENTITY = (column.getGenerator() == null || column.getGenerator().equals("")) ? getIDENTITY()
            : column.getGenerator();
    if (IDENTITY.equalsIgnoreCase("JDBC")) {
        keyGenerator = new Jdbc3KeyGenerator();
    } else {
        SqlSource sqlSource = new RawSqlSource(configuration, IDENTITY, entityClass);

        MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, keyId, sqlSource,
                SqlCommandType.SELECT);
        statementBuilder.resource(ms.getResource());
        statementBuilder.fetchSize(null);
        statementBuilder.statementType(StatementType.STATEMENT);
        statementBuilder.keyGenerator(new NoKeyGenerator());
        statementBuilder.keyProperty(column.getProperty());
        statementBuilder.keyColumn(null);
        statementBuilder.databaseId(null);
        statementBuilder.lang(configuration.getDefaultScriptingLanuageInstance());
        statementBuilder.resultOrdered(false);
        statementBuilder.resulSets(null);
        statementBuilder.timeout(configuration.getDefaultStatementTimeout());

        List<ParameterMapping> parameterMappings = new ArrayList<ParameterMapping>();
        ParameterMap.Builder inlineParameterMapBuilder = new ParameterMap.Builder(configuration,
                statementBuilder.id() + "-Inline", entityClass, parameterMappings);
        statementBuilder.parameterMap(inlineParameterMapBuilder.build());

        List<ResultMap> resultMaps = new ArrayList<ResultMap>();
        ResultMap.Builder inlineResultMapBuilder = new ResultMap.Builder(configuration,
                statementBuilder.id() + "-Inline", column.getJavaType(), new ArrayList<ResultMapping>(), null);
        resultMaps.add(inlineResultMapBuilder.build());
        statementBuilder.resultMaps(resultMaps);
        statementBuilder.resultSetType(null);

        statementBuilder.flushCacheRequired(false);
        statementBuilder.useCache(false);
        statementBuilder.cache(null);

        MappedStatement statement = statementBuilder.build();
        configuration.addMappedStatement(statement);

        MappedStatement keyStatement = configuration.getMappedStatement(keyId, false);
        keyGenerator = new SelectKeyGenerator(keyStatement, executeBefore);
        configuration.addKeyGenerator(keyId, keyGenerator);
    }
    // keyGenerator
    try {
        MetaObject msObject = forObject(ms);
        msObject.setValue("keyGenerator", keyGenerator);
        msObject.setValue("keyProperties", new String[] { column.getProperty() });
    } catch (Exception e) {
        // ignore
    }
}

From source file:cn.org.awcp.core.utils.SqlUtil.java

License:Open Source License

/**
 * ?sqlSource/*from   www  .  j  a v  a  2s . c  om*/
 *
 * @param configuration
 * @param newSqlSource
 * @return
 */
private BoundSqlSqlSource getPageSqlSource(Configuration configuration, BoundSqlSqlSource newSqlSource) {
    String sql = newSqlSource.getBoundSql().getSql();
    // sql
    MetaObject sqlObject = forObject(newSqlSource);
    sqlObject.setValue("boundSql.sql", sqlParser.getPageSql(sql));
    // ?
    List<ParameterMapping> newParameterMappings = new ArrayList<ParameterMapping>();
    newParameterMappings.addAll(newSqlSource.getBoundSql().getParameterMappings());
    newParameterMappings
            .add(new ParameterMapping.Builder(configuration, PAGEPARAMETER_FIRST, Integer.class).build());
    newParameterMappings
            .add(new ParameterMapping.Builder(configuration, PAGEPARAMETER_SECOND, Integer.class).build());
    sqlObject.setValue("boundSql.parameterMappings", newParameterMappings);
    return newSqlSource;
}

From source file:com.appleframework.orm.mybatis.pagehelper.SqlUtil.java

License:Open Source License

/**
 * SqlSource/*from ww  w. j  av  a 2s .  c o m*/
 *
 * @param ms
 * @throws Throwable
 */
public void processMappedStatement(MappedStatement ms) throws Throwable {
    SqlSource sqlSource = ms.getSqlSource();
    MetaObject msObject = SystemMetaObject.forObject(ms);
    SqlSource pageSqlSource;
    if (sqlSource instanceof StaticSqlSource) {
        pageSqlSource = new PageStaticSqlSource((StaticSqlSource) sqlSource);
    } else if (sqlSource instanceof RawSqlSource) {
        pageSqlSource = new PageRawSqlSource((RawSqlSource) sqlSource);
    } else if (sqlSource instanceof ProviderSqlSource) {
        pageSqlSource = new PageProviderSqlSource((ProviderSqlSource) sqlSource);
    } else if (sqlSource instanceof DynamicSqlSource) {
        pageSqlSource = new PageDynamicSqlSource((DynamicSqlSource) sqlSource);
    } else {
        throw new RuntimeException("?[" + sqlSource.getClass() + "]SqlSource");
    }
    msObject.setValue("sqlSource", pageSqlSource);
    //count??CountMS
    msCountMap.put(ms.getId(), MSUtils.newCountMappedStatement(ms));
}

From source file:com.baomidou.mybatisplus.MybatisDefaultParameterHandler.java

License:Apache License

/**
 * <p>/*from  w ww  .ja va  2s  . co m*/
 * 
 * </p>
 *
 * @param metaObjectHandler ??
 * @param tableInfo         ????
 * @param ms                MappedStatement
 * @param parameterObject   ??
 * @return Object
 */
protected static Object populateKeys(MetaObjectHandler metaObjectHandler, TableInfo tableInfo,
        MappedStatement ms, Object parameterObject) {
    if (null == tableInfo || StringUtils.isEmpty(tableInfo.getKeyProperty()) || null == tableInfo.getIdType()) {
        /* ?? */
        return parameterObject;
    }
    /*  */
    MetaObject metaObject = ms.getConfiguration().newMetaObject(parameterObject);
    if (ms.getSqlCommandType() == SqlCommandType.INSERT) {
        if (tableInfo.getIdType().getKey() >= 2) {
            Object idValue = metaObject.getValue(tableInfo.getKeyProperty());
            /*  ID */
            if (StringUtils.checkValNull(idValue)) {
                if (tableInfo.getIdType() == IdType.ID_WORKER) {
                    metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.getId());
                } else if (tableInfo.getIdType() == IdType.UUID) {
                    metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.get32UUID());
                }
            }
        }
        // ?
        if (metaObjectHandler.openInsertFill()) {
            metaObjectHandler.insertFill(metaObject);
        }
    } else if (ms.getSqlCommandType() == SqlCommandType.UPDATE && metaObjectHandler.openUpdateFill()) {
        // 
        metaObjectHandler.updateFill(metaObject);
    }
    return metaObject.getOriginalObject();
}