Get the App
SLTechnology News&Howtos  ›  Database  › 

Mybatis source code [01.SqlSessionFactoryBuilder]

Shulou Source: shulou.com Published: 2022-06-01 20:05:56 10月02日 Update

It can be said that each MyBatis is centered on an instance of SqlSessionFactory. SqlSessionFactory instances can be built through SqlSessionFactoryBuilder. First, you can build SqlSessionFactory through XML configuration files, and second, you can build it through Java API. But no matter how you do it, there is a Configuration that runs through it, and the various configurations are implemented through Configuration instances.

Public class SqlSessionFactoryBuilder {/ / (1) get SqlSessionFactory public SqlSessionFactory build (Reader reader) {return build (reader, null, null);} / / (2) get SqlSessionFactory from configuration file and set which environment parameters (development / production environment) public SqlSessionFactory build (Reader reader, String environment) {return build (reader, environment, null) } / / (3) get the SqlSessionFactory from the configuration file and set which configuration parameters are dependent (property configuration file, which properties can be used multiple times in the configuration file in the form of ${propName} syntax) public SqlSessionFactory build (Reader reader, Properties properties) {return build (reader, null, properties) } / / General build function-(1), (2), (3) build this function public SqlSessionFactory build (Reader reader, String environment, Properties properties) {try {/ / delegate XMLConfigBuilder to parse the xml file and return a Configuration object. The generation of SqlSessionFactory depends on this Configuration object 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. }} / / (4) get SqlSessionFactory public SqlSessionFactory build (InputStream inputStream) {return build (inputStream, null, null) from the data stream;} / / (5) get the SqlSessionFactory from the data stream and set which environment parameters (development / production environment) public SqlSessionFactory build (InputStream inputStream, String environment) {return build (inputStream, environment, null) } / / (6) get the SqlSessionFactory from the data flow and set which configuration parameters are dependent (property configuration files, which properties can be used multiple times in the configuration file in the form of ${propName} syntax) public SqlSessionFactory build (InputStream inputStream, Properties properties) {return build (inputStream, null, properties) } / / General build function II: (4), (5), (6) Internal implementations of the build function all call this function public SqlSessionFactory build (InputStream inputStream, String environment, Properties properties) {try {XMLConfigBuilder parser = new XMLConfigBuilder (inputStream, environment, properties); return build (parser.parse ());} catch (Exception e) {throw ExceptionFactory.wrapException ("Error building SqlSession.", e) } finally {ErrorContext.instance (). Reset (); try {inputStream.close ();} catch (IOException e) {/ / Intentionally ignore. Prefer previous error. }} / / Universal builder one and generic builder two finally call this function, take the Configuration generated by XMLConfigBuilder as a parameter, and return the DefaultSqlSessionFactory object public SqlSessionFactory build (Configuration config) {return new DefaultSqlSessionFactory (config);}}

Tags: Configuration functions files environment parameters properties can be passed through instance object data data flow mode form syntax development production delegation generation source code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Tech Info Xiaomi macOS Apple