默认情况下,mybatis的接口和xml文件是在同一个包下的,如果想要将它们分开,在spring配置的时候,需要在配置的id为sqlSessionFactory的这个bean下,配置一个mapperLocations属性,来映射xml文件,具体配置如下:
<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- druid数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
<!--映射xml文件,mapper是本人创建的文件夹,根据自己需求改动-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描接口所在的包,包名按需改动-->
<property name="basePackage" value="com.bestgg.mapper" />
</bean>