Mybatis研究二 —加载xml的方式(自动与手动)
Mybatis在启动时会去读取配置文件xml的内容,我们默认都会将xml文件和mapper.java文件放在一个目录结构下,(eclipse中在同一文件夹下,idea的话java文件在src下,xml在resouces下)
这种情况下,当我们的程序启动时,只要我们配置了@MapperScan或者spring中的MapperScannerConfigurer把com.xwl.mapper设为扫描路径就会自动加载xml.
为什么会被自动扫描呢到mybatis中去呢
,因为MapperScannerConfigurer类中有一个方法,postProcessBeanDefinitionRegistry
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
if (this.processPropertyPlaceHolders) {
processPropertyPlaceHolders();
}
ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
scanner.setAddToConfig(this.addToConfig);
scanner.setAnnotationClass(this.annotationClass);
scanner.setMarkerInterface(this.markerInterface);
scanner.setSqlSessionFactory(this.sqlSessionFactory);
scanner.setSqlSessionTemplate(this.sqlSessionTemplate);
scanner.setSqlSessionFactoryBeanName(this.sqlSessionFactoryBeanName);
scanner.setSqlSessionTemplateBeanName(this.sqlSessionTemplateBeanName);
scanner.setResourceLoader(this.applicationContext);
scanner.setBeanNameGenerator(this.nameGenerator);
scanner.registerFilters();
scanner.scan(StringUtils.tokenizeToStringArray(this.basePackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));
}
从上面的scan方法进入 ClassPathBeanDefinitionScanner类
public int scan(String... basePackages) {
int beanCountAtScanStart = this.registry.getBeanDefinitionCount();
doScan(basePackages);
// Register annotation config processors, if necessary.
if (this.includeAnnotationConfig) {
AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
}
return (this.registry.getBeanDefinitionCount() - beanCountAtScanStart);
}
很明显就是这个doScan方法 通过DeBug再进入ClassPathMapperScanner类
@Override
public Set<BeanDefinitionHolder> doScan(String... basePackages) {
Set<BeanDefinitionHolder> beanDefinitions = super.doScan(basePackages);
if (beanDefinitions.isEmpty()) {
logger.warn("No MyBatis mapper was found in '" + Arrays.toString(basePackages) + "' package. Please check your configuration.");
} else {
processBeanDefinitions(beanDefinitions);
}
return beanDefinitions;
}
这里的super.doScan就是对路径下的文件进行扫描,将com.xwl.mapper下的类注册为一个Bean,但是此时还没有和mybatis关联起来,关键是下面的processBeanDefinitions点进去查看
private void processBeanDefinitions(Set<BeanDefinitionHolder> beanDefinitions) {
GenericBeanDefinition definition;
for (BeanDefinitionHolder holder : beanDefinitions) {
definition = (GenericBeanDefinition) holder.getBeanDefinition();
if (logger.isDebugEnabled()) {
logger.debug("Creating MapperFactoryBean with name '" + holder.getBeanName()
+ "' and '" + definition.getBeanClassName() + "' mapperInterface");
}
// the mapper interface is the original class of the bean
// but, the actual class of the bean is MapperFactoryBean
definition.getConstructorArgumentValues().addGenericArgumentValue(definition.getBeanClassName()); // issue #59
definition.setBeanClass(this.mapperFactoryBean.getClass());
definition.getPropertyValues().add("addToConfig", this.addToConfig);
boolean explicitFactoryUsed = false;
if (StringUtils.hasText(this.sqlSessionFactoryBeanName)) {
definition.getPropertyValues().add("sqlSessionFactory", new RuntimeBeanReference(this.sqlSessionFactoryBeanName));
explicitFactoryUsed = true;
} else if (this.sqlSessionFactory != null) {
definition.getPropertyValues().add("sqlSessionFactory", this.sqlSessionFactory);
explicitFactoryUsed = true;
}
if (StringUtils.hasText(this.sqlSessionTemplateBeanName)) {
if (explicitFactoryUsed) {
logger.warn("Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored.");
}
definition.getPropertyValues().add("sqlSessionTemplate", new RuntimeBeanReference(this.sqlSessionTemplateBeanName));
explicitFactoryUsed = true;
} else if (this.sqlSessionTemplate != null) {
if (explicitFactoryUsed) {
logger.warn("Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored.");
}
definition.getPropertyValues().add("sqlSessionTemplate", this.sqlSessionTemplate);
explicitFactoryUsed = true;
}
if (!explicitFactoryUsed) {
if (logger.isDebugEnabled()) {
logger.debug("Enabling autowire by type for MapperFactoryBean with name '" + holder.getBeanName() + "'.");
}
definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
}
}
}
很长,但是大致过一遍,你看sqlSessionTemplate,sqlSessionFactory,mapperFactoryBean是不是都很眼熟了,这里就是关键,特别是mapperFactoryBean,总之就是对扫描的bean进行一个包装 可以这么理解吧
那么xml什么时候解析呢
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx=new AnnotationConfigApplicationContext(MybatisConfig3.class);
UserMapper bean = (UserMapper) ctx.getBean("userMapper");
List<User> all = bean.findAll();
for(User user:all){
System.out.println(user.toString());
}
}
这是我的启动类,通过断点,可以发现在执行第一行代码的时候,就会根据上面设置的mapperFactoryBean定义去匹配到对应的Mybatis实现类
@Override
protected void checkDaoConfig() {
super.checkDaoConfig();
notNull(this.mapperInterface, "Property 'mapperInterface' is required");
Configuration configuration = getSqlSession().getConfiguration();
if (this.addToConfig && !configuration.hasMapper(this.mapperInterface)) {
try {
configuration.addMapper(this.mapperInterface);
} catch (Exception e) {
logger.error("Error while adding the mapper '" + this.mapperInterface + "' to configuration.", e);
throw new IllegalArgumentException(e);
} finally {
ErrorContext.instance().reset();
}
}
}
,进入MapperFactoryBean类中的checkDaoConfig方法检查是否添加过xml文件信息,如果存在就不添加,如果不存在就将当前这个bean如userMapper.class文件后缀改为.xml追找到同目录下的文件并对其进行解析,这样就会进入到configuration中,再通过MapperFactoryBean来调用代理类就实现了
我们手动配置时又是如何加载
虽然不常用,但可能有些时候xml文件不和java文件在同一目录结构下,这种时候就需要我们手动配置,如
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource());
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//mapperConfig.xml配置
factoryBean.setConfigLocation(resolver.getResource("MapperConfig.xml"));
//mapper.xml配置
factoryBean.setMapperLocations(resolver.getResources("classpath:com.xwl.mapper/*.xml"));
return factoryBean.getObject();
}
他在sqlSe’s’si’o’nFacTory生成时就已经被加载到Myabtis的configuartion中去了,debug进入
SqlSessionFactoryBean中的buildSqlSessionFactory方法里面的代码很直观就是根据上面配置set进来的值进行匹配,如
if (this.configuration != null) {
configuration = this.configuration;
if (configuration.getVariables() == null) {
configuration.setVariables(this.configurationProperties);
} else if (this.configurationProperties != null) {
configuration.getVariables().putAll(this.configurationProperties);
}
} else if (this.configLocation != null) {
xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), null, this.configurationProperties);
configuration = xmlConfigBuilder.getConfiguration();
} else {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Property 'configuration' or 'configLocation' not specified, using default MyBatis Configuration");
}
configuration = new Configuration();
if (this.configurationProperties != null) {
configuration.setVariables(this.configurationProperties);
}
}
匹配mybaitsz总配置文件
以及
if (!isEmpty(this.mapperLocations)) {
for (Resource mapperLocation : this.mapperLocations) {
if (mapperLocation == null) {
continue;
}
try {
XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(),
configuration, mapperLocation.toString(), configuration.getSqlFragments());
xmlMapperBuilder.parse();
} catch (Exception e) {
throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e);
} finally {
ErrorContext.instance().reset();
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Parsed mapper file: '" + mapperLocation + "'");
}
}
} else {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Property 'mapperLocations' was not specified or no matching resources found");
}
}
匹配指定mapper.xml文件
补充
只要找到xml文件了mybatis就会调用parse方法对他解析的,上面的代码中也有解析过程也很明了,这里就不粘贴了
总结
自动和手动引入xml的加载时机是不同的,还有注解形式的sql也是一样,只有手动配置的配置才会在sqlsessionfactory创建时就有,其他都是需要扫描到bean并对其处理后才会加载