本文来说下多数据源框架baomidou
程序准备
测试使用的数据库
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for order
-- ----------------------------
DROP TABLE IF EXISTS `order`;
CREATE TABLE `order` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '订单表主键',
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '商品名称',
`price` decimal(10, 2) NULL DEFAULT NULL COMMENT '商品价格',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 26 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of order
-- ----------------------------
INSERT INTO `order` VALUES (19, '苹果笔记本', 4000.00);
INSERT INTO `order` VALUES (20, '手机', 2000.00);
INSERT INTO `order` VALUES (21, '风扇', 1000.00);
SET FOREIGN_KEY_CHECKS = 1;
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for product
-- ----------------------------
DROP TABLE IF EXISTS `product`;
CREATE TABLE `product` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '商品表主键',
`address` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '地址',
`remark` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '备注',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 15 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of product
-- ----------------------------
INSERT INTO `product` VALUES (1, '北京', 'aa');
INSERT INTO `product` VALUES (2, '上海', 'bb');
INSERT INTO `product` VALUES (3, '杭州', 'cc');
INSERT INTO `product` VALUES (4, '杭州', 'dd');
INSERT INTO `product` VALUES (5, '上海', 'ee');
INSERT INTO `product` VALUES (6, '北京', 'ff');
INSERT INTO `product` VALUES (7, '北京', 'gg');
SET FOREIGN_KEY_CHECKS = 1;
在pom.xml文件中添加相关依赖
<!--baomidou-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
新增application.yml配置文件
server:
port: 8088
spring:
datasource:
dynamic:
primary: master #设置默认的数据源或者数据源组,默认值即为master
strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
datasource:
master:
url: jdbc:mysql://localhost:3306/order?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
slave:
url: jdbc:mysql://localhost:3306/product?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
程序实例
程序搭建
package cn.wideth.controller;
import cn.wideth.entity.Order;
import cn.wideth.entity.Product;
import cn.wideth.response.ResponseBean;
import cn.wideth.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Collections;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/api/order")
@Api(tags = "订单的基本信息")
public class OrderController {
@Autowired
private IOrderService iOrderService;
@GetMapping("/getOrderInfo")
@ApiOperation(value = "订单的基本信息")
public ResponseBean<?> getOrderInfo() {
List<Order> data = iOrderService.getOrderInfo();
if (data == null || data.size() == 0) {
return new ResponseBean<>(400, "fail", Collections.emptyList());
}
return new ResponseBean<>(200, "success", data);
}
@GetMapping("/getProductInfo")
@ApiOperation(value = "产品的基本信息")
public ResponseBean<?> getProductInfo() {
List<Product> data = iOrderService.getProductInfo();
if (data == null || data.size() == 0) {
return new ResponseBean<>(400, "fail", Collections.emptyList());
}
return new ResponseBean<>(200, "success", data);
}
}
OrderServiceImpl实现类
package cn.wideth.service.impl;
import cn.wideth.entity.Order;
import cn.wideth.entity.Product;
import cn.wideth.mapper.OrderMapper;
import cn.wideth.service.IOrderService;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class OrderServiceImpl implements IOrderService {
@Autowired
private OrderMapper orderMapper;
@DS("master")
@Override
public List<Order> getOrderInfo() {
List<Order> data = orderMapper.getOrderInfo();
return data;
}
@DS("slave")
@Override
public List<Product> getProductInfo() {
List<Product> data = orderMapper.getProductInfo();
return data;
}
}
mapper实现
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.wideth.mapper.OrderMapper">
<select id="getOrderInfo" resultType="cn.wideth.entity.Order">
SELECT *
FROM order_info
</select>
<select id="getProductInfo" resultType="cn.wideth.entity.Product">
SELECT *
FROM product
</select>
</mapper>
测试结果
测试结果