@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()); } } } }
/** * 根据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; }