mybatis整合spring03

本章学习目标:
mybatis整合spring,Mapper接口扫描(推荐)

本片博文在上篇的基础上进行改造:https://blog.csdn.net/weixin_45493345/article/details/103932448

修改applicationContext.xml

<!-- 配置Mapper接口 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
	<!-- 关联Mapper接口 -->
	<property name="mapperInterface" value="com.imooc.dao.UserMapper"/>
	<!-- 关联SqlSessionFactory -->
	<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>

讲解:我们有多少个接口就会在上面创建多少个bean,很显然在正式生产环境这种做法是不可取的。

下面我们使用接口扫描的方式

<!-- Mapper接口的扫描 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 配置Mapper接口所在包路径 -->
		<property name="basePackage" value="com.imooc.dao"/>
	</bean>

注意:如果使用Mapper接口包扫描, 那么每个Mapper接口在Spring容器中的id名称为类名(首字母小写): 例如UserMapper -> userMapper

发布了39 篇原创文章 · 获赞 6 · 访问量 1992

猜你喜欢

转载自blog.csdn.net/weixin_45493345/article/details/103935660