Mybatis整合spring主要是通过mybatis-spring这个jar包来处理的,主要有两个重要的过程:
1.SqlSessionFactory,SqlSession的初始化
2.mapper的解析和生成代理对象
1.SqlSessionFactory,SqlSession的初始化,首先我们看一下项目中的使用配置
//这里通过SqlSessionFactoryBean去生成SqlSessionFactory对象
@Bean
public SqlSessionFactory mcpSqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSourceMcp());
sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath:mapper/mcp/*.xml"));
sessionFactoryBean.setTypeAliasesPackage("com.weimob.saas.marketing.scloud.service.impl.module.mcp.*");
return sessionFactoryBean.getObject();
}
//这里通过SqlSessionFactory 获取SqlSessionTemplate
@Bean
public SqlSessionTemplate mcpSqlSessionTemplate() throws Exception {
SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(mcpSqlSessionFactory());
return sqlSessionTemplate;
}
//这个是配置的事务处理器
@Bean
public DataSourceTransactionManager mcpDataSourceTransactionManager() {
return new DataSourceTransactionManager(dataSourceMcp());
}
1.1我们先看一下SqlSessionFactoryBean 这个类
实现了InitializingBean接口,我们看一下afterPropertiesSet这个方法:初始化sqlSessionFactory
public void afterPropertiesSet() throws Exception {
notNull(dataSource, "Property 'dataSource' is required");
notNull(sqlSessionFactoryBuilder, "Property 'sqlSessionFactoryBuilder' is required");
state((configuration == null && configLocation == null) || !(configuration != null && configLocation != null),
"Property 'configuration' and 'configLocation' can not specified with together");
//创建了sqlSessionFactory 对象
this.sqlSessionFactory = buildSqlSessionFactory();
}
//创建sqlSessionFactory 对象
//这里主要是通过配置的一些参数进行解析数据 生成Configuration
//然后通过sqlSessionFactoryBuilder和Configuration 来创建SqlSessionFactory
//这里解析跟我们在之前的文章里解析 普通mybatis的执解析是一样的,就不做详细解析了
protected SqlSessionFactory buildSqlSessionFactory() throws IOException {
Configuration configuration;
XMLConfigBuilder xmlConfigBuilder = null;
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);
}
if (this.objectWrapperFactory != null) {
configuration.setObjectWrapperFactory(this.objectWrapperFactory);
}
if (this.vfs != null) {
configuration.setVfsImpl(this.vfs);
}
if (hasLength(this.typeAliasesPackage)) {
String[] typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage,
ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
for (String packageToScan : typeAliasPackageArray) {
configuration.getTypeAliasRegistry().registerAliases(pa