首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何配置Spring以查找类文件夹/目录中的XML?

如何配置Spring以查找类文件夹/目录中的XML?
EN

Stack Overflow用户
提问于 2022-08-08 14:05:28
回答 1查看 453关注 0票数 0

我有一个大型的Spring + MyBatis应用程序,我试图将它转换为Spring+ MyBatis AWS (+ layers)函数。我试图通过基于https://www.springcloud.io/post/2022-03/springboot-mybatis-mysql/#gsc.tab=0的小步骤来完成这一任务。

本质上,我希望将XML映射器移动到相应的java映射器所在的位置(与我最初的应用程序相同,无论出于什么原因,该应用程序工作得很好):

但是,当我移动XML时,我似乎无法将MyBatis配置为工作。我在application.yml文件中尝试了以下设置,但没有成功:

代码语言:javascript
复制
server:
  port: 9090
  
...

mybatis:
  mapper-locations: classpath:mapper/**/*-mapper.xml                    # OK with mapper under .../resources/mapper
#  mapper-locations: classpath:**/*-mapper.xml                          # OK with mapper under .../resources/mapper
#  mapper-locations: classpath:**/*-mapper.xml                          # NOT OK with mapper under io/springcloud/.../mapper
#  mapper-locations: classpath:io/springcloud/mybatis/**/*-mapper.xml   # NOT OK with mapper under io/springcloud/.../mapper

如果移动XML映射程序,在运行mvn安装时,它通常会像这样失败:

代码语言:javascript
复制
...
2022-08-08 14:57:30.673  INFO 13716 --- [           main] o.s.t.c.transaction.TransactionContext   : Began transaction (1) for test context [DefaultTestContext@52f27fbd testClass = ApplicationTest, testInstance = io.springcloud.test.ApplicationTest@6f204a1a, testMethod = test@ApplicationTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@4c163e3 testClass = ApplicationTest, locations = '{}', classes = '{class io.springcloud.mybatis.Application}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true, server.port=0}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@6b19b79, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@75f32542, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@27ae2fd0, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@15b204a1, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@419c5f1a, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@4d49af10], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> false, 'org.springframework.test.context.event.ApplicationEventsTestExecutionListener.recordApplicationEvents' -> false]]; transaction manager [org.springframework.jdbc.support.JdbcTransactionManager@3d24420b]; rollback [false]
2022-08-08 14:57:31.742  INFO 13716 --- [           main] o.s.t.c.transaction.TransactionContext   : Committed transaction for test: [DefaultTestContext@52f27fbd testClass = ApplicationTest, testInstance = io.springcloud.test.ApplicationTest@6f204a1a, testMethod = test@ApplicationTest, testException = org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): io.springcloud.mybatis.mapper.FooMapper.now, mergedContextConfiguration = [WebMergedContextConfiguration@4c163e3 testClass = ApplicationTest, locations = '{}', classes = '{class io.springcloud.mybatis.Application}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true, server.port=0}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@6b19b79, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@75f32542, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@27ae2fd0, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@15b204a1, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@419c5f1a, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@4d49af10], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> false, 'org.springframework.test.context.event.ApplicationEventsTestExecutionListener.recordApplicationEvents' -> false]]
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 9.632 s <<< FAILURE! - in io.springcloud.test.ApplicationTest
[ERROR] test  Time elapsed: 1.701 s  <<< ERROR!
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): io.springcloud.mybatis.mapper.FooMapper.now
    at io.springcloud.test.ApplicationTest.test(ApplicationTest.java:35)

2022-08-08 14:57:31.856  INFO 13716 --- [ionShutdownHook] io.undertow                              : stopping server: Undertow - 2.2.16.Final
2022-08-08 14:57:31.885  INFO 13716 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2022-08-08 14:57:31.922  INFO 13716 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Errors: 
[ERROR]   ApplicationTest.test:35 » Binding Invalid bound statement (not found): io.spri...
[INFO] 
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
...

我会很感激你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2022-08-09 01:32:43

请检查foo-mapper.xml的命名空间是否是FooMapper包名的值。见下屏幕:

foo-mapper.xml

FooMapper.java

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73279186

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档