Differences Between spring and mybatis integrated configuration of MapperFactoryBean and MapperScannerConfigurer,
MapperFactoryBean and MapperScannerConfigurer
Spring injection er
Four data processing methods using mybatis
① SqlSessionTemplate ② SqlSessionDaoSupport ③ MapperFactoryBean ④ MapperScannerConfigurer
① SqlSessionTemplate: This configuration file needs to be written to inject sqlsession into the implementation class, and then use sqlsession, which is fine-grained control (non-interface development)
② SqlSessionDaoSupport (existing and older jar packages): This only needs to inherit special classes in the implementation class (such as HibernateDaoSupport)
You can use sqlsession (non-interface development)
③ MapperFactoryBean: Write the configuration file and reference all corresponding interfaces in the configuration file. You do not need to write implementation classes and need to manually inject them (Dynamic proxy development)
④ MapperScannerConfigurer: To write the configuration file, you only need to provide the package where the interface is located. The interface in the package is automatically introduced,
No need to write implementation classes. @ Autowired (Dynamic proxy)
In SpringMVC, you do not have to write the corresponding implementation for every DAO, but create it for SpringMVC.
To replace the manual use of SqlSessionDaoSupport or SqlSessionTemplate to write data access object (DAO) code, MyBatis-Spring provides a dynamic proxy implementation: MapperFactoryBean. This class allows you to directly inject the data ER interface into your service layer bean. When you use the Mapper, you can call them just as you call your DAO, but you do not need to write any DAO implementation code, because MyBatis-Spring will create a proxy for you.
MapperFactoryBean
The data ER interface can be added to Spring as follows:
The proxy class created by MapperFactoryBean implements the UserMapper interface and injects it into the application. Because the agent is created in the Runtime environment (Runtime, Translator's note), the specified mapper must be an interface rather than a specific implementation class.
If UserMapper has an XML er file corresponding to MyBatis, if the XML file is in the same path as the er Class (that is. java and. the xml file is in the same package), which is automatically parsed by MapperFactoryBean. You do not need to specify the ER in the MyBatis configuration file unless the XML file of the er is in different class paths.
Note: When MapperFactoryBean requires SqlSessionFactory or SqlSessionTemplate. These can be set through their respective SqlSessionFactory or SqlSessionTemplate attributes, or they can be automatically assembled by Spring. If both attributes are set, SqlSessionFactory will be ignored, because SqlSessionTemplate requires a session factory setting, which will be used by MapperFactoryBean.
You can directly inject the Mapper in the business/service object in the same way as injecting any Spring bean:
This bean can be directly used in the application logic:
Note that there is no reference to SqlSession or MyBatis in this Code. There is no code to create, enable, or disable a session. MyBatis-Spring will care about it.
MapperScannerConfigurer
There is no need to register all the mappers in the XML configuration file of Spring. Instead, you can use a MapperScannerConfigurer to find the er under the class path and automatically create MapperFactoryBean.
To create a MapperScannerConfigurer, add the following code in Spring Configuration:
The basePackage attribute allows you to set the basic package path for the Mapper interface file. You can use semicolons or commas as separators to set more than one
Package path. Each mapper is recursively searched in the specified package path.
Summary:
MapperFactoryBean requires one Configuration
MapperScannerConfigurer can be configured with multiple