Example usage for org.apache.ibatis.session Configuration getMappedStatement

List of usage examples for org.apache.ibatis.session Configuration getMappedStatement

Introduction

In this page you can find the example usage for org.apache.ibatis.session Configuration getMappedStatement.

Prototype

public MappedStatement getMappedStatement(String id) 

Source Link

Usage

From source file:com.bluesky.iplatform.commons.db.mybatis.SqlHelper.java

License:Open Source License

/**
 * ????sql/*from w  w  w  .ja  va 2  s . c  o  m*/
 *
 * @param session
 * @param namespace
 * @param params
 * @return
 */
public static String getNamespaceSql(SqlSession session, String namespace, Object params) {
    params = wrapCollection(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;
}

From source file:com.itfsw.mybatis.generator.plugins.tools.SqlHelper.java

License:Apache License

/**
 * ????sql//from   w  ww. j  a  v  a  2  s  .  c o m
 * @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;
}

From source file:com.xiaogua.web.util.MybatisSqlHelper.java

License:Open Source License

/**
        // w w w .  jav  a 2  s .c o  m
 * ????sql
        
 *
        
 * @param session
        
 * @param namespace
        
 * @param params
        
 * @return
        
 */
public static String getNameSpaceSql(SqlSession session, String namespace, Object params) {
    params = wrapCollection(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;
}

From source file:com.yimidida.shards.session.impl.ShardedSqlSessionImpl.java

License:Open Source License

@Override
public int insert(String statement, Object parameter) {
    ShardId shardId = this.selectShardIdForNewObject(statement, parameter);
    if (shardId == null) {
        shardId = this.getShardIdForStatementOrParameter(statement, parameter);
    }//w  w w  .j a  v  a 2 s  . c  o  m

    Assert.notNull(shardId);

    // ?id
    setCurrentSubgraphShardId(shardId);

    log.debug(String.format("Inserting object of type %s to shard %s", parameter.getClass(), shardId));

    SqlSession session = shardIdsToShards.get(shardId).establishSqlSession();

    IdGenerator idGenerator = shardedSqlSessionFactory.getIdGenerator();
    if (idGenerator != null) {
        //TODO(fengkuok) ? DB?session
        Serializable id = idGenerator.generate(session, parameter);

        log.debug(String.format(
                "Generating id for object %s ,the type of IdGenerator is %s and generated Id is %s.",
                parameter.getClass(), idGenerator.getClass(), id));

        ParameterUtil.generatePrimaryKey(parameter, id);
    }

    final Object params = ParameterUtil.resolve(parameter, shardId);

    final int rows = session.insert(statement, params);

    //fixed set keys
    if (params instanceof Map) {
        Map map = (Map) params;
        Configuration configuration = session.getConfiguration();
        MappedStatement ms = configuration.getMappedStatement(statement);

        if (parameter != null && ms != null && ms.getKeyProperties() != null) {
            String keyProperty = ms.getKeyProperties()[0]; // just one key property is supported
            final MetaObject metaParam = configuration.newMetaObject(parameter);
            if (keyProperty != null && metaParam.hasSetter(keyProperty)) {
                metaParam.setValue(keyProperty, map.get(keyProperty));
            }
        }
    }

    return rows;
}

From source file:org.alfresco.ibatis.HierarchicalSqlSessionFactoryBeanTest.java

License:Open Source License

public void testHierarchyTreeSet() throws Exception {
    Configuration mybatisConfig = getConfiguration(TreeSet.class);
    MappedStatement stmt = mybatisConfig.getMappedStatement(QUERY_TREESET);
    assertNotNull("Query missing for " + QUERY_TREESET + " using " + TreeSet.class, stmt);
    try {//  www. j av  a  2 s  . co  m
        mybatisConfig.getMappedStatement(QUERY_ABSTRACTCOLLECTION);
        fail("Query not missing for " + QUERY_ABSTRACTCOLLECTION + " using " + TreeSet.class);
    } catch (IllegalArgumentException e) {
        // Expected
    }
}

From source file:org.alfresco.ibatis.HierarchicalSqlSessionFactoryBeanTest.java

License:Open Source License

public void testHierarchyHashSet() throws Exception {
    Configuration mybatisConfig = getConfiguration(HashSet.class);
    MappedStatement stmt = mybatisConfig.getMappedStatement(QUERY_ABSTRACTCOLLECTION);
    assertNotNull("Query missing for " + QUERY_ABSTRACTCOLLECTION + " using " + HashSet.class, stmt);
    try {/*ww w .ja va  2s  . co  m*/
        mybatisConfig.getMappedStatement(QUERY_OBJECT);
        fail("Query not missing for " + QUERY_OBJECT + " using " + HashSet.class);
    } catch (IllegalArgumentException e) {
        // Expected
    }
}

From source file:org.alfresco.ibatis.HierarchicalSqlSessionFactoryBeanTest.java

License:Open Source License

public void testHierarchyArrayList() throws Exception {
    Configuration mybatisConfig = getConfiguration(ArrayList.class);
    MappedStatement stmt = mybatisConfig.getMappedStatement(QUERY_ABSTRACTLIST);
    assertNotNull("Query missing for " + QUERY_ABSTRACTLIST + " using " + ArrayList.class, stmt);
    try {//from w  ww.j  a  va 2s . c o m
        mybatisConfig.getMappedStatement(QUERY_ABSTRACTCOLLECTION);
        fail("Query not missing for " + QUERY_ABSTRACTCOLLECTION + " using " + ArrayList.class);
    } catch (IllegalArgumentException e) {
        // Expected
    }
}

From source file:org.alfresco.ibatis.HierarchicalSqlSessionFactoryBeanTest.java

License:Open Source License

public void testHierarchyAbstractCollection() throws Exception {
    Configuration mybatisConfig = getConfiguration(AbstractCollection.class);
    MappedStatement stmt = mybatisConfig.getMappedStatement(QUERY_ABSTRACTCOLLECTION);
    assertNotNull("Query missing for " + QUERY_ABSTRACTCOLLECTION + " using " + AbstractCollection.class, stmt);
    try {/*from   w w w . j a  v  a 2  s. c  o m*/
        mybatisConfig.getMappedStatement(QUERY_OBJECT);
        fail("Query not missing for " + QUERY_OBJECT + " using " + AbstractCollection.class);
    } catch (IllegalArgumentException e) {
        // Expected
    }
}

From source file:org.sonar.core.persistence.BatchSession.java

License:Open Source License

private void makeSureGeneratedKeysAreNotUsedInBatchInserts(String statement) {
    Configuration configuration = super.getConfiguration();
    if (null != configuration) {
        MappedStatement mappedStatement = configuration.getMappedStatement(statement);
        if (null != mappedStatement) {
            KeyGenerator keyGenerator = mappedStatement.getKeyGenerator();
            if (keyGenerator instanceof Jdbc3KeyGenerator) {
                throw new IllegalStateException("Batch inserts cannot use generated keys");
            }//from  ww  w. jav  a 2s.co m
        }
    }
}