001/** 002 * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package com.mybatisflex.core.mybatis; 017 018import com.mybatisflex.core.FlexGlobalConfig; 019import com.mybatisflex.core.dialect.DbType; 020import com.mybatisflex.core.dialect.DbTypeUtil; 021import com.mybatisflex.core.exception.FlexExceptions; 022import org.apache.ibatis.exceptions.ExceptionFactory; 023import org.apache.ibatis.executor.ErrorContext; 024import org.apache.ibatis.session.Configuration; 025import org.apache.ibatis.session.SqlSessionFactory; 026import org.apache.ibatis.session.SqlSessionFactoryBuilder; 027 028import java.io.IOException; 029import java.io.InputStream; 030import java.util.Properties; 031 032public class FlexSqlSessionFactoryBuilder extends SqlSessionFactoryBuilder { 033 034 @Override 035 public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) { 036 try { 037 FlexXMLConfigBuilder parser = new FlexXMLConfigBuilder(inputStream, environment, properties); 038 return build(parser.parse()); 039 } catch (Exception e) { 040 throw ExceptionFactory.wrapException("Error building SqlSession.", e); 041 } finally { 042 ErrorContext.instance().reset(); 043 try { 044 if (inputStream != null) { 045 inputStream.close(); 046 } 047 } catch (IOException e) { 048 // Intentionally ignore. Prefer previous error. 049 } 050 } 051 } 052 053 054 @Override 055 public SqlSessionFactory build(Configuration configuration) { 056 if (!FlexConfiguration.class.isAssignableFrom(configuration.getClass())) { 057 throw FlexExceptions.wrap("only support FlexMybatisConfiguration."); 058 } 059 060 SqlSessionFactory sessionFactory = super.build(configuration); 061 DbType dbType = DbTypeUtil.getDbType(configuration.getEnvironment().getDataSource()); 062 063 //设置全局配置的 sessionFactory 和 dbType 064 initGlobalConfig(configuration, sessionFactory, dbType); 065 066 return sessionFactory; 067 } 068 069 070 /** 071 * 设置全局配置 072 * 073 * @param config 074 * @param sessionFactory 075 */ 076 private void initGlobalConfig(Configuration config, SqlSessionFactory sessionFactory, DbType dbType) { 077 FlexGlobalConfig flexGlobalConfig = new FlexGlobalConfig(); 078 flexGlobalConfig.setSqlSessionFactory(sessionFactory); 079 flexGlobalConfig.setDbType(dbType); 080 flexGlobalConfig.setConfiguration(config); 081 082 String environmentId = config.getEnvironment().getId(); 083 FlexGlobalConfig.setConfig(environmentId, flexGlobalConfig); 084 } 085 086 087 088}