【第八章】 springboot + mybatis + 多数据源
2017-07-13 13:35
399 查看
在实际开发中,我们一个项目可能会用到多个数据库,通常一个数据库对应一个数据源。
代码结构:
package com.xxx.firstboot.web; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.xxx.firstboot.domain.Shop; import com.xxx.firstboot.service.ShopService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @RestController @RequestMapping("/shop") @Api("shopController相关api") public class ShopController { @Autowired private ShopService service; @ApiOperation("获取shop信息,测试多数据源") @RequestMapping(value = "/getShop", method = RequestMethod.GET) public Shop getShop(@RequestParam("id") int id) { return service.getShop(id); } }View Code 补:其实DatabaseContextHolder和DynamicDataSource完全可以合为一个类
参考:
https://www.geek-share.com/detail/2612022200.html
遗留:在实际开发中,一个dao类只会用到一个数据源,如果dao类中的方法很多的话,每一个方法前边都要添加一个设置数据源的一句话,代码有些冗余,可以使用AOP切面。
相关文章推荐
- 第八章 springboot + mybatis + 多数据源
- 第八章 springboot + mybatis + 多数据源
- springboot + mybatis + 多数据源 (AOP实现)
- 第九章 springboot + mybatis + 多数据源 (AOP实现)
- Spring Boot 整合 Mybatis 实现 Druid 多数据源详解
- springboot+mybatis配置多数据源
- SpringBoot多数据源的配置(SpringBoot+MyBatis)
- spring boot(七):springboot+mybatis多数据源最简解决方案
- springboot(七):springboot+mybatis多数据源最简解决方案
- Spring Boot 集成Mybatis实现主从(多数据源)分离方案示例
- Spring-boot Mybatis多数据源事务配置
- spring-boot+mybatis多数据源最简解决方案
- spring boot 配置MyBatis,支持多个数据源和分页插件
- 运用springboot+mybatis 实现动态多数据源
- springboot + mybatis + 多数据源
- spring boot(七):springboot+mybatis多数据源最简解决方案
- 第八章 springboot + mybatis + 多数据源
- 第九章 springboot + mybatis + 多数据源 (AOP实现)
- springboot+mybatis多数据源最简解决方案