我是靠谱客的博主 悦耳毛巾,这篇文章主要介绍解决maven打包时漏掉src/main/java资源里的xml的问题,现在分享给大家,希望可以做个参考。

在整合Spring + Mybatis框架的时候,自动扫描配置都已经配置好了。
配置如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<context:component-scan base-package="com.yd"/> <!-- 引入数据库属性文件 --> <context:property-placeholder ignore-unresolvable="true" location="classpath:config.properties" /> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath:/com/yd/pushcloud/dao/*.xml"/> </bean> <!-- DAO接口所在包名,Spring会自动查找其下的类 ,包下的类需要使用@MapperScan注解,否则容器注入会失败 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.yd.pushcloud.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean>

但是在maven install的时候会报错:

复制代码
1
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.aheizi.dao.UserMapper...

显示绑定错误,分析可知应该是在UserMapper接口和SQL的隐射文件没有结合。
第一时间检查了mapper的namespace属性,确认没有错。
后来查看了target/classes/../mapping并不存在,也就是说mapper.xml文件并没有在编译的时候放进classes中。
这就尴尬了,一开始我以为是我eclipse编译有问题,于是每次编译的时候,我都先maven clean一下,然后选择eclipse的project的clean一下我的项目,让他重新编译,最后在maven install一下,xml文件才会编译进去,但是如果eclipse的clean时间太短也会导致xml没编译进去,这让我十分苦恼。
后来通过查看maven打包机制,才发现maven默认只把src/main/resources路径下的资源文件打包,src/main/java目录下只编译Java文件,问题就迎刃而解了。
解决办法有两种:
1.在pom.xml下的build节点下加入以下配置

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- 填补maven打包的坑,使maven编译时包含mybatis的sql映射xml文件 --> <resources> <resource> <directory>src/main/resources</directory> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources>

意思就是把src/main/java目录也加入要打包的资源路径。

2.把mybatis的sqlmapping配置文件放到src/main/resources目录下,但是这样会显得很丑。
建议使用第一种方法!

最后

以上就是悦耳毛巾最近收集整理的关于解决maven打包时漏掉src/main/java资源里的xml的问题的全部内容,更多相关解决maven打包时漏掉src/main/java资源里内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(75)

评论列表共有 0 条评论

立即
投稿
返回
顶部