如果你的mapper接口和mapper.xml不在一个文件夹,继续往下看,如果在,直接关闭这个页面吧,对你没有帮助的
出现这个报错最根本的问题mapper接口和mapper.xml不在一个文件夹。
Mapper.xml与Mapper.class在同一个包下且同名,spring扫描Mapper.class的同时会自动扫描同名的Mapper.xml并装配到Mapper.class,问题就出在这里。
解决方法
使用配置文件配置的:
去spring和mybatis整合文件中加入<propertyname="mapperLocations"value="classpath:mapper/*.xml"/>
classpath后面的路径是以resources为根目录的路径
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
使用配置类配置的:
ssfb.setMapperLocations(this.resolveMapperLocations());这个代码和里面的resolveMapperLocations方法相当于上面配置文件中写的<propertyname="mapperLocations"value="classpath:mapper/*.xml"/>
@Bean
public SqlSessionFactoryBean getSqlSessionFactoryBean(@Autowired DataSource dataSource,@Autowired PageInterceptor pageIntercptor){
SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
//等同于<property name="dataSource" ref="dataSource"/>
ssfb.setDataSource(dataSource);
ssfb.setMapperLocations(this.resolveMapperLocations());
Interceptor[] plugins={pageIntercptor};
ssfb.setPlugins(plugins);
return ssfb;
}
/*
定义MyBatis的映射扫描,
等同于<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
*/
@Bean
public MapperScannerConfigurer getMapperScannerConfigurer(){
MapperScannerConfigurer msc = new MapperScannerConfigurer();
//等同于<property name="basePackage" value="com.itheima.dao"/>
msc.setBasePackage("net.f3322.lingwl13.mapper");
return msc;
}
public Resource[] resolveMapperLocations(){
ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
List<String> mapperLocations = new ArrayList<>();
mapperLocations.add("classpath*:net.f3322.lingwl13.mapper/*.xml");
List<Resource> resources = new ArrayList();
if (!CollectionUtils.isEmpty(mapperLocations)) {
for (String mapperLocation : mapperLocations) {
try {
Resource[] mappers = resourceResolver.getResources(mapperLocation);
resources.addAll(Arrays.asList(mappers));
} catch (IOException e) {
e.printStackTrace();
}
}
}
return resources.toArray(new Resource[resources.size()]);
}
感谢三位大佬铺路
Java书签 #MyBatis之setMapperLocations配置多个mapper路径的两种方法详解_有时有味的博客-CSDN博客_setmapperlocations
mybatis之mapperLocations配置的问题_进击的攻城狮-zxc的博客-CSDN博客_mybatis.mapper-locations不起ssm中Invalid bound statement (not found)错误的处理(IDEA中)_si_ma_tong_ming的博客-CSDN博客