spring boot 配置访问其他模块包中的mapper和xml

maven项目结构如下,这里只是简单测试demo,使用的springboot版本为2.1.3.RELEASE

1、comm模块主要是一些mybatis的mapper接口和对应的xml文件,以及数据库表实体映射类,目录结构

2、bat模块包含调用comm模块的mapper接口的service服务,以及测试controller,引入comm模块作为依赖,目录结构

涉及点:

@MapperScan配置mapper路径

@SpringBootApplication
// 路径设置为comm模块的mapper路径
@MapperScan(basePackages = {"com.kinson.comm.mapper"})
public class BatApplication {
    
    public static void main(String[] args) {
         SpringApplication.run(BatApplication.class, args);
    }
    
}

application.yml配置文件

# MyBatis
    mybatis:
    # 数据库映射实体类包路径 type-aliases-package: com.kinson.comm.domain mapper-locations: classpath:/mybatis/*.xml

启动项目

访问测试url

可以看到访问comm的接口成功

 

Springboot集成mybaits源码参照

Github spring boot 配置访问其他模块包中的mapper和xml源码参照

转载于:https://www.cnblogs.com/kingsonfu/p/11590219.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个快速开发的框架,提供了很多方便的工具,其中包括Mybatis框架。在使用Mybatis框架时,需要将Mapper.xml文件加载到应用程序中。 在Spring Boot中,可以使用@MapperScan注解将Mapper接口的包路径配置到扫描器中,然后使用@Mapper注解将Mapper接口注入到IOC容器中。但是,这种方式只会自动扫描Mapper接口,对于Mapper.xml文件需要手动进行配置。 首先,需要在application.properties或application.yml文件中配置Mapper.xml文件的路径,例如: mybatis.mapper-locations=classpath:mapper/*.xml 然后,需要创建SqlSessionFactoryBean并为其设置DataSource和Mapper.xml文件的位置: @Configuration public class MybatisConfig { @Value("${mybatis.mapper-locations}") private String mapperLocations; @Autowired private DataSource dataSource; @Bean public SqlSessionFactoryBean sqlSessionFactory() throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources(mapperLocations)); return sessionFactory; } } 在创建完SqlSessionFactoryBean后,需要在Mapper接口中使用@Mapper注解,并通过@Autowired注入SqlSessionFactoryBean,例如: @Mapper public interface UserMapper { @Autowired SqlSessionFactory sqlSessionFactory; // ... } 最后,在需要使用Mapper接口的地方,可以直接将Mapper接口注入到Spring Bean中,并使用@Autowired注入即可,例如: @RestController public class UserController { @Autowired private UserMapper userMapper; // ... } 通过这种方式,就可以在Spring Boot中方便地加载Mapper.xml文件,并使用Mybatis框架完成相应的数据操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值