Where are the datasource sources of spring DataSource and MyBatis

Source: Internet
Author: User

Configure a data source
Spring contains two implementations of data sources in third-party dependency packages, one of which is Apache DBCP and the other is c3p0. You can use either of the two configuration data sources in the spring configuration file.

Configure a data source
Spring contains two implementations of data sources in third-party dependency packages, one of which is Apache DBCP and the other is c3p0. You can use either of the two configuration data sources in the spring configuration file.

Configure a data source
Spring contains two implementations of data sources in third-party dependency packages, one of which is Apache DBCP and the other is c3p0. You can use either of the two configuration data sources in the spring configuration file.

(1) How to read XML file construction

String resource = "Org/mybatis/example/mybatis-config.xml";  InputStream InputStream = resources.getresourceasstream (Resource); Sqlsessionfactory sqlsessionfactory = new Sqlsessionfactorybuilder (). Build (InputStream);

(2) Programming construction method

DataSource DataSource = Blogdatasourcefactory.getblogdatasource (); Transactionfactory transactionfactory = new Jdbctransactionfactory (); Environment environment = new Environment ("development", Transactionfactory, DataSource); Configuration configuration = new configuration (environment); Configuration.addmapper (Blogmapper.class); Sqlsessionfactory sqlsessionfactory = new Sqlsessionfactorybuilder (). Build (configuration);

The following is the source of the build method for parsing the XML file construction:

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.}}  }

Through the above lines of code, we can see that based on the structure of XML files, through the work of reading information from XML, but also to construct the configuration object and then continue to build the sqlsessionfactory work, just a little more XML parsing work , so we just need to straightforward, just press the code of the Analytic programming construction, or directly analyze the code of Build (Parser.parse ()) (The parameter generation process is skipped first)

The build method of programming constructs the source code is as follows (the XML-based build (Parser.parse ()) is finally tuned for this):

  

Public sqlsessionfactory build (Configuration config) {                return new defaultsqlsessionfactory (config);  }

In fact, it seems that the default implementation class for Sqlsessionfactory in MyBatis is Org.apache.ibatis.session.defaults.DefaultSqlSessionFactory, Its construction process is mainly injected into the configuration of the instance object, the configuration of the instance object can be generated by parsing the XML configuration file, may also be directly constructed through code. The above code uses a design pattern: builder, Sqlsessionfactorybuilder plays the concrete builder, the configuration class is responsible for the details of the construction work, and sqlsession is the product that is built.

The following is a basic pattern of the class diagram and builder pattern, which is read by the reader in its own control.

    

The constructor pattern is the creation mode of an object. It can completely separate the internal constituent characteristics of a complex object from the construction process of the object.

Where are the datasource sources of spring DataSource and MyBatis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.