Spring 启动 MyBatis @MapperScan 不再像 WAR 那样工作
Spring Boot MyBatis @MapperScan No Longer Functions as WAR
问题描述
我有一个 Spring Boot
Java
应用程序,它使用 MyBatis
Mappers
通过使用 interfaces
和 XML
.
当 运行 作为 Eclipse IDE
中的 Spring Boot
应用程序时,此设置工作正常。但是,当我使用 Mavan
将应用程序构建为 WAR
文件,并将其部署到外部 Tomcat
服务器上时,我突然收到错误消息,在部署应用程序时找不到我的映射器:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springmapper': Unsatisfied dependency expressed through field 'thingTypes'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'otherthingtypes': Invocation of init method failed; nested exception is
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.myapplicaton.dao.MybatisMapper.selectByExample
我感觉,当 运行 作为 WAR
时,应用程序不再扫描这些 MyBatis
mappers
作为 mappers
。我一定需要更改一些配置,但我不知道是什么。
====================
当前配置(当 运行 在 Eclipse IDE
作为 Spring Boot
应用时工作)..
(1) com.myapplicaton.dao 中的所有 MyBatis
java
映射器都有 @Mapper
注释:
package com.myapplicaton.dao;
@Mapper
public interface MybatisMapper {
List<Thing> selectByExample(ThingExample example);
//...
}
(2) 在应用程序启动中添加了一个MapperScan
..
@SpringBootApplication
@MapperScan({"com.myapplicaton.dao","com.myapplicaton.other.mappers"})
public class MyApplication extends SpringBootServletInitializer
{
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
(3) 在 @Autowired
类:
中使用 MyBatis
映射器
@Configuration
public class SomeConfig {
@Bean(name = "springmapper")
@Scope(value = "singleton")
public SpringMapper getSpringMapper() {
return new SpringMapper();
}
@Bean(name = "thingTypes")
@Scope(value = "singleton")
public ThingTypes thingTypes() {
return new ThingTypes();
}
}
package com.myapplicaton.other.mappers;
public class SpringMapper {
@Autowired
ThingTypes thingTypes;
//...
}
package com.myapplicaton.beans;
public class ThingTypes {
@Autowired
MybatisMapper mybatisMapper;
//...
}
====================
问题
为什么部署为 WAR
时这不起作用?
好吧,折腾了一天,总算有了答案。
首先,我不得不删除两个 'problem' bean 我有一个使用 @PostConstruct
的 init()
方法,它使用 @Autowired
Mybatis
映射器。我不知道为什么它不喜欢那样,但我不在乎,因为在我删除它们之后,应用程序作为 WAR
.. 主要是
正常启动
它曾经做过这样的事情:
package com.myapplicaton.beans;
public class ThingTypes {
@Autowired
MybatisMapper mybatisMapper;
@PostConstruct
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
private void init() {
mybatisMapper.selectByExample(example);
}
}
我遇到的第二个问题是应用程序无法在我的dao
中找到MyBatis
mapper
.xml
文件] 包部署为 WAR
。像往常一样,在 Eclipse 上 运行 时很好。
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.myapplicaton.dao.MybatisMapper.selectByExample
我检查了 WAR
文件,你瞧,Maven
跳过了 .xml
文件,只有 Java
class
文件住在那个文件夹里。我无法弄清楚如何使用 spring-boot-maven-plugin
取消跳过 .xml
文件,而且我不打算尝试找出另一个插件,所以我能够如下击败系统:
我在应用程序的 src/main/resources/
文件夹中创建了一组文件夹,其文件夹结构与 .java
interface
mappers
所在的文件夹结构完全相同。所以,我创建了 src/main/resources/com/myapplication/dao
。我把所有 .xml
文件都放在那里。
检查新 WAR 后,.xml
文件与 java
class
文件放在同一目录中,并且 MyBatis
正常工作.
欢呼。
问题描述
我有一个 Spring Boot
Java
应用程序,它使用 MyBatis
Mappers
通过使用 interfaces
和 XML
.
当 运行 作为 Eclipse IDE
中的 Spring Boot
应用程序时,此设置工作正常。但是,当我使用 Mavan
将应用程序构建为 WAR
文件,并将其部署到外部 Tomcat
服务器上时,我突然收到错误消息,在部署应用程序时找不到我的映射器:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springmapper': Unsatisfied dependency expressed through field 'thingTypes'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'otherthingtypes': Invocation of init method failed; nested exception is
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.myapplicaton.dao.MybatisMapper.selectByExample
我感觉,当 运行 作为 WAR
时,应用程序不再扫描这些 MyBatis
mappers
作为 mappers
。我一定需要更改一些配置,但我不知道是什么。
====================
当前配置(当 运行 在 Eclipse IDE
作为 Spring Boot
应用时工作)..
(1) com.myapplicaton.dao 中的所有 MyBatis
java
映射器都有 @Mapper
注释:
package com.myapplicaton.dao;
@Mapper
public interface MybatisMapper {
List<Thing> selectByExample(ThingExample example);
//...
}
(2) 在应用程序启动中添加了一个MapperScan
..
@SpringBootApplication
@MapperScan({"com.myapplicaton.dao","com.myapplicaton.other.mappers"})
public class MyApplication extends SpringBootServletInitializer
{
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
(3) 在 @Autowired
类:
MyBatis
映射器
@Configuration
public class SomeConfig {
@Bean(name = "springmapper")
@Scope(value = "singleton")
public SpringMapper getSpringMapper() {
return new SpringMapper();
}
@Bean(name = "thingTypes")
@Scope(value = "singleton")
public ThingTypes thingTypes() {
return new ThingTypes();
}
}
package com.myapplicaton.other.mappers;
public class SpringMapper {
@Autowired
ThingTypes thingTypes;
//...
}
package com.myapplicaton.beans;
public class ThingTypes {
@Autowired
MybatisMapper mybatisMapper;
//...
}
====================
问题
为什么部署为 WAR
时这不起作用?
好吧,折腾了一天,总算有了答案。
首先,我不得不删除两个 'problem' bean 我有一个使用 @PostConstruct
的 init()
方法,它使用 @Autowired
Mybatis
映射器。我不知道为什么它不喜欢那样,但我不在乎,因为在我删除它们之后,应用程序作为 WAR
.. 主要是
它曾经做过这样的事情:
package com.myapplicaton.beans;
public class ThingTypes {
@Autowired
MybatisMapper mybatisMapper;
@PostConstruct
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
private void init() {
mybatisMapper.selectByExample(example);
}
}
我遇到的第二个问题是应用程序无法在我的dao
中找到MyBatis
mapper
.xml
文件] 包部署为 WAR
。像往常一样,在 Eclipse 上 运行 时很好。
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.myapplicaton.dao.MybatisMapper.selectByExample
我检查了 WAR
文件,你瞧,Maven
跳过了 .xml
文件,只有 Java
class
文件住在那个文件夹里。我无法弄清楚如何使用 spring-boot-maven-plugin
取消跳过 .xml
文件,而且我不打算尝试找出另一个插件,所以我能够如下击败系统:
我在应用程序的 src/main/resources/
文件夹中创建了一组文件夹,其文件夹结构与 .java
interface
mappers
所在的文件夹结构完全相同。所以,我创建了 src/main/resources/com/myapplication/dao
。我把所有 .xml
文件都放在那里。
检查新 WAR 后,.xml
文件与 java
class
文件放在同一目录中,并且 MyBatis
正常工作.
欢呼。