Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mybatis source code [01.SqlSessionFactoryBuilder]

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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);}}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report