美文网首页
在Spring Boot + Mybatis 中,使用@Repo

在Spring Boot + Mybatis 中,使用@Repo

作者: 清_晨_ | 来源:发表于2018-11-25 00:19 被阅读249次

在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,但是不影响正常运行,如下所示:

11.png

相关文章

网友评论

      本文标题:在Spring Boot + Mybatis 中,使用@Repo

      本文链接:https://www.haomeiwen.com/subject/mpjuqqtx.html