在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 {
请问是什么原因造成的,如果解决?
报错是因为你在MybatisController中注入了UserXMLMapper,但是UserXMLMapper没有纳入spring管理。
@Mapper是由mybatis-spring-boot-starter这个包扫描注册的,没注入成功我猜可能是这个包扫描的时间在profile加载之前。
如何解决的话,可以试下@MapperScan指定不同包。