IDEA报错Could not resolve resource location pattern classpath:com/xxxxx/mapper/*.xml]解决办法
这个问题以前就遇到过,一般是这样,Eclipse和MyEclipse可以成功运行,但是放在IDEA上导入项目后编译运行起来就报上面这个错误。查了一下原因是:
IDEA编译打包的时候,xml文件没有被Mavan编译打包到target文件夹下面,即编译后的target文件下没有mapping的xml文件
解决办法:
第一步:
将spring-mybatis.xml配置文件里的sqlSessionFactory
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
<property name="mapperLocations" value="classpath:com/xxxxx/mapper/*.xml" /><!-- 扫描映射文件 -->
</bean>
改为
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
<property name="mapperLocations" value="classpath*:com/xxxxx/mapper/*.xml" /><!-- 扫描映射文件 -->
</bean>
即
classpath后面加*,写成classpath*
第二步:
将pom.xml的标签里加上如下代码:
<!-- 因为resources若不配置,可能会发送打包不全-->
<resources>
<resource>
<!--需要打包的目录-->
<directory>src/main/java</directory>
<!--目录中的文件类型-->
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
OK,再重新编译运行,解决问题!
和大家分享下,感谢大家!