Mybatis中接口和对应的mapper文件位置配置深入剖析
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--配置映射文件方式一 -->
<!--<property name="configLocation" value="classpath:mybatis-config.xml"></property>-->
<!-- 配置映射文件方式二 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
<constructor-arg index="1" value="BATCH" />
</bean>
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="org.mybatis.spring.sample.mapper.UserMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类,扫描所有dao -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lq.dao" />
<property name="annotationClass" value="org.springframework.stereotype.Repository" />
<!-- 名字为sqlSessionFactory时即为默认,可以不配置 -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<!-- 配置了sqlSessionTemplateBeanName,sqlSessionFactoryBeanName将被覆盖,二者选其一即可 -->
<!--<property name="sqlSessionTemplateBeanName" value="sqlSessionTemplate"></property>-->
<!-- 实现了该接口,就认为是mapper -->
<!--<property name="markerInterface" value=""/>-->
</bean>
配置1和4即可,由SqlSessionFactory建立SqlSession即可连接到数据库,然后根据mapper生成动态代理类,完成增删改查业务。