最近使用idea2017搭建SSM框架遇到这样的问题: Could not resolve resource location pattern [classpath:mapping/*.xml]: class path resource [mapping/] cannot be resolved to URL because it does not exist. 目录结构如下: 之前参照别人的博客使用的是myeclipse2017搭建SSM成功了,但是使用idea就出问题了,最后网上找到了2钟解决方法:
1.直接将Java中的mapping中的.xml文件丢到resources源文件夹下。
然后修改spring-mybatis.xml
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描mapping.xml文件 --> <property name="mapperLocations" value="classpath:mapping/*.xml"/> </bean>2.或者在pom.xml文件中添加如下配置:
<build> <finalName>API</finalName> <resources> ······ <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> ······ </resources> </build>修改spring-mybatis.xml
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描mapping.xml文件 --> <property name="mapperLocations" value="classpath:com/cn/xl/ssm/mapping/*.xml"></property> </bean>分析原因: 原来maven推荐resources放资源文件, maven 并不会自动将 src/main/java 中的 xml 等资源文件复制到 target/classes 中去,那么我们就把这些mapping中的xml文件丢resources源文件夹下,或者在pom.xml文件中上述配置,使编译之后包含xml文件。