在springboot中,mybatis引用的jar包
org.mybatis.spring.boot mybatis-spring-boot-starter2.1.3
mybatis的配置一般放在application.yml文件中
mybatis: mapper-locations: classpath:mybatis/mapper/*Mapper.xml type-aliases-package: com.xxx.xlt.model.po
mybatis自动配置主要跟mybatis-spring-boot-autoconfigure有关,该jar包中的MybatisProperties可以读取application.properties文件中以mybatis开头的配置信息
@ConfigurationProperties(prefix = MybatisProperties.MYBATIS_PREFIX) public class MybatisProperties { public static final String MYBATIS_PREFIX = "mybatis"; private static final ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver(); private String configLocation; private String[] mapperLocations; private String typeAliasesPackage; private Class> typeAliasesSuperType; private String typeHandlersPackage; private boolean checkConfigLocation = false; private ExecutorType executorType; private Class extends LanguageDriver> defaultscriptingLanguageDriver; private Properties configurationProperties; @NestedConfigurationProperty private Configuration configuration; }
在应用启动过程中就会读取配置文件中的mybatis配置信息
MybatisAutoConfiguration可以实现mybatis的自动配置,他的说明原文是这样的
Auto-Configuration for Mybatis. Contributes a SqlSessionFactory and a SqlSessionTemplate. If org.mybatis.spring.annotation.MapperScan is used, or a configuration file is specified as a property, those will be considered, otherwise this auto-configuration will attempt to register mappers based on the interface definitions in or under the root auto-configuration package.
意思是说:自动配置mybatis, 生成一个SqlSessionFactory和SqlSessionTemplate的bean。如果使用了@MapperScan注解或者是一个配置文件被指定为一个属性,这些将会被考虑。否则,自动配置将会尝试基于在自动配置根package下的接口定义去注册mappers.
@org.springframework.context.annotation.Configuration // SqlSessionFactory和SqlSessionFactoryBean位于类路径上时实例化bean @ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class }) // 自动装配一般使用,beanFactory包含了指定的bean的时候,条件匹配 @ConditionalOnSingleCandidate(DataSource.class) // 开启MybatisProperties.class的自动参数映射 @EnableConfigurationProperties(MybatisProperties.class) // 在DataSourceAutoConfiguration,MybatisLanguageDriverAutoConfiguration初始化之后,进行自动配置 @AutoConfigureAfter({ DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class }) public class MybatisAutoConfiguration implements InitializingBean { private final MybatisProperties properties; private final Interceptor[] interceptors; private final TypeHandler[] typeHandlers; private final LanguageDriver[] languageDrivers; private final ResourceLoader resourceLoader; private final DatabaseIdProvider databaseIdProvider; private final ListconfigurationCustomizers; // bean初始化时执行 @Override public void afterPropertiesSet() { checkConfigFileExists(); } private void checkConfigFileExists() { if (this.properties.isCheckConfigLocation() && StringUtils.hasText(this.properties.getConfigLocation())) { Resource resource = this.resourceLoader.getResource(this.properties.getConfigLocation()); Assert.state(resource.exists(), "Cannot find config location: " + resource + " (please add config file or check your Mybatis configuration)"); } } // beanFactory中缺少SqlSessionFactory时会初始化一个 @Bean @ConditionalOnMissingBean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean factory = new SqlSessionFactoryBean(); factory.setDataSource(dataSource); factory.setVfs(SpringBootVFS.class); if (StringUtils.hasText(this.properties.getConfigLocation())) { factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation())); } applyConfiguration(factory); if (this.properties.getConfigurationProperties() != null) { factory.setConfigurationProperties(this.properties.getConfigurationProperties()); } if (!ObjectUtils.isEmpty(this.interceptors)) { factory.setPlugins(this.interceptors); } if (this.databaseIdProvider != null) { factory.setDatabaseIdProvider(this.databaseIdProvider); } if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) { factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage()); } if (this.properties.getTypeAliasesSuperType() != null) { factory.setTypeAliasesSuperType(this.properties.getTypeAliasesSuperType()); } if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) { factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage()); } if (!ObjectUtils.isEmpty(this.typeHandlers)) { factory.setTypeHandlers(this.typeHandlers); } if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) { factory.setMapperLocations(this.properties.resolveMapperLocations()); } Set factoryPropertyNames = Stream .of(new BeanWrapperImpl(SqlSessionFactoryBean.class).getPropertyDescriptors()).map(PropertyDescriptor::getName) .collect(Collectors.toSet()); Class extends LanguageDriver> defaultLanguageDriver = this.properties.getDefaultscriptingLanguageDriver(); if (factoryPropertyNames.contains("scriptingLanguageDrivers") && !ObjectUtils.isEmpty(this.languageDrivers)) { // Need to mybatis-spring 2.0.2+ factory.setscriptingLanguageDrivers(this.languageDrivers); if (defaultLanguageDriver == null && this.languageDrivers.length == 1) { defaultLanguageDriver = this.languageDrivers[0].getClass(); } } if (factoryPropertyNames.contains("defaultscriptingLanguageDriver")) { // Need to mybatis-spring 2.0.2+ factory.setDefaultscriptingLanguageDriver(defaultLanguageDriver); } return factory.getObject(); } // beanFactory中缺少SqlSessionTemplate时会初始化一个 @Bean @ConditionalOnMissingBean public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { ExecutorType executorType = this.properties.getExecutorType(); if (executorType != null) { return new SqlSessionTemplate(sqlSessionFactory, executorType); } else { return new SqlSessionTemplate(sqlSessionFactory); } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)