spring mybatis项目中,@Mapper和mybatis.mapper-locations的区别
在springboot mybatis项目中使用不同的方式来配置映射器位置,会导致不同的行为。
@Mapper vs mybatis.mapper-locations
功能差异
@Mapper和mybatis.mapper-locations的主要功能差异如下:
为什么两者都可以工作?
在你的案例中,@Mapper和application.properties中的mybatis.mapper-locations都可以工作,原因如下:
atis.mapper-locations时,mybatis会扫描配置文件中指定的路径(classpath:mapper/*.xml),并加载相应的xml映射器文件。由于xml文件指定了相同的映射器接口,因此也会生成代理对象。什么时候使用@Mapper?
建议在如下情况下使用@Mapper:
什么时候使用mybatis.mapper-locations?
建议在以下情况下使用mybatis.mapper-locations: