|
@@ -21,6 +21,7 @@ import org.apache.ibatis.builder.xml.XMLMapperEntityResolver;
|
|
|
import org.apache.ibatis.datasource.DataSourceFactory;
|
|
|
import org.apache.ibatis.executor.ErrorContext;
|
|
|
import org.apache.ibatis.executor.loader.ProxyFactory;
|
|
|
+import org.apache.ibatis.io.ResolverUtil;
|
|
|
import org.apache.ibatis.io.Resources;
|
|
|
import org.apache.ibatis.io.VFS;
|
|
|
import org.apache.ibatis.logging.Log;
|
|
@@ -45,9 +46,9 @@ import org.apache.ibatis.type.JdbcType;
|
|
|
import javax.sql.DataSource;
|
|
|
import java.io.InputStream;
|
|
|
import java.io.Reader;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
+import java.util.HashSet;
|
|
|
import java.util.Properties;
|
|
|
+import java.util.Set;
|
|
|
|
|
|
/**
|
|
|
* <p>
|
|
@@ -366,40 +367,16 @@ public class MybatisXMLConfigBuilder extends BaseBuilder {
|
|
|
}
|
|
|
|
|
|
private void mapperElement(XNode parent) throws Exception {
|
|
|
- List<String> resources = new ArrayList<String>();
|
|
|
- List<String> urls = new ArrayList<String>();
|
|
|
- List<String> mapperClasses = new ArrayList<String>();
|
|
|
+ /**
|
|
|
+ * 定义集合 用来分类放置mybatis的Mapper与XML 按顺序依次遍历
|
|
|
+ */
|
|
|
if (parent != null) {
|
|
|
- for (XNode child : parent.getChildren()) {
|
|
|
- if ("package".equals(child.getName())) {
|
|
|
- String mapperPackage = child.getStringAttribute("name");
|
|
|
- configuration.addMappers(mapperPackage);
|
|
|
- } else {
|
|
|
- String resource = child.getStringAttribute("resource");
|
|
|
- String url = child.getStringAttribute("url");
|
|
|
- String mapperClass = child.getStringAttribute("class");
|
|
|
- if (resource != null && url == null && mapperClass == null) {
|
|
|
- resources.add(resource);
|
|
|
- } else if (resource == null && url != null && mapperClass == null) {
|
|
|
- urls.add(url);
|
|
|
-
|
|
|
- } else if (resource == null && url == null && mapperClass != null) {
|
|
|
- mapperClasses.add(mapperClass);
|
|
|
-
|
|
|
- } else {
|
|
|
- throw new BuilderException(
|
|
|
- "A mapper element may only specify a url, resource or class, but not more than one.");
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- for (String url : urls) {
|
|
|
- ErrorContext.instance().resource(url);
|
|
|
- InputStream inputStream = Resources.getUrlAsStream(url);
|
|
|
- //TODO
|
|
|
- MybatisXMLMapperBuilder mapperParser = new MybatisXMLMapperBuilder(inputStream, configuration, url,
|
|
|
- configuration.getSqlFragments());
|
|
|
- mapperParser.parse();
|
|
|
- }
|
|
|
+ //指定在classpath中的mapper文件
|
|
|
+ Set<String> resources = new HashSet<String>();
|
|
|
+ //指向一个mapper接口
|
|
|
+ Set<Class<?>> mapperClasses = new HashSet<Class<?>>();
|
|
|
+ setResource(parent, resources, mapperClasses);
|
|
|
+ // 依次遍历 首先 resource 然后 mapper
|
|
|
for (String resource : resources) {
|
|
|
ErrorContext.instance().resource(resource);
|
|
|
InputStream inputStream = Resources.getResourceAsStream(resource);
|
|
@@ -408,11 +385,47 @@ public class MybatisXMLConfigBuilder extends BaseBuilder {
|
|
|
configuration.getSqlFragments());
|
|
|
mapperParser.parse();
|
|
|
}
|
|
|
- for (String mapper : mapperClasses) {
|
|
|
- Class<?> mapperInterface = Resources.classForName(mapper);
|
|
|
- configuration.addMapper(mapperInterface);
|
|
|
+ for (Class<?> mapper : mapperClasses) {
|
|
|
+ //TODO
|
|
|
+ configuration.addMapper(mapper);
|
|
|
}
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * 查找mybatis配置文件填充至Set集合
|
|
|
+ *
|
|
|
+ * @param parent 节点
|
|
|
+ * @param resources
|
|
|
+ * @param mapper
|
|
|
+ * @throws ClassNotFoundException
|
|
|
+ */
|
|
|
+ private void setResource(XNode parent, Set<String> resources, Set<Class<?>> mapper) throws ClassNotFoundException {
|
|
|
+ for (XNode child : parent.getChildren()) {
|
|
|
+ if ("package".equals(child.getName())) {
|
|
|
+ String mapperPackage = child.getStringAttribute("name");
|
|
|
+ ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<Class<?>>();
|
|
|
+ resolverUtil.find(new ResolverUtil.IsA(Object.class), mapperPackage);
|
|
|
+ Set<Class<? extends Class<?>>> mapperSet = resolverUtil.getClasses();
|
|
|
+ for (Class<?> mapperClass : mapperSet) {
|
|
|
+ mapper.add(mapperClass);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ String resource = child.getStringAttribute("resource");
|
|
|
+ String url = child.getStringAttribute("url");
|
|
|
+ String mapperClass = child.getStringAttribute("class");
|
|
|
+ if (resource != null && url == null && mapperClass == null) {
|
|
|
+ resources.add(resource);
|
|
|
+ } else if (resource == null && url != null && mapperClass == null) {
|
|
|
+ resources.add(url);
|
|
|
+ } else if (resource == null && url == null && mapperClass != null) {
|
|
|
+ Class<?> mapperInterface = Resources.classForName(mapperClass);
|
|
|
+ mapper.add(mapperInterface);
|
|
|
+ } else {
|
|
|
+ throw new BuilderException(
|
|
|
+ "A mapper element may only specify a url, resource or class, but not more than one.");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|