springboot配置多个环境是,mybatis找不到mapper

在springboot中配置多个环境时,mybatis报错说

Description:

Field userMapper in com.lovekun.jpa.controller.mybatis.MybatisController required a bean of type 'com.lovekun.jpa.dao.mybatis.xml.UserXMLMapper' that could not be found.


Action:

Consider defining a bean of type 'com.lovekun.jpa.dao.mybatis.xml.UserXMLMapper' in your configuration.

Mapper上只是加了Profile的注解

@Mapper
@Profile("mybatis")
public interface UserXMLMapper {

    User queryByUsername(@Param("username") String username);
    
}

对应的配置文件application.properties,只是配置了多环境的内容。

spring.profiles.active=mybatis,mybatis-config-javaconfig,mybatis-mapping-xml

去掉@Profile("mybatis") 就没有问题,但是其他包含有类似注解的bean都被成功加载了

对Mapper的扫描已经配置在

@Configuration
@MapperScan(basePackages="com.lovekun.jpa.dao.mybatis.xml")
@Profile("mybatis-config-javaconfig")
public class MybatisConfig {

请问是什么原因造成的,如果解决?

阅读 11.7k
3 个回答

报错是因为你在MybatisController中注入了UserXMLMapper,但是UserXMLMapper没有纳入spring管理。
@Mapper是由mybatis-spring-boot-starter这个包扫描注册的,没注入成功我猜可能是这个包扫描的时间在profile加载之前。
如何解决的话,可以试下@MapperScan指定不同包。

我觉得你把相关类的完整代码还有包名等。

以及配置类的内容和相关代码,以及xml文件的内容都放上来,可能才好给我们做一个判断。

从提示中真看不出什么呢

UserXMLMapper 没有托管给spring容器 检查一下包扫描路径是否正确 或者尝试去掉@Profile 这个注解

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题