Browse Source

🎉 增加mybatis多数据源demo

test
smallchill 5 years ago
parent
commit
66e6426670
  1. 6
      blade-service/blade-demo/pom.xml
  2. 67
      blade-service/blade-demo/src/main/java/com/example/demo/controller/DynamicController.java
  3. 45
      blade-service/blade-demo/src/main/java/com/example/demo/service/IDynamicService.java
  4. 30
      blade-service/blade-demo/src/main/java/com/example/demo/service/impl/DynamicServiceImpl.java
  5. 24
      blade-service/blade-demo/src/main/resources/application-dev.yml
  6. 24
      blade-service/blade-demo/src/main/resources/application-prod.yml
  7. 24
      blade-service/blade-demo/src/main/resources/application-test.yml
  8. 10
      doc/nacos/blade-demo-dev.yaml

6
blade-service/blade-demo/pom.xml

@ -42,6 +42,11 @@
<artifactId>blade-demo-api</artifactId>
<version>${bladex.project.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>org.springblade</groupId>
<artifactId>blade-core-test</artifactId>
@ -69,6 +74,7 @@
</goals>
<configuration>
<tasks>
<!--suppress UnresolvedMavenProperty -->
<copy overwrite="true"
tofile="${session.executionRootDirectory}/target/${artifactId}.jar"
file="${project.build.directory}/${artifactId}.jar" />

67
blade-service/blade-demo/src/main/java/com/example/demo/controller/DynamicController.java

@ -0,0 +1,67 @@
/*
* Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the dreamlu.net developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: Chill 庄骞 (smallchill@163.com)
*/
package com.example.demo.controller;
import com.example.demo.entity.Notice;
import com.example.demo.service.IDynamicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* 多数据源
*
* @author Chill
*/
@RestController
@AllArgsConstructor
@RequestMapping("dynamic")
@Api(value = "多数据源接口", tags = "多数据源")
public class DynamicController {
private IDynamicService dynamicService;
/**
* master列表
*/
@GetMapping("/master-list")
@ApiOperationSupport(order = 1)
@ApiOperation(value = "master列表", notes = "master列表")
public R<List<Notice>> masterList() {
List<Notice> list = dynamicService.masterList();
return R.data(list);
}
/**
* slave列表
*/
@GetMapping("/slave-list")
@ApiOperationSupport(order = 1)
@ApiOperation(value = "slave列表", notes = "slave列表")
public R<List<Notice>> slaveList() {
List<Notice> list = dynamicService.slaveList();
return R.data(list);
}
}

45
blade-service/blade-demo/src/main/java/com/example/demo/service/IDynamicService.java

@ -0,0 +1,45 @@
/*
* Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the dreamlu.net developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: Chill 庄骞 (smallchill@163.com)
*/
package com.example.demo.service;
import com.example.demo.entity.Notice;
import org.springblade.core.mp.base.BaseService;
import java.util.List;
/**
* 服务类
*
* @author Chill
*/
public interface IDynamicService extends BaseService<Notice> {
/**
* master数据源的列表
*
* @return
*/
List<Notice> masterList();
/**
* slave数据源的列表
*
* @return
*/
List<Notice> slaveList();
}

30
blade-service/blade-demo/src/main/java/com/example/demo/service/impl/DynamicServiceImpl.java

@ -0,0 +1,30 @@
package com.example.demo.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.example.demo.entity.Notice;
import com.example.demo.mapper.NoticeMapper;
import com.example.demo.service.IDynamicService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* DynamicServiceImpl
*
* @author Chill
*/
@Service
public class DynamicServiceImpl extends BaseServiceImpl<NoticeMapper, Notice> implements IDynamicService {
@Override
public List<Notice> masterList() {
return this.list();
}
@Override
@DS("slave")
public List<Notice> slaveList() {
return this.list();
}
}

24
blade-service/blade-demo/src/main/resources/application-dev.yml

@ -3,8 +3,26 @@ server:
port: 8200
#数据源配置
#spring:
# datasource:
# url: ${blade.datasource.dev.url}
# username: ${blade.datasource.dev.username}
# password: ${blade.datasource.dev.password}
spring:
#排除DruidDataSourceAutoConfigure
autoconfigure:
exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
datasource:
url: ${blade.datasource.dev.url}
username: ${blade.datasource.dev.username}
password: ${blade.datasource.dev.password}
dynamic:
#设置默认的数据源或者数据源组,默认值即为master
primary: master
datasource:
master:
url: ${blade.datasource.demo.master.url}
username: ${blade.datasource.demo.master.username}
password: ${blade.datasource.demo.master.password}
slave:
url: ${blade.datasource.demo.slave.url}
username: ${blade.datasource.demo.slave.username}
password: ${blade.datasource.demo.slave.password}

24
blade-service/blade-demo/src/main/resources/application-prod.yml

@ -3,8 +3,26 @@ server:
port: 8200
#数据源配置
#spring:
# datasource:
# url: ${blade.datasource.prod.url}
# username: ${blade.datasource.prod.username}
# password: ${blade.datasource.prod.password}
spring:
#排除DruidDataSourceAutoConfigure
autoconfigure:
exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
datasource:
url: ${blade.datasource.prod.url}
username: ${blade.datasource.prod.username}
password: ${blade.datasource.prod.password}
dynamic:
#设置默认的数据源或者数据源组,默认值即为master
primary: master
datasource:
master:
url: ${blade.datasource.demo.master.url}
username: ${blade.datasource.demo.master.username}
password: ${blade.datasource.demo.master.password}
slave:
url: ${blade.datasource.demo.slave.url}
username: ${blade.datasource.demo.slave.username}
password: ${blade.datasource.demo.slave.password}

24
blade-service/blade-demo/src/main/resources/application-test.yml

@ -3,8 +3,26 @@ server:
port: 8200
#数据源配置
#spring:
# datasource:
# url: ${blade.datasource.test.url}
# username: ${blade.datasource.test.username}
# password: ${blade.datasource.test.password}
spring:
#排除DruidDataSourceAutoConfigure
autoconfigure:
exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
datasource:
url: ${blade.datasource.test.url}
username: ${blade.datasource.test.username}
password: ${blade.datasource.test.password}
dynamic:
#设置默认的数据源或者数据源组,默认值即为master
primary: master
datasource:
master:
url: ${blade.datasource.demo.master.url}
username: ${blade.datasource.demo.master.username}
password: ${blade.datasource.demo.master.password}
slave:
url: ${blade.datasource.demo.slave.url}
username: ${blade.datasource.demo.slave.username}
password: ${blade.datasource.demo.slave.password}

10
doc/nacos/blade-demo-dev.yaml

@ -7,3 +7,13 @@ blade:
secure:
skip-url:
- /demo/**
datasource:
demo:
master:
url: jdbc:mysql://localhost:3306/bladex?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&allowMultiQueries=true&serverTimezone=GMT%2B8
username: root
password: root
slave:
url: jdbc:mysql://localhost:3306/bladex_slave?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&allowMultiQueries=true&serverTimezone=GMT%2B8
username: root
password: root

Loading…
Cancel
Save