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.FlexConsts; 019import com.mybatisflex.core.FlexGlobalConfig; 020import com.mybatisflex.core.dialect.DbType; 021import com.mybatisflex.core.dialect.DbTypeUtil; 022import com.mybatisflex.core.exception.FlexExceptions; 023import org.apache.ibatis.exceptions.ExceptionFactory; 024import org.apache.ibatis.executor.ErrorContext; 025import org.apache.ibatis.session.Configuration; 026import org.apache.ibatis.session.SqlSessionFactory; 027import org.apache.ibatis.session.SqlSessionFactoryBuilder; 028 029import java.io.IOException; 030import java.io.InputStream; 031import java.util.Properties; 032 033public class FlexSqlSessionFactoryBuilder extends SqlSessionFactoryBuilder { 034 035 @Override 036 public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) { 037 try { 038 FlexXMLConfigBuilder parser = new FlexXMLConfigBuilder(inputStream, environment, properties); 039 return build(parser.parse()); 040 } catch (Exception e) { 041 throw ExceptionFactory.wrapException("Error building SqlSession.", e); 042 } finally { 043 ErrorContext.instance().reset(); 044 try { 045 if (inputStream != null) { 046 inputStream.close(); 047 } 048 } catch (IOException e) { 049 // Intentionally ignore. Prefer previous error. 050 } 051 } 052 } 053 054 055 @Override 056 public SqlSessionFactory build(Configuration configuration) { 057 if (!FlexConfiguration.class.isAssignableFrom(configuration.getClass())) { 058 throw FlexExceptions.wrap("only support FlexMybatisConfiguration."); 059 } 060 061 SqlSessionFactory sessionFactory = super.build(configuration); 062 DbType dbType = DbTypeUtil.getDbType(configuration.getEnvironment().getDataSource()); 063 064 //设置全局配置的 sessionFactory 和 dbType 065 initGlobalConfig(configuration, sessionFactory, dbType); 066 067 printBanner(); 068 069 return sessionFactory; 070 } 071 072 073 private void printBanner() { 074 if (!FlexGlobalConfig.getDefaultConfig().isPrintBanner()) { 075 return; 076 } 077 String banner = " __ __ _ _ _ _____ _ \n" + 078 " | \\/ |_ _| |__ __ _| |_(_)___ | ___| | _____ __\n" + 079 " | |\\/| | | | | '_ \\ / _` | __| / __| | |_ | |/ _ \\ \\/ /\n" + 080 " | | | | |_| | |_) | (_| | |_| \\__ \\ | _| | | __/> < \n" + 081 " |_| |_|\\__, |_.__/ \\__,_|\\__|_|___/ |_| |_|\\___/_/\\_\\\n" + 082 " |___/ v" + FlexConsts.VERSION + " https://mybatis-flex.com"; 083 System.out.println(banner); 084 } 085 086 /** 087 * 设置全局配置 088 * 089 * @param config 090 * @param sessionFactory 091 */ 092 private void initGlobalConfig(Configuration config, SqlSessionFactory sessionFactory, DbType dbType) { 093 FlexGlobalConfig flexGlobalConfig = new FlexGlobalConfig(); 094 flexGlobalConfig.setSqlSessionFactory(sessionFactory); 095 flexGlobalConfig.setDbType(dbType); 096 flexGlobalConfig.setConfiguration(config); 097 098 String environmentId = config.getEnvironment().getId(); 099 FlexGlobalConfig.setConfig(environmentId, flexGlobalConfig); 100 } 101 102 103}