c语言sscanf函数的用法是什么
303
2023-02-08
spring boot动态切换数据源的实现
demo地址:https://github.com/songshijun1995/spring-boot-dynamic-demo
新建项目引入依赖
配置yml
server:
port: 8081
servlet:
context-path: /
spring:
application:
name: spring-boot-dynamic-demo
datasource:
dynamic:
primary: master #设置默认的数据源或者数据源组,默认值即为master
strict: false #设置严格模式,默认false不启动. 启动后在未匹配到指定数据源时候会抛出异常,不启动则使用默认数据源.
datasource:
master: #替换成自己的数据库连接
url: jdbc:mysql://127.0.0.1:3306/dynamic1?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowMultiQueries=true&rewriteBatchedStatements=true
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
slave_1:
url: jdbc:mysql://127.0.0.1:3306/dynamic2?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowMultiQueries=true&rewriteBatchedStatements=true
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
global-config:
db-config:
logic-delete-value: 1
logic-not-delete-value: 0
logic-delete-field: deleted
mapper-locations: classpath:/mapper/**.xml
logging:
level:
ROOT: INFO
com.dynamic.dynamicdemo: DEBUG
pattern:
file: '%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{50} - %msg%n'
file:
name: ${logging.file.path}/${spring.application.name}.log
path: /home/logs/${spring.application.name}
logback:
rollingpolicy:
max-file-size: 10MB
max-history: 30
mybatis-plus和swagger配置我就不多说了,demo里都有
如何使用
package com.dynamic.dynamicdemo.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dynamic.dynamicdemo.dto.PageRequest;
import com.dynamic.dynamicdemo.dto.UserInfoRequest;
import com.dynamic.dynamicdemo.entities.BaseEntity;
import com.dynamic.dynamicdemo.entities.UserInfo;
import com.dynamic.dynamicdemo.mapper.UserInfoMapper;
import com.dynamic.dynamicdemo.service.UserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class UserInfoServiceImpl extends ServiceImpl
@DS("master")
@Transactional(rollbackFor = Exception.class)
@Override
public String saveUser(UserInfoRequest request) {
UserInfo userInfo = new UserInfo();
BeanUtils.copyProperties(request, userInfo);
boolean save = save(userInfo);
if (!save) {
throw new RuntimeException("添加用户失败");
}
return "添加用户成功";
}
@DS("slave_1")
@Override
public IPage
return page(new Page<>(request.getPageNum(), request.getPageSize()),sFPEfbAFH Wrappers.
}
}
在方法上或者类上加@DS("master")注解,来指定数据源
更换为自己的数据库连接,以及建库,运行resources/sql目录下的sql文件
然后启动项目访问http://127.0.0.1:8081/doc.html进行测试
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~