您的位置:首页 > 编程语言 > Java开发

【第八章】 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切面。

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: