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([size=x-large][color=red]contrary to the Mybatis manual[/color][/size]), 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
[size=xx-large][color=blue]must added property :
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />[/color][/size]
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.
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([size=x-large][color=red]contrary to the Mybatis manual[/color][/size]), 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
[size=xx-large][color=blue]must added property :
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />[/color][/size]
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.
本文介绍如何利用MyBatis-Spring中的MapperScannerConfigurer自动扫描并配置Mapper接口,减少手动配置工作,同时详细解释了如何正确配置SqlSessionFactory以确保Mapper正常工作,避免常见错误。
7403

被折叠的 条评论
为什么被折叠?



