MapperScannerConfigurer and automatically marking the interfaces

Finally, those of you who don’t want to spend lots of time in wiring down each and every interface bean, can use the MapperScannerConfigurer which allows you to automatically scan the directories and mark the mapper interfaces.
1. Define the MapperScannerConfigurer and mark the basePackage.

1 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
2      <property name="basePackage" value="org.mybatis.spring.sample.mapper" />
3 </bean>

2. The mapper interfaces which are referred in the service classes need to be autowired, since their references will no longer be present in the mapper-config.xml

3. Finally, sqlSessionFactory also needs to be configured( contrary to the Mybatis manual), else the mappers will not work and will throw this exception :

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘employeeMapperInterface’ defined in file [C:\anirban\Work_Ani\SpringIntegration\WebRoot\WEB-INF\classes\com\mybatis\dao\EmployeeMapperInterface.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property ‘sqlSessionFactory’ or ‘sqlSessionTemplate’ are required

must added property :
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />



Note: There was an issue with PropertyPlaceHolderConfigurer not working when used in conjunction with mybatis-spring-3.1.0. Its resolved by replacing with mybatis-spring-3.1.1 bundle.

Thats it folks for the Mybatis stuff for this session. Now, lets jump to the Spring related changes . Recall that ,                                                                                            

Request –> Spring MVC controller –> accesses Service facade –> accesses the DAO xml/interface.

你可能感兴趣的:(MapperScannerConfigurer and automatically marking the interfaces)