近期研究了下spring集成ibatis,涉及到的一些核心类,下面一个图大体的描述了这些类之间的一些关系:
1、ibatis通过SqlMapClient接口提供了访问数据库的相关操作。Spring的org.springframework.orm.ibatis.SqlMapClientFactoryBean类,负责创建SqlMapClient实体,具体的创建代码如下:
protected SqlMapClient buildSqlMapClient(
Resource[] configLocations, Resource[] mappingLocations, Properties properties)
throws IOException {
if (ObjectUtils.isEmpty(configLocations)) {
throw new IllegalArgumentException("At least 1 'configLocation' entry is required");
}
SqlMapClient client = null;
SqlMapConfigParser configParser = new SqlMapConfigParser();
for (int i = 0; i < configLocations.length; i++) {
InputStream is = configLocations[i].getInputStream();
try {
client = configParser.parse(is, properties);
}
catch (RuntimeException ex) {
throw new NestedIOException("Failed to parse config resource: " + configLocations[i], ex.getCause());
}
}
if (mappingLocations != null) {
SqlMapParser mapParser = SqlMapParserFactory.createSqlMapParser(configParser);
for (int i = 0; i < mappingLocations.length; i++) {
try {
mapParser.parse(mappingLocations[i].getInputStream());
}
catch (NodeletException ex) {
throw new NestedIOException("Failed to parse mapping resource: " + mappingLocations[i], ex);
}
}
}
return client;
}
2、从上面的代码可以看出,ibatis通过SqlMapConfigParser类的parse方法,解析对应的XML配置文件,从而构造出SqlMapClient的实体。