最近一直在看mybatis源码,稍有心得,接下来就然我们一起看下springboot整合mybatis的源码的步骤是怎样的
废话不多说,咱们就一起看看源码吧
首先,咱们看下配置文件,下面是我配置的配置文件,没什么多说的,都是基本配置,映射文件的位置,实体类的位置,数据库的基本信息等
之前一直有个疑问,就是咱们在配置文件中写这些配置的时候都会自动提示,一直不知道是怎么回事,看了源码,才知道原因,原来,在
spring-configuration-metadata.json这个json文件里面配置了相对应的属性,MybatisProperties与之相对应,咱们经常配置的属性有以下几个分别为
mybatis
//检查配置文件是否存在的开关(如果确实没有配置mybatis的配置文件,这个就可以不用配置了)
mybatis.check-config-location=true
//映射文件的地址
mybatis.mapper-locations=classpath:/mapper/*.xml
//实体类的地址
mybatis.type-aliases-package=com.ryx.xiaoxin_distribute.entity.pojo
datasource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://xx.xx.xx.xx:3306/ryx?autoReconnect=true&useUnicode=true&useSSL=false&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
当咱们启动springboot项目的时候,会先进入run方法(今天咱们主要分析的mybatis的源码流程,所以,spring的源码就先不分析了),
mybatis的加载的主要的类有 MybatisAutoConfiguration,在这个类中,spring会完成SqlSessionFactory的创建,SqlSessionTemplate的
简单叙述下这个流程
第一步:启动run方法
第二步:执行refreshContext方法,刷新上下文
第三步:执行prepareRefresh方法执行预刷新
第四步:invokeBeanFactoryPostProcessors(beanFactory)注册bean工厂
第五步:finishBeanFactoryInitialization(beanFactory);实例化所有的单例
第六步:遍历每一个bean实例,这里会获取到MybatisAutoConfiguration这个实例,然后调用getBean方法,
第七步:执行这里会获取到MybatisAutoConfiguration类中的checkConfigFileExists检查文件是否存在
第八步:创建SqlSessionFactory
第九步:创建SqlSessionTemplate,这里值得注意的是在MybatisAutoConfiguration中有一个静态内部类AutoConfiguredMapperScannerRegistrar
用于扫描加了@Mapper注解的接口,也就是说,如果在项目启动的时候没有配置mapperSan注解,需要在数据库接口上添加这个注解,区别在于@MapperSan
是一个全局扫描注解,只要指定包名,就能全部加载,然而@Mapper需要在每个操作数据库的接口上都要添加@Mappe接口.源码执行的时候就会看到效果,当在
springboot启动类型添加了@MapperSan注解,是不会进入这个方法的,但当没有配置的时候,是会执行这个方法的
MybatisAutoConfiguration中最主要的有三个方法
1:checkConfigFileExists:检查文件是否存在
2:创建SqlSessionFactory
3:创建SqlSessionTemplate
接下来,一起来分析下这三个方法,
checkConfigFileExists
@PostConstruct
public 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)");
}
}
//创建SqlSessionFactory对象,在创建SqlSessionFactory对象的同时,会将咱们配置在配置文件中的各种属性设置到SqlSessionFactoryBean中
@Bean
@ConditionalOnMissingBean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
//创建SqlSessionFactoryBean对象
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
//向SqlSessionFactoryBean设置属性值
factory.setDataSource(dataSource);
factory.setVfs(SpringBootVFS.class);
if (StringUtils.hasText(this.properties.getConfigLocation())) {
//向factory中添加配置文件属性
factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
}
Configuration configuration = this.properties.getConfiguration();
if (configuration == null && !StringUtils.hasText(this.properties.getConfigLocation())) {
configuration = new Configuration();
}
if (configuration != null && !CollectionUtils.isEmpty(this.configurationCustomizers)) {
for (ConfigurationCustomizer customizer : this.configurationCustomizers) {
customizer.customize(configuration);
}
}
factory.setConfiguration(configuration);
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 (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
}
if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
//添加映射文件地址
factory.setMapperLocations(this.properties.resolveMapperLocations());
}
//获取factory对象,一起看下这个方法
return factory.getObject();
}
@Override
public SqlSessionFactory getObject() throws Exception {
//当SqlSessionFactory为空,调用afterPropertiesSet构建SqlSessionFactory
if (this.sqlSessionFactory == null) {
afterPropertiesSet();
}
//不为空,直接返回
return this.sqlSessionFactory;
}
重点看下afterPropertiesSet,就是facory为空的时候需要构建factory
@Override
public void afterPropertiesSet() throws Exception {
//dataSource不能为空
notNull(dataSource, "Property 'dataSource' is required");
//sqlSessionFactoryBuilder不能为空
notNull(sqlSessionFactoryBuilder, "Property 'sqlSessionFactoryBuilder' is required");
//configuration和configLocation的属性不能一起设置
state((configuration == null && configLocation == null) || !(configuration != null && configLocation != null),
"Property 'configuration' and 'configLocation' can not specified with together");
//构建sqlSessionFacory
this.sqlSessionFactory = buildSqlSessionFactory();
}
//终于快到重点了,buildSqlSessionFactory,一起来看看这个方法,在这个方法的创建中,会加载配置文件,然后加载映射文件
protected SqlSessionFactory buildSqlSessionFactory() throws IOException {
Configuration configuration;
//构建xmlConfigBuilder对象
XMLConfigBuilder xmlConfigBuilder = null;
//校验configuration和configLocation,并且向configuration中添加属性
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);
}
}
//设置对象工厂属性
if (this.objectFactory != null) {
configuration.setObjectFactory(this.objectFactory);
}
//向configuration设置对象包装工厂
if (this.objectWrapperFactory != null) {
configuration.setObjectWrapperFactory(this.objectWrapperFactory);
}
if (this.vfs != null) {
configuration.setVfsImpl(this.vfs);
}
//解析类型别名包,就是咱们在属性文件中配置的pojo类
if (hasLength(this.typeAliasesPackage)) {
String[] typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage,
ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
for (String packageToScan : typeAliasPackageArray) {
configuration.getTypeAliasRegistry().registerAliases(packageToScan,
typeAliasesSuperType == null ? Object.class : typeAliasesSuperType);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Scanned package: '" + packageToScan + "' for aliases");
}
}
}
//解析别名
if (!isEmpty(this.typeAliases)) {
for (Class<?> typeAlias : this.typeAliases) {
configuration.getTypeAliasRegistry().registerAlias(typeAlias);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Registered type alias: '" + typeAlias + "'");
}
}
}
if (!isEmpty(this.plugins)) {
for (Interceptor plugin : this.plugins) {
configuration.addInterceptor(plugin);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Registered plugin: '" + plugin + "'");
}
}
}
//解析类型
if (hasLength(this.typeHandlersPackage)) {
String[] typeHandlersPackageArray = tokenizeToStringArray(this.typeHandlersPackage,
ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
for (String packageToScan : typeHandlersPackageArray) {
configuration.getTypeHandlerRegistry().register(packageToScan);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Scanned package: '" + packageToScan + "' for type handlers");
}
}
}
if (!isEmpty(this.typeHandlers)) {
for (TypeHandler<?> typeHandler : this.typeHandlers) {
configuration.getTypeHandlerRegistry().register(typeHandler);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Registered type handler: '" + typeHandler + "'");
}
}
}
if (this.databaseIdProvider != null) {//fix #64 set databaseId before parse mapper xmls
try {
configuration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource));
} catch (SQLException e) {
throw new NestedIOException("Failed getting a databaseId", e);
}
}
if (this.cache != null) {
configuration.addCache(this.cache);
}
//开始解析mybatis的配置文件
if (xmlConfigBuilder != null) {
try {
//调用xmlConfigBuilderpare方法执行配置文件加载
xmlConfigBuilder.parse();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Parsed configuration file: '" + this.configLocation + "'");
}
} catch (Exception ex) {
throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex);
} finally {
ErrorContext.instance().reset();
}
}
//创建事物工厂对象
if (this.transactionFactory == null) {
this.transactionFactory = new SpringManagedTransactionFactory();
}
configuration.setEnvironment(new Environment(this.environment, this.transactionFactory, this.dataSource));
//解析映射文件
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");
}
}
return this.sqlSessionFactoryBuilder.build(configuration);
}
//终于要进入解析配置文件的方法了,这个步骤有点艰难啊,是不是,哈哈
我们进入xmlConfigBuilder.parse();方法,一起来看下
//注意一个 xpath 表达式 - /configuration。这个表达式代表的是 MyBatis 的<configuration/>标签,这里选中这个标签,并传递给parseConfiguration方法
public Configuration parse() {
if (parsed) {
throw new BuilderException("Each XMLConfigBuilder can only be used once.");
}
parsed = true;
parseConfiguration(parser.evalNode("/configuration"));
return configuration;
}
private void parseConfiguration(XNode root) {
try {
//解析propertie配置
propertiesElement(root.evalNode("properties"));
//解析settings配置
Properties settings = settingsAsProperties(root.evalNode("settings"));
//加载vfs
loadCustomVfs(settings);
//解析typeAliases配置
typeAliasesElement(root.evalNode("typeAliases"));
//解析plugins配置
pluginElement(root.evalNode("plugins"));
//解析objectFactory配置
objectFactoryElement(root.evalNode("objectFactory"));
//解析objectWrapperFactory配置
objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
//解析 reflectorFactory 配置
reflectorFactoryElement(root.evalNode("reflectorFactory"));
//将settings中的信息添加到configurtion对象中
settingsElement(settings);
// 解析 environments 配置
environmentsElement(root.evalNode("environments"));
//解析 databaseIdProvider,获取并设置 databaseId 到 Configuration 对象
databaseIdProviderElement(root.evalNode("databaseIdProvider"));
//解析 typeHandlers 配置
typeHandlerElement(root.evalNode("typeHandlers"));
// 解析 mappers 配置
mapperElement(root.evalNode("mappers"));
} catch (Exception e) {
throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
}
}
//终于要进入解析配置文件的方法了,这个步骤有点艰难啊,是不是,哈哈
我们进入xmlConfigBuilder.parse();方法,一起来看下
//注意一个 xpath 表达式 - /configuration。这个表达式代表的是 MyBatis 的<configuration/>标签,这里选中这个标签,并传递给parseConfiguration方法
public Configuration parse() {
if (parsed) {
throw new BuilderException("Each XMLConfigBuilder can only be used once.");
}
parsed = true;
parseConfiguration(parser.evalNode("/configuration"));
return configuration;
}
先来看看properties属性的解析
private void propertiesElement(XNode context) throws Exception {
if (context != null) {
// 解析 propertis 的子节点,并将这些节点内容转换为属性对象 Properties
Properties defaults = context.getChildrenAsProperties();
//获取属性中resource和url的属性值
String resource = context.getStringAttribute("resource");
String url = context.getStringAttribute("url");
//两个都不为空,抛出异常
if (resource != null && url != null) {
throw new BuilderException("The properties element cannot specify both a URL and a resource based property file reference. Please specify one or the other.");
}
if (resource != null) {
//从文件中加载并解析属性文件
defaults.putAll(Resources.getResourceAsProperties(resource));
} else if (url != null) {
//通过url解析属性文件
defaults.putAll(Resources.getUrlAsProperties(url));
}
Properties vars = configuration.getVariables();
if (vars != null) {
defaults.putAll(vars);
}
parser.setVariables(defaults);
// 将属性值设置到 configuration 中
configuration.setVariables(defaults);
}
}
解析 settings 配置
private Properties settingsAsProperties(XNode context) {
if (context == null) {
return new Properties();
}
// 获取 settings 子节点中的内容
Properties props = context.getChildrenAsProperties();
// 创建 Configuration 类的对象
MetaClass metaConfig = MetaClass.forClass(Configuration.class, localReflectorFactory);
for (Object key : props.keySet()) {
// 检测 Configuration 中是否存在相关属性,不存在则抛出异常
if (!metaConfig.hasSetter(String.valueOf(key))) {
throw new BuilderException("The setting " + key + " is not known. Make sure you spelled it correctly (case sensitive).");
}
}
return props;
}
暂时分析到这里吧,其余的我就不做分析了,这块代码比较多,小伙伴们可以自行分析下这块的内容,
下一期咱们一起分析下对映射文件的解析!
Thanks!