1 InputStream rd = Resources.getResourceAsStream("db.xml"); 2 SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(rd);
能夠看出,SqlSessionFactory是由SqlSessionFactoryBuilder類中的方法build建立的mybatis
public SqlSessionFactory build(Reader reader, String environment, Properties properties) { try { XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties); return build(parser.parse()); } catch (Exception e) { throw ExceptionFactory.wrapException("Error building SqlSession.", e); } finally { ErrorContext.instance().reset(); try { reader.close(); } catch (IOException e) { // Intentionally ignore. Prefer previous error. } } }
實現的功能就是讀取mybatis的配置文件,將並將其中的配置轉化爲configuration對象,而後調用DefaultSqlSessionFactory的構造方法,將其賦值給DefaultSqlSessionFactory中的configuration引用。 最後返回DefaultSqlSessionFactory對象。ui