Service层代码
@DataSource(value = MultiDataSourceConst.SLAVE)
public List<User> getUserList() {
List<User> userList = userRepository.findAll();
return userList;
}
application.yml配置
server:
port: 3389
servlet:
# 项目contextPath
context-path: /
tomcat:
# tomcat的URI编码
uri-encoding: UTF-8
# tomcat最大线程数,默认为200
max-threads: 800
# Tomcat启动初始化的线程数,默认值25
min-spare-threads: 30
max-http-header-size: 8192
spring:
# 模板引擎
thymeleaf:
mode: HTML
encoding: utf-8
# 禁用缓存
cache: false
# 资源信息
messages:
# 国际化资源文件路径
basename: i18n/messages
jackson:
time-zone: GMT+8
date-format: yyyy-MM-dd HH:mm:ss
# 文件上传
servlet:
multipart:
max-file-size: 30MB
max-request-size: 30MB
# 服务模块
devtools:
restart:
# 热部署开关
enabled: true
http:
encoding:
force: true
charset: utf-8
enabled: true
multi:
datasource:
master:
url: jdbc:mysql://localhost:3306/courtdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
slave:
url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
jpa:
database: MYSQL
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
show-sql: true
hibernate:
ddl-auto: update
pom.xml依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
注:默认是master主数据库
工具类下载 https://download.youkuaiyun.com/download/samurai77/11868976
本文介绍了一个使用Spring Boot框架的项目中如何配置多数据源并实现在Service层的业务逻辑。通过配置application.yml文件,详细展示了主从数据库的连接参数设置,包括URL、驱动类名、用户名和密码等。同时,提供了Service层代码示例,演示了如何通过注解指定使用从数据库查询用户列表。
5784

被折叠的 条评论
为什么被折叠?



