/** * 生成泛型对应的ResultMap * * @param namespace * @param clazz * @param conf * @return */ private List<ResultMap> getResultMap(String namespace, Class<?> clazz, Configuration conf) { String dynamicResultMapId = namespace + "." + GENERATE_RESULT_MAP_NAME; ResultMap dynamicResultMap = buildResultMap(dynamicResultMapId, clazz, conf); List<ResultMap> resultMaps = Lists.newArrayList(); resultMaps.add(dynamicResultMap); return Collections.unmodifiableList(resultMaps); }
/** * 构建ResultMap对象 * * @param id * @param clazz * @param configuration * @return */ private ResultMap buildResultMap(String id, Class<?> clazz, Configuration configuration) { // 判断是否已经存在缓存里 if (configuration.hasResultMap(id)) { return configuration.getResultMap(id); } List<ResultMapping> resultMappings = Lists.newArrayList(); Map<String, Field> columns = EntityUtil.getFields(clazz); for (Map.Entry<String, Field> column : columns.entrySet()) { Field field = column.getValue(); String fieldName = field.getName(); Class<?> columnTypeClass = resolveResultJavaType(clazz, fieldName, null); List<ResultFlag> flags = Lists.newArrayList(); if (field.isAnnotationPresent(Id.class)) { flags.add(ResultFlag.ID); } String columnName = column.getKey(); resultMappings.add( buildResultMapping(configuration, fieldName, columnName, columnTypeClass, flags)); } // 构建ResultMap ResultMap.Builder resultMapBuilder = new ResultMap.Builder(configuration, id, clazz, resultMappings); ResultMap rm = resultMapBuilder.build(); // 放到缓存中 configuration.addResultMap(rm); return rm; }
private SqlSource getSqlSource( MappedStatement ms, SqlSource sqlSource, Object parameterObject, boolean isCount) { if (ms.getSqlSource() instanceof DynamicSqlSource) { MetaObject msObject = MetaObjectUtils.forObject(ms); SqlNode sqlNode = (SqlNode) msObject.getValue("sqlSource.rootSqlNode"); MixedSqlNode mixedSqlNode = null; if (sqlNode instanceof MixedSqlNode) { mixedSqlNode = (MixedSqlNode) sqlNode; } else { List<SqlNode> contents = new ArrayList<SqlNode>(1); contents.add(sqlNode); mixedSqlNode = new MixedSqlNode(contents); } return new CustomerDynamicSqlSource(ms.getConfiguration(), mixedSqlNode, isCount, dialect); } else if (sqlSource instanceof ProviderSqlSource) { return new CustomerProviderSqlSource( ms.getConfiguration(), (ProviderSqlSource) sqlSource, isCount, dialect); } else if (!isCount) { BoundSql boundSql = sqlSource.getBoundSql(parameterObject); return new StaticSqlSource( ms.getConfiguration(), dialect.generatePageSQL(boundSql.getSql()), MybatisUtils.plusTwoParameterToMapping(ms.getConfiguration(), boundSql)); } else { BoundSql boundSql = sqlSource.getBoundSql(parameterObject); return new StaticSqlSource( ms.getConfiguration(), dialect.generateCountSQL(boundSql.getSql()), boundSql.getParameterMappings()); } }