How spring manages MyBatis and injects MyBatis mapper beans

Source: Internet
Author: User

1.spring launches two important classes of MyBatis: Sqlsessionfactorybean and Mapperfactorybean, all of which are org.mybatis.spring jar packages.

is the key to starting MyBatis, why does spring recognize these two classes? Because these two classes implement the spring interface.

The point here is that Org.mybatis.spring.SqlSessionFactoryBean and Org.mybatis.spring.mapper.mapperfactorybean[b] implement the spring interface, and produces the object.

Org.mybatis.spring.SqlSessionFactoryBean

Org.mybatis.spring.mapper.MapperFactoryBean

2.

<bean id= "Sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" >

<!--DataSource property specifies the connection pool to use--

<property name= "DataSource" ref= "DataSource"/>

<!--Configlocation property specifies the core configuration file for MyBatis--

<property name= "configlocation" value= "Config/configuration.xml"/>

</bean>

3.

Myabtis Mapper Bean Implementation class is called Org.mybatis.spring.mapper.MapperFactoryBean

The jar package is the org.mybatis.spring package

Plainly, the Java implementation Class of MyBatis is Mapperfactorybean, where the jar package hierarchy Org.mybatis.spring.mapper.MapperFactoryBean

<bean id= "Usermapper" class= "Org.mybatis.spring.mapper.MapperFactoryBean" >

<!--Sqlsessionfactory property specifies the Sqlsessionfactory instance to use--

<property name= "Sqlsessionfactory" ref= "Sqlsessionfactory"/>

<!--Mapperinterface property specifies the Mapper interface for implementing this interface and generating mapper objects--<property name= "Mapperinterface" value= " Com.yihaomen.mybatis.inter.IUserOperation "/>

</bean>

4.mybatis start, the first step is to produce sqlsessionfactory

When using the MyBatis framework, the first step is to generate an instance of the Sqlsessionfactory class (equivalent to creating a connection pool)

3.

As you can see, the Sqlsessionfactorybuilder class is responsible for building the sqlsessionfactory and provides overloaded methods for multiple builds. But many of them are in the same way as the signature, for example:

Public sqlsessionfactory Build (InputStream InputStream, String Environment, properties properties), Just because the method parameters environment and Propertiese can be null,

Therefore, in order to provide the convenience of the call, the following three methods are provided:

  

Public sqlsessionfactory Build (InputStream inputstream) public sqlsessionfactory build (InputStream InputStream, String Environment) Public sqlsessionfactory build (InputStream InputStream, properties properties)

In accordance with the above ideas to remove duplicates, the real overload method only the following three kinds:

Public sqlsessionfactory Build (InputStream InputStream, String Environment, properties properties) public Sqlsessionfactory Build (Reader Reader, String environment, properties properties) public Sqlsessionfactory build ( Configuration config)

As you can see, configuration information can be provided in three forms to the Sqlsessionfactory build method, namely InputStream (Byte stream), Reader (character stream), configuration (Class), Since the byte stream and the character stream are all part of the way to read the configuration file, it is easy to think of a sqlsessionfactory from the source of the configuration information in two ways, roughly the following code:

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




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

How spring manages MyBatis and injects MyBatis mapper beans

Related Article

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.