Mybatis-spring allows you to inject the mapper into the service bean. When using the Mapper, call the mapper just as you would call DAO, but at this point you do not need to encode any DAO implementations because MyBatis will do it for you.
With the injected mapper, your code will not appear to 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 a mapper:
<bean id= "Usermapper" class= "Org.mybatis.spring.mapper.MapperFactoryBean" >
<property name= " Sqlsessionfactory "ref=" sqlsessionfactory "/> <property name=" mapperinterface "value="
sample. Usermapper "/>
</bean>
Now your mapper is ready to inject in the service object:
<bean id= "Fooservice" class= "Sample." Fooserviceimpl ">
<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 previous example: Usermapper) and is injected into the application. Because the agent is created in a running environment, the specified mapper must be an interface. Instead of a specific implementation class.
It is not necessary to register all the mapper in the spring XML configuration file. Instead, you can use a mapperscannerconfigurer, which will find the mapper under the Classpath and automatically create the Mapperfactorybeans. To create a mapperscannerconfigurer, you can add the following code in the configuration of spring:
<bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" >
<propery name= "Basepackage" Org.mybatis.spring.sample.mapper "/>
</bean>
The
Basepackage property allows you to set the basic package path for the Mapper interface file. You can use semicolons or commas as delimiters to set more than one package path. Each mapper is recursively searched for in the specified package path.
Note that there is no need to specify Sqlsessionfactory or Sqlsessiontemplate, because Mapperscannerconfigurer will create Mapperfactorybean and then assemble automatically. However, if you use more than one datasource (and therefore also multiple sqlsessionfactory), then automatic assembly may fail. In this case, you can use the Sqlsessionfactory or Sqlsessiontemplate property to set the correct factory/template.
&NBSP
Mapperscannerconfigurer supports filtering to create a mapper from the specified creation interface or annotation. The Annotationclass property specifies the name of the note to look for. The Markerinterface property specifies the parent interface to look for. If both are specified, the mapper that is 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 a mapper. The mapper discovered by
&NBSP
will use spring to name the default naming policy for automatic detection components. That is, if no annotation is found, it uses the not-capitalized, unqualified class name of the mapper. However, if a @component or jsr-330@named annotation is found, it gets the name.