MyBatis-Spring allows you to inject mappers into Service Beans. When using a mapper, it's OK to call the mapper just like calling DAO, but at this time you don't need to encode any DAO implementations, because MyBatis will do it for you.
With the injected mapper, your code will not have any MyBatis-Spring dependencies and MyBatis dependencies. There is such a simple mapper in our application. You should also know that the mapper is just an interface:
public interface UserMapper { User getUser(String userId); }This is how you use MyBatis-Spring to create mappers:
<bean id="userMapper"> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> <property name="mapperInterface" value="sample.UserMapper" /> </bean>
Now your mapper is ready to inject into the Service object:
<bean id="fooService"> <property name="userMapper" ref="userMapper"/> </bean>
Note: The full class name of the mapper interface corresponds to the namespace of the mapper xml configuration file.
About MapperFactoryBean
The proxy class created by MapperFactoryBean implements the mapper interface (as in the example above: UserMapper) and is injected into the application. Because the agent is created in the run environment, the specified mapper must be an interface. Instead of a concrete implementation class.
There is no need to register all mappers in Spring's XML configuration file. Instead, you can use a MapperScannerConfigurer, which will look for mappers under the classpath and automatically create them into MapperFactoryBeans. To create a MapperScannerConfigurer, you can add the following code to Spring's configuration:
<bean> <propery name="basePackage" value="org.mybatis.spring.sample.mapper"/> </bean>
The basePackage property allows you to set the basic package path for the mapper interface file. You can set more than one package path using a semicolon or a comma as a separator. Each mapper will be recursively searched in the specified package path.
Note that there is no need to specify SqlSessionFactory or SqlSessionTemplate, because the MapperScannerConfigurer will create a MapperFactoryBean and then automatically assemble it. However, if you use more than one DataSource (and therefore multiple SqlSessionFactory), the auto-assembly may fail. In this case, you can use the sqlSessionFactory or sqlSessionTemplate property to set the correct factory/template.
MapperScannerConfigurer supports filtering to create mappers by specified creation interfaces or annotations. The annotationClass property specifies the annotation name to be searched for. The markerInterface property specifies the parent interface to be searched for. If both are specified, the mapper added to the interface matches both criteria. By default, both properties are null, so all interfaces given in the base package can be loaded as mappers.
The discovered mapper will be named using Spring's default naming policy for the automatic detection component. That is, if no annotation is found, it uses the mapper's non-capsulated non-fully qualified class name. But if you find the @Component or JSR-330@Named annotation, it will get the name.