It provides integration-related APIs:
Sqlsessionfactorybean
--Providing Sqlsession object resources for integrated applications
Mapperfactorybean
--Generates a bean instance based on the specified mapper interface
Mapperscannerconfigurer
--bulk scan of mapper interfaces and generate instances based on specified packages
Sqlsessionfactorybean:
When using MyBatis alone, all operations are carried out around sqlsession, sqlsession is obtained through Sqlsessionfactory, Sqlsessionfactory is also generated by Sqlsessionfactorybuilder creation.
In Springmvc+mybatis integration, it is also necessary to sqlsession. Sqlsessionfactorybean This component generates Sqlsessionfactory objects from the original Sqlsessionfactorybuilder, providing sqlsession objects for the consolidated application.
Mapperfactorybean:
The function is to obtain the mapper object we want according to the Mapper interface, which encapsulates the implementation of the original Session.getmapper () function.
When defining Mapperfactorybean, you need to inject a few two attributes:
--sqlsessionfactorybean object for providing sqlsession
--To return the Mapper interface of the Mapper object
The Mapperfactorybean configuration is as follows:
<!--method One: Define mapper--><bean id= "Deptmapper" class= "Org.mybatis.spring.mapper.MapperFactoryBean" >< Property Name= "Mapperinterface" value= "Com.lydia.entity.DeptMapper" ></property><!-- Specify Sqlsessionfactorybean object--><property name= "Sqlsessionfactory" ref= "Sqlsessionfactory" ></property ></bean>
Mapperscannerconfigurer configuration using:
Note: when using Mapperfactorybean, when there is a mapper (which can be understood as a mapping file for a table) Mapperfactorybean, When the mapper minority can be obtained through the ApplicationContext configuration file, through the ID.
If a lot of mapper, It is necessary to use the Mapperscannerconfigurer component passed by Mybatis-spring.jar, which can automatically scan each mapper interface under the specified package and register the corresponding Mapperfactorybean object.
put the previous the configuration of the Mapperfactorybean is commented out, and the following configuration is still performed by:
<!--Method 2: You can turn the scanned mapper interface into a Mapper object--><bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" ><!--Specify the package to scan: multiple packets separated by commas--><property name= "basepackage" value= "Com.lydia,com.tarena"/><!-- Specify Sqlsessionfactory--><property name= "sqlsessionfactory" ref= "Sqlsessionfactory" ></property></ Bean>
Note: The Sqlsessionfactory property above can also be specified without specifying it, and is injected by default in autowired mode.
If the specified package is not exactly the Mapper interface we define, we can also specify the build Mapperfactorybean object by customizing the annotations.
The configuration is as follows:
<!--Method 3: As long as the Mapper class preceded by @mybatisrepository this own designated annotation Ok--><bean class= " Org.mybatis.spring.mapper.MapperScannerConfigurer "><property name=" Basepackage "value=" Com.lydia "/>< Property Name= "Annotationclass" value= "Com.lydia.annotation.MyBatisRepository"/></bean>
Custom annotations: Mybatisrepository.java public @interface mybatisrepository {}
Below also give everybody a thought, take a rest first
Rewrite Mapperfactorybean The following document will be studied together with you.
SPRIGNMVC MyBatis Integration (i) based on Mapperfactorybean