在日常开发中,若遇到多个数据源的需求,怎么办呢?
通过springboot集成mybatis实现多数据源配置,简单尝试一下。
- 新建springboot工程,引入web、mysql、mybatis依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

本文介绍了如何在SpringBoot项目中集成MyBatis以实现多数据源配置。通过配置文件设置两个数据源,并创建对应的配置类进行数据源初始化。同时,为每个数据源配置了MyBatis的SqlSessionFactory和SqlSessionTemplate,以及对应的Mapper接口。在测试中展示了如何在不同数据源间切换进行数据操作。
最低0.47元/天 解锁文章
2992





