Mysql多数据源Springboot+mybatis+druid+dynamic-datasource

一、添加POM引用

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>MySQL-Test</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>DynamicDatabase</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!--mybatis驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
<!--druid数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.16</version>
</dependency>
<!-- 动态数据源 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
</dependency>
<!--lombok实体工具-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--swagger2-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>

</project>

二、配置连接属性

server:
port: 8088
spring:
application:
name: DynamicDatabase
datasource:
type: com.alibaba.druid.pool.DruidDataSource
dynamic:
primary: master
datasource:
# 主数据库
master:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/aamm1?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT
username: root
password: 123456
# 从数据库
slave:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/aamm2?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT
username: root
password: 123456
druid:
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
spring.datasource.filters: stat,wall,log4j
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
mybatis-plus:
type-aliases-package: com.lubansoft.model

三、代码

1、Application代码

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
public class Application {
public static void main(String[] args)
{
SpringApplication.run(Application.class);
}
}

2、Mapper代码

import com.lubansoft.model.Poi;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface PoiMapper {
@Insert({"<script>" ,
"insert into poi (id,poiid, title, address,lon, lat, city, category_name, checkin_num, photo_num) values ",
" <foreach collection = ‘pois‘ item = ‘poi‘ index = ‘index‘ open=\"(\" close=\")\" separator = ‘,‘> ",
" (#{poi.id},#{poi.poiid},#{poi.title},#{poi.address},#{poi.lon},#{poi.lat},#{poi.city},#{poi.category_name},#{poi.checkin_num},#{poi.photo_num}) ",
" </foreach> ",
"</script>"})
void insert(@Param("pois") List<Poi> poi);

@Select({"<script>" ,
"select * from poi ",
"</script>"})
List<Poi> getAll();
}

3、controller代码

import com.lubansoft.model.Poi;
import com.lubansoft.service.Poi1Service;
import com.lubansoft.service.PoiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RequestMapping("/poi")
@RestController
@Api("PoiController")
public class PoiController {
@Autowired
private PoiService poiService;
@Autowired
private Poi1Service poi1Service;

@ApiOperation("insert")
@PostMapping("/insert")
public String insert(@RequestParam("poi") Poi poi){
poiService.insert(Arrays.asList(poi));
return "插入成功";
}

@ApiOperation("获取全部主数据库")
@GetMapping("/getAll")
public List<Poi> getAll(){
return poiService.getAll();
}

@ApiOperation("获取全部从数据库")
@GetMapping("/getAll1")
public List<Poi> getAll1(){
return poi1Service.getAll();
}
}

4、model

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

@ApiModel("Poi")
@Getter
@Setter
public class Poi {
@ApiModelProperty("id")
private String id;
@ApiModelProperty("poiid")
private String poiid;
@ApiModelProperty("title")
private String title;
@ApiModelProperty("address")
private String address;
@ApiModelProperty("lon")
private String lon;
@ApiModelProperty("lat")
private String lat;
@ApiModelProperty("city")
private String city;
@ApiModelProperty("category_name")
private String category_name;
@ApiModelProperty("checkin_num")
private String checkin_num;
@ApiModelProperty("photo_num")
private String photo_num;
}

5、services

import com.baomidou.dynamic.datasource.annotation.DS;
import com.lubansoft.mapper.PoiMapper;
import com.lubansoft.model.Poi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@DS("master")
@Service
public class PoiService {
    @Autowired
    PoiMapper poiMapper;

    public Boolean insert(List<Poi> pois){
        poiMapper.insert(pois);
        return Boolean.TRUE;
    }

    public List<Poi> getAll(){
        return poiMapper.getAll();
    }
}
import com.baomidou.dynamic.datasource.annotation.DS;
import com.lubansoft.mapper.PoiMapper;
import com.lubansoft.model.Poi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@DS("slave")
@Service
public class Poi1Service {
    @Autowired
    PoiMapper poiMapper;

    public Boolean insert(List<Poi> pois){
        poiMapper.insert(pois);
        return Boolean.TRUE;
    }

    public List<Poi> getAll(){
        return poiMapper.getAll();
    }
}

 

6、swagger

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
public class SwaggerConfig {
    Boolean swaggerEnabled=true;
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_12).apiInfo(apiInfo())
                // 是否开启
                .enable(swaggerEnabled).select()
                // 扫描的路径包
                .apis(RequestHandlerSelectors.basePackage("com.test.controller"))
                // 指定路径处理PathSelectors.any()代表所有的路径
                .paths(PathSelectors.any()).build().pathMapping("/");
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SpringBoot-Swagger3集成和使用-demo示例")
                .description("springboot | swagger")
                // 作者信息
                .contact(new Contact("name", "个人主页url", "email"))
                .version("1.0.0")
                .build();
    }
}

Mysql多数据源Springboot+mybatis+druid+dynamic-datasource

上一篇:mysql设置了utf8mb4,为什么还有utf8mb4_general_ci和utf8mb4_0900_ai_ci?


下一篇:Innodb中有哪些锁?