日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

maven项目编译漏掉src/main/java下的xml配置文件

發(fā)布時間:2025/7/14 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 maven项目编译漏掉src/main/java下的xml配置文件 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在整合Spring + Mybatis框架的時候,自動掃描配置都已經(jīng)配置好了。
配置如下:

<?xml?version="1.0"?encoding="UTF-8"?> <beans?xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans??http://www.springframework.org/schema/beans/spring-beans-3.1.xsd??http://www.springframework.org/schema/context??http://www.springframework.org/schema/context/spring-context-3.1.xsd??http://www.springframework.org/schema/mvc??http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"><!--?自動掃描?--><context:component-scan?base-package="com.aheizi"?/><!--?引入配置文件?--><bean?id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property?name="location"?value="classpath:properties/jdbc.properties"?/></bean><bean?id="dataSource"?class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property?name="driverClassName"?value="${driver}"?/><property?name="url"?value="${url}"?/><property?name="username"?value="${username}"?/><property?name="password"?value="${password}"?/><!--?初始化連接大小?--><property?name="initialSize"?value="${initialSize}"></property><!--?連接池最大數(shù)量?--><property?name="maxActive"?value="${maxActive}"></property><!--?連接池最大空閑?--><property?name="maxIdle"?value="${maxIdle}"></property><!--?連接池最小空閑?--><property?name="minIdle"?value="${minIdle}"></property><!--?獲取連接最大等待時間?--><property?name="maxWait"?value="${maxWait}"></property></bean><!--?spring和MyBatis完美整合,不需要mybatis的配置映射文件?--><bean?id="sqlSessionFactory"?class="org.mybatis.spring.SqlSessionFactoryBean"><property?name="dataSource"?ref="dataSource"?/><!--?自動掃描mapping.xml文件?--><property?name="mapperLocations"?value="classpath*:com/aheizi/mapping/*.xml"></property><property?name="typeAliasesPackage"?value="com.aheizi.domain"?/></bean><!--?DAO接口所在包名,Spring會自動查找其下的類?--><bean?class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property?name="basePackage"?value="com.aheizi.dao"?/><property?name="sqlSessionFactoryBeanName"?value="sqlSessionFactory"></property></bean><!--?(事務管理)transaction?manager,?use?JtaTransactionManager?for?global?tx?--><bean?id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property?name="dataSource"?ref="dataSource"?/></bean> </beans>

但是在install的時候報了如下錯誤:

org.apache.ibatis.binding.BindingException:?Invalid?bound?statement?(not?found):?com.aheizi.dao.UserMapper.selectByUserIdat?org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:184)at?org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:38)at?org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:49)at?org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:42)at?com.sun.proxy.$Proxy11.selectByUserId(Unknown?Source)at?com.aheizi.service.impl.UserServiceImpl.getUserById(UserServiceImpl.java:20)at?com.aheizi.testmybatis.TestSpringDemo.test1(TestSpringDemo.java:37)at?sun.reflect.NativeMethodAccessorImpl.invoke0(Native?Method)at?sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)at?sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at?java.lang.reflect.Method.invoke(Method.java:601)at?org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)at?org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)at?org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)at?org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)at?org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)at?org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)at?org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)at?org.springframework.test.context.junit4.SpringJUnit4Cla***unner.runChild(SpringJUnit4Cla***unner.java:231)at?org.springframework.test.context.junit4.SpringJUnit4Cla***unner.runChild(SpringJUnit4Cla***unner.java:88)at?org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)at?org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)at?org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)at?org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)at?org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)at?org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)at?org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)at?org.junit.runners.ParentRunner.run(ParentRunner.java:309)at?org.springframework.test.context.junit4.SpringJUnit4Cla***unner.run(SpringJUnit4Cla***unner.java:174)at?org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)at?org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)at?org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)at?sun.reflect.NativeMethodAccessorImpl.invoke0(Native?Method)at?sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)at?sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at?java.lang.reflect.Method.invoke(Method.java:601)at?org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)at?org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)at?org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)at?org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)at?org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)

顯示綁定錯誤,分析可知應該是在UserMapper接口和SQL的隱射文件沒有結(jié)合。
第一時間檢查了mapper的namespace屬性,確認沒有錯。
后來查看了target/classes/../mapping并不存在,也就是說mapper.xml文件并沒有在編譯的時候放進classes中。
那么接口當然綁定不了selectByUserId這個方法。
查資料得知,有兩個解決辦法:
1.在pom.xml中添加如下代碼:

<resources>??<resource>??<directory>src/main/resources</directory>??<includes>??<include>**/*.properties</include>??<include>**/*.xml</include>??</includes>??<filtering>true</filtering>??</resource>??<resource>??<directory>src/main/java</directory>??<includes>??<include>**/*.xml</include>??</includes>??<filtering>true</filtering>??</resource>?? </resources>

添加完成后,install仍然報錯,查看classes文件,xml文件已經(jīng)存在,但是卻出現(xiàn)了其他問題,jdbc驅(qū)動找不到了,唉,無力。
解決不了心里很不舒服啊,花了一下午,終于解決了,將上面的文件修改為:

<resources>??<resource>??<directory>src/main/resources</directory>??</resource>??<resource>??<directory>src/main/java</directory>??<includes>??<include>**/*.xml</include>??</includes>??<filtering>true</filtering>??</resource>?? </resources>

意思是maven在編譯的時候class文件夾下包含src/main/resources下的文件與src/main/java下的*.properties文件。
上面一個直接指定,會加載不了。

這樣處理讓我感覺有點別扭。

2.將mapper.xml映射文件放到resources文件夾下。
文件目錄如下:

運行就不會報錯了,但是我看著還是有點別扭,就是看著不爽,等以后想到好辦法之后再換吧。

我就納了個悶了,maven這樣限制xml文件的位置,即沒讓我覺得美觀,也沒有感到實用。


轉(zhuǎn)載于:https://blog.51cto.com/zhanghuiyuan/1937067

總結(jié)

以上是生活随笔為你收集整理的maven项目编译漏掉src/main/java下的xml配置文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。