1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.taotao</groupId>
<artifactId>datasources_ds</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>datasources_ds</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2,yml配置:
spring:
datasource:
dynamic:
primary: itmayidu-order #设置默认的数据源或者数据源组,默认值即为master
strict: false #设置严格模式,默认false不启动. 启动后在未匹配到指定数据源时候回抛出异常,不启动会使用默认数据源.
datasource:
##会员的数据库
itmayidu-order:
url: jdbc:mysql://localhost:3306/itmayidu-order?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
###订单数据库
itmayidu-stock:
url: jdbc:mysql://localhost:3306/itmayidu-stock?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
logging:
level:
###打印mybatis日志
com.taotao.datasources_ds : debug
3,启动类
package com.taotao.datasources_ds;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan(basePackages = {"com.taotao.datasources_ds.stock.mapper","com.taotao.datasources_ds.order.mapper"})
@SpringBootApplication
public class DatasourcesDsApplication {
public static void main(String[] args) {
SpringApplication.run(DatasourcesDsApplication.class, args);
}
}
4,实体类
package com.taotao.datasources_ds.order.entity;
import java.time.LocalDateTime;
import java.io.Serializable;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* <p>
*
* </p>
*
* @author jobob
* @since 2020-06-17
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class Order implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Long id;
/**
* 订单名称
*/
private String name;
/**
* 下单时间
*/
private LocalDateTime orderCreatetime;
/**
* 订单状态 0 已经未支付 1已经支付 2已退单
*/
private Integer orderState;
/**
* 订单价格
*/
private Double orderMoney;
/**
* 商品ID
*/
private Integer commodityId;
}
package com.taotao.datasources_ds.stock.entity;
import java.io.Serializable;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* <p>
*
* </p>
*
* @author jobob
* @since 2020-06-17
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class Stock implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Long id;
/**
* 商品ID
*/
private Integer commodityId;
/**
* 库存余额
*/
private Integer stock;
}
5,mapper
package com.taotao.datasources_ds.order.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.taotao.datasources_ds.order.entity.Order;
import org.apache.ibatis.annotations.Mapper;
/**
* <p>
* Mapper 接口
* </p>
*
* @author jobob
* @since 2020-06-17
*/
@Mapper
public interface OrderMapper extends BaseMapper<Order> {
}
package com.taotao.datasources_ds.stock.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.taotao.datasources_ds.stock.entity.Stock;
/**
* <p>
* Mapper 接口
* </p>
*
* @author jobob
* @since 2020-06-17
*/
public interface StockMapper extends BaseMapper<Stock> {
}
6,service
package com.taotao.datasources_ds.order.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taotao.datasources_ds.order.entity.Order;
import com.taotao.datasources_ds.order.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* <p>
* 服务实现类
* </p>
*
* @author jobob
* @since 2020-06-17
*/
@RestController
@DS("itmayidu-order")
public class OrderServiceImpl {
@Autowired
private OrderMapper orderMapper;
@GetMapping("/findByorderId")
public Order findByorderId(Long id){
Order order= orderMapper.selectById(id);
return order;
}
}
package com.taotao.datasources_ds.stock.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taotao.datasources_ds.stock.entity.Stock;
import com.taotao.datasources_ds.stock.mapper.StockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* <p>
* 服务实现类
* </p>
*
* @author jobob
* @since 2020-06-17
*/
@RestController
@DS(value = "itmayidu-stock")
public class StockServiceImpl {
@Autowired
private StockMapper stockMapper;
@GetMapping("/findByuserId")
public Stock findByuserId(Long userid){
return stockMapper.selectById(userid);
}
}