20 MyBatis 整合Spring

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hxdeng/article/details/82898680

MyBatis 整合Spring

1. 添加MyBatis-Spring-xx.jar 包

2. 修改Spring核心配置文件

2.1 创建SqlSessionFactory

<!-- myBatis文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!-- configLocation指定全局配置文件的位置 -->
    <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    <!-- 自动扫描Mapper文件, -->
    <property name="mapperLocations" value="classpath:xxx/xxx/xxx/*.xml" />
</bean>

2.2 扫描Mapper接口的实现,以便于mapper能够自动注入

方案一

<mybatis-spring:scan base-package="com.ly.mybatis.dao"/>

方案二

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!-- 扫描dao接口 -->
    <property name="basePackage" value="com.ly.mybatis.dao" />
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>

猜你喜欢

转载自blog.csdn.net/hxdeng/article/details/82898680