博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mybatis的sqlSessionFactory的加载过程
阅读量:6321 次
发布时间:2019-06-22

本文共 6560 字,大约阅读时间需要 21 分钟。

使用过SSM的框架的都知道mybatis这个持久层框架,今天小编就来简单说说这个框架的核心工厂类sqlSessionFactory的加载过程,一般的SSM框架我们都会在spring的application.xml中引入如下的配置:

其中的SqlSessionFactoryBean便是加载sqlSessionFactory的入口,首先我们来看看这个类的源代码:

public class SqlSessionFactoryBean implements FactoryBean
, InitializingBean, ApplicationListener
{ private static final Log logger = LogFactory.getLog(SqlSessionFactoryBean.class); private Resource configLocation; private Resource[] mapperLocations; private DataSource dataSource; private TransactionFactory transactionFactory; private Properties configurationProperties; private SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); private SqlSessionFactory sqlSessionFactory;

其中标红的两处,就是我们在application.xml中注入的两个属性,从源码中我们可以看出该类实现了InitializingBean接口,实现了其afterPropertiesSet()方法,

该方法是在当前bean的所有属性被初始化完成之后再执行,也就是其中的datasource和configurationProperties等属性,接下来我们看看这个方法主要干什么了?

1 public void afterPropertiesSet() throws Exception {2     notNull(dataSource, "Property 'dataSource' is required");3     notNull(sqlSessionFactoryBuilder, "Property 'sqlSessionFactoryBuilder' is required");4 5     this.sqlSessionFactory = buildSqlSessionFactory();6   }

其中第5行很明显的buildSqlSessionFactory()方法初始化了sqlSessionFactory ,接下来我们看看这个方法的主要行为:

1 Configuration configuration;  2   3     XMLConfigBuilder xmlConfigBuilder = null;  4     if (this.configLocation != null) {  5       xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), null, this.configurationProperties);  6       configuration = xmlConfigBuilder.getConfiguration();  7     } else {  8       if (logger.isDebugEnabled()) {  9         logger.debug("Property 'configLocation' not specified, using default MyBatis Configuration"); 10       } 11       configuration = new Configuration(); 12       configuration.setVariables(this.configurationProperties); 13     } 14  15     if (this.objectFactory != null) { 16       configuration.setObjectFactory(this.objectFactory); 17     } 18  19     if (this.objectWrapperFactory != null) { 20       configuration.setObjectWrapperFactory(this.objectWrapperFactory); 21     } 22  23     if (hasLength(this.typeAliasesPackage)) { 24       String[] typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage, 25           ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS); 26       for (String packageToScan : typeAliasPackageArray) { 27         configuration.getTypeAliasRegistry().registerAliases(packageToScan, 28                 typeAliasesSuperType == null ? Object.class : typeAliasesSuperType); 29         if (logger.isDebugEnabled()) { 30           logger.debug("Scanned package: '" + packageToScan + "' for aliases"); 31         } 32       } 33     } 34  35     if (!isEmpty(this.typeAliases)) { 36       for (Class
typeAlias : this.typeAliases) { 37 configuration.getTypeAliasRegistry().registerAlias(typeAlias); 38 if (logger.isDebugEnabled()) { 39 logger.debug("Registered type alias: '" + typeAlias + "'"); 40 } 41 } 42 } 43 44 if (!isEmpty(this.plugins)) { 45 for (Interceptor plugin : this.plugins) { 46 configuration.addInterceptor(plugin); 47 if (logger.isDebugEnabled()) { 48 logger.debug("Registered plugin: '" + plugin + "'"); 49 } 50 } 51 } 52 53 if (hasLength(this.typeHandlersPackage)) { 54 String[] typeHandlersPackageArray = tokenizeToStringArray(this.typeHandlersPackage, 55 ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS); 56 for (String packageToScan : typeHandlersPackageArray) { 57 configuration.getTypeHandlerRegistry().register(packageToScan); 58 if (logger.isDebugEnabled()) { 59 logger.debug("Scanned package: '" + packageToScan + "' for type handlers"); 60 } 61 } 62 } 63 64 if (!isEmpty(this.typeHandlers)) { 65 for (TypeHandler
typeHandler : this.typeHandlers) { 66 configuration.getTypeHandlerRegistry().register(typeHandler); 67 if (logger.isDebugEnabled()) { 68 logger.debug("Registered type handler: '" + typeHandler + "'"); 69 } 70 } 71 } 72 73 if (xmlConfigBuilder != null) { 74 try { 75 xmlConfigBuilder.parse(); 76 77 if (logger.isDebugEnabled()) { 78 logger.debug("Parsed configuration file: '" + this.configLocation + "'"); 79 } 80 } catch (Exception ex) { 81 throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex); 82 } finally { 83 ErrorContext.instance().reset(); 84 } 85 } 86 87 if (this.transactionFactory == null) { 88 this.transactionFactory = new SpringManagedTransactionFactory(); 89 } 90 91 Environment environment = new Environment(this.environment, this.transactionFactory, this.dataSource); 92 configuration.setEnvironment(environment); 93 94 if (this.databaseIdProvider != null) { 95 try { 96 configuration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource)); 97 } catch (SQLException e) { 98 throw new NestedIOException("Failed getting a databaseId", e); 99 }100 }101 102 if (!isEmpty(this.mapperLocations)) {103 for (Resource mapperLocation : this.mapperLocations) {104 if (mapperLocation == null) {105 continue;106 }107 108 try {109 XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(),110 configuration, mapperLocation.toString(), configuration.getSqlFragments());111 xmlMapperBuilder.parse();112 } catch (Exception e) {113 throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e);114 } finally {115 ErrorContext.instance().reset();116 }117 118 if (logger.isDebugEnabled()) {119 logger.debug("Parsed mapper file: '" + mapperLocation + "'");120 }121 }122 } else {123 if (logger.isDebugEnabled()) {124 logger.debug("Property 'mapperLocations' was not specified or no matching resources found");125 }126 }
View Code

该方法主要就是设置configuration参数,首先是从mybatis-config.xml中获取对应的配置信息,然后再从spring的注入属性中获取所有的配置信息,从后调用

sqlSessionFactoryBuilder.build(configuration)方法获取sqlSessionFactory的实例。

由于整个加载过程没有过多的难点,所以就介绍到这里,感兴趣的朋友可以自己去源码,了解更多知识。

 

转载于:https://www.cnblogs.com/ljy-20180122/p/9374754.html

你可能感兴趣的文章
简单的一条SQL,不简单的做事思维 NOT IN 、NOT EXISTS、LEFT JOIN用法差别 ...
查看>>
DataWorks:任务未运行自助排查
查看>>
ionic/cordova热部署
查看>>
「镁客早报」特斯拉裁员,马斯克解释没有办法;微软推出Azure DevOps赏金计划...
查看>>
centos 7.4 使用 pgxc_ctl 安装与使用
查看>>
Redis 单key值过大 优化方式
查看>>
【数据库】表分区
查看>>
nutz-sqltpl 1.3.4.RELEASE 发布,在 Nutz 项目中“解决 Java 拼接 SQL”问题
查看>>
城市 | 800个地铁站数据透析的京沪白领图鉴:隐形土豪、无产中产阶级和猪猪女孩...
查看>>
前端脚本!网站图片素材中文转英文
查看>>
linux的常用易忘命令
查看>>
PHP 分割字符串
查看>>
java 基于QRCode、zxing 的二维码生成与解析
查看>>
img垂直水平居中与div
查看>>
防恶意注册的思考
查看>>
订餐系统之同步美团商家订单
查看>>
使用ArrayList时设置初始容量的重要性
查看>>
Java Web-----JSP与Servlet(一)
查看>>
CentOS 6.9通过RPM安装EPEL源(http://dl.fedoraproject.org)
查看>>
IE8调用window.open导出EXCEL文件题目
查看>>