/** * 构建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; }
/** * 从传递的参数中找Page对象,并返回 * * @param paramObj * @return */ public Page<?> findPageParameter(Object paramObj) { Page<?> page = null; if (paramObj instanceof Page) { page = (Page<?>) paramObj; } else if (paramObj instanceof Map) { Map<?, ?> m = (Map<?, ?>) paramObj; for (Object o : m.values()) { if (o instanceof Page) { page = (Page<?>) o; break; } } } if (page != null) { PAGE_THREAD_LOCAL.set(page); } return page; }
/** * 根据namespace查找需要的信息,避免每次反射的耗时操作 * * @param conf * @param statementId * @return * @throws ClassNotFoundException */ private MapperMeta getMapperMeta(Configuration conf, final String statementId) throws ClassNotFoundException { MapperMeta meta = CACHED.get(statementId); if (meta == null) { int pos = statementId.lastIndexOf('.'); String namespace = statementId.substring(0, pos); // mapper类名 String methodName = statementId.substring(pos + 1); Class<?> mapperClass; mapperClass = Class.forName(namespace); synchronized (this) { meta = CACHED.get(statementId); if (meta == null) { meta = buildMapperMeta(conf, namespace, methodName, mapperClass); CACHED.put(statementId, meta); } } } return meta; }
@SuppressWarnings({"rawtypes", "unchecked"}) private void autoMap(Invocation invocation, String name) throws ClassNotFoundException { final Object[] queryArgs = invocation.getArgs(); final MappedStatement ms = (MappedStatement) queryArgs[0]; final Object parameter = queryArgs[1]; String statementId = ms.getId(); MapperMeta meta = getMapperMeta(ms.getConfiguration(), statementId); if (meta.isFillEntity()) { // 将泛型类加入到参数中供CrudTemplate使用 if (parameter != null) { Map map; if (parameter instanceof Map) { map = (HashMap) parameter; map.put(CrudProvider.CLASS_KEY, meta.getEntity()); } else { map = new HashMap(); map.put(CrudProvider.PARA_KEY, parameter); map.put(CrudProvider.CLASS_KEY, meta.getEntity()); } queryArgs[1] = map; } else { queryArgs[1] = meta.getEntity(); } } if (meta.isFillResultMap()) { MetaObject metaMappedStatement = getMetaObject(ms); metaMappedStatement.setValue("resultMaps", meta.getResultMaps()); } if (name.equals("query")) { final RowBounds rowBounds = (RowBounds) queryArgs[2]; if (rowBounds == null || rowBounds == RowBounds.DEFAULT) { Page p = findPageParameter(queryArgs[1]); if (p != null) { queryArgs[2] = new RowBounds(p.getOffset(), p.getLimit()); } } } }