- br/>@ConditionalOnMissingBean
- public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
- // 同样还是通过 SqlSessionFactoryBean 来创建 SqlSessionFactory
- 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()));
- }
- factory.setConfiguration(properties.getConfiguration());
- 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())) {
- // 就是我们在 application.yml 中配置的 mybatis.mapper-locations: classpath:mapper/*.xml
- factory.setMapperLocations(this.properties.resolveMapperLocations());
- }
来源: http://blog.51cto.com/13792737/2154799