Integration of mybatis3 and spring with sqlsessionfactorybean

Source: Internet
Author: User

In the Basic mybatis, the session factory can be created using sqlsessionfactorybuilder. In mybatis-spring, use sqlsessionfactorybean instead.

Sqlsessionfactorybean implements the spring factorybean interface. This shows that the bean finally created by spring is not the sqlsessionfactorybean itself, but the method result returned by the GetObject () of the factory class. In this case, spring will create the sqlsessionfactory object for you at the application startup, and store it in the name of sqlsessionfactory.

In general mybatis-spring usage, you do not need to directly use sqlsessionfactorybean or its corresponding sqlsessionfactory. On the contrary, the session factory will be injected into mapperfactorybean or other Dao that extends sqlsessiondaosupport.

Attribute

Sqlsessionfactory has a separate mandatory attribute, that is, the JDBCDatasource. This can be any datasource. The configuration should be the same as that of other spring databases.

A common attribute isConfiglocationIs used to specifyMybatis xml configuration file path. If the basic mybatis configuration needs to be changed, this is where it is needed. This is usually the part of <Settings> or <typealiases>. Note that this configuration file does not need to be a complete mybatis configuration. The transaction manager of the data source and mybatis is ignored in any environment. Sqlsessionfactorybean creates its own, which is required to use these values to customize the environment of mybatis.

If the mybatis er XML file does not exist in the same path as the er Class, another reason to configure the file is that it does. There are two options to use this configuration. First, manuallyUse the <mappers> section in the XML configuration file of mybatis to specify the class path.. The second is to use factory BeanMapperlocationsAttribute. The mapperlocations attribute usesList of resource locations. This attribute can be used to specify the location of the mybatis XML er file. For example:

<Bean id = "sqlsessionfactory" class = "org. mybatis. spring. sqlsessionfactorybean "> <property name =" datasource "ref =" datasource "/> <property name =" configlocation "value =" classpath: mybatis. XML "/> <property name =" mapperlocations "value =" classpath: mapper /**/*. XML "/> </bean>

In the container environment to manage transactions, a possible attribute isTransactionfactoryclass.

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.