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.