List of usage examples for org.apache.ibatis.session SqlSession getConfiguration
Configuration getConfiguration();
From source file:com.xiaogua.web.util.MybatisSqlHelper.java
License:Open Source License
/** //ww w . j a v a2 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); }/*from w w w. jav a 2 s. co 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; }