天天看點

在Spring Boot + Mybatis 中,使用@Repository失效

在Spring Boot + Mybatis 中,使用@Repository 無法将mapper接口添加到Bean

位址: https://www.jianshu.com/p/bafc8ffdf11e

在springboot 中,給mapper的接口上加上@Repository,無法生成相應的bean,進而無法@Autowired,這是因為spring掃描注解時,自動過濾掉了接口和抽象類,這種情況下可以在啟動的類前加 上@MapperScan(“×××.×××.mapper”,進而使mapper可以自動注入,但是idea還會提示bean無法找到,但是不會影響運作。如下所示:

啟動類上:

@SpringBootApplication
@MapperScan(
        basePackages = {"com.test.teinterface"},
        annotationClass = Mapper.class
)
public class InterfaceApplication extends SpringBootServletInitializer {
    public InterfaceApplication() {
    }

    public static void main(String[] args) {
        (new SpringApplicationBuilder(new Object[]{InterfaceApplication.class})).web(true).run(args);
    }

    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        SpringApplicationBuilder rst = builder.sources(new Class[]{InterfaceApplication.class});
        return rst;
    }
}
           

mybatis mapper接口上:

@Mapper
@Repository
public interface TPersonBaseInfoMapper extends tk.mybatis.mapper.common.Mapper<TPersonBaseInfo>  {
    List<Map<String, String>> query(String var1);

    Integer getCountByCid(String var1);

    Integer update(TPersonBaseInfo var1);
}
           

當不加@Repository注解時,在service中,idea會提示bean not found