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,再重新編譯運行,解決問題!
和大家分享下,感謝大家!