3,springboot整合mybatisplus 多数据源

1.pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.1.RELEASE
         
    
    com.taotao
    datasources_ds
    0.0.1-SNAPSHOT
    datasources_ds
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            log4j
            log4j
            1.2.17
        
        
            org.projectlombok
            lombok
        

        
            org.springframework.boot
            spring-boot-starter-aop
        
        
            org.apache.commons
            commons-lang3
        

        
            com.baomidou
            mybatis-plus-boot-starter
            3.3.2
        
        
            mysql
            mysql-connector-java
        
        
            com.alibaba
            fastjson
            1.2.62
        

        
            com.baomidou
            dynamic-datasource-spring-boot-starter
            3.1.0
        


        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    




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;

/**
 * 

* *

* * @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;

/**
 * 

* *

* * @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;

/**
 * 

* Mapper 接口 *

* * @author jobob * @since 2020-06-17 */ @Mapper public interface OrderMapper extends BaseMapper { }
package com.taotao.datasources_ds.stock.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.taotao.datasources_ds.stock.entity.Stock;

/**
 * 

* Mapper 接口 *

* * @author jobob * @since 2020-06-17 */ public interface StockMapper extends BaseMapper { }

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;

/**
 * 

* 服务实现类 *

* * @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;

/**
 * 

* 服务实现类 *

* * @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); } }

你可能感兴趣的:(3,springboot整合mybatisplus 多数据源)