spring和mybatis的整合开发(基于MapperScannerConfigurer的整合开发(适用于复杂项目,接口较多的情况))...
生活随笔
收集整理的這篇文章主要介紹了
spring和mybatis的整合开发(基于MapperScannerConfigurer的整合开发(适用于复杂项目,接口较多的情况))...
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在實際項目中,Dao層會包含很多接口,這樣會導致spring配置文件過于臃腫。這時就需要采用掃描包的形式來配置mybaits中的映射器。
采用MapperScannerConfigurer來實現。
MapperScannerConfigurer類在spring配置文件中可以配置以下幾個屬性:
1.basePackage:用于指定映射接口文件的包路徑,當需要掃描多個包時使用逗號或分號隔開。
2.annotationClass:指定了要掃描的注解名稱,只有被注解標示的類才會被配置為映射器。
3.markerInterface:指定創建映射器的接口。
4.sqlSessionFactoryBeanName:指定在spring中定義的sqlSessionFactory的bean名稱。
5.sqlSessionTemplateBeanName:指定在spring中定義的sqlSessionTemplate的bean名稱。如果定義此屬性,那么sqlSessionFactoryBeanName將不起作用。
例如:
CustomerMapper 接口,用@Repository表示此接口是一個dao層。 @Repository//標示是一個dao層public interface CustomerMapper {
public Customer findCustomerById(Integer id);
}
mapper映射文件 <mapper namespace="com.itheima.po.mapper.CustomerMapper">
<select id="findCustomerById" parameterType="Integer" resultType="customer">
select * from t_customer where id=#{id}
</select>
</mapper>
在spring的配置文件中配置 <!--基于MapperScannerConfigurer的開發-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itheima.po.mapper"/>//用于掃描映射文件包,可以將映射文件放入不同的包中。
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>//value值是配置=mybatis工廠時的id值sqlSessionFactory
<property name="annotationClass" value="org.springframework.stereotype.Repository"/>//根據注解進行掃描,成mapper對象。
</bean>
最后測試,成功。 ?
?
轉載于:https://www.cnblogs.com/jasonboren/p/10598083.html
總結
以上是生活随笔為你收集整理的spring和mybatis的整合开发(基于MapperScannerConfigurer的整合开发(适用于复杂项目,接口较多的情况))...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Maven私服(Nexus)搭建总结
- 下一篇: OO-第一单元总结