pom
<!-- mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<!-- Dynamic DataSource -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
</dependency>
application.yaml
我这边直接使用了hikari连接池,当然也可以配成druid
# spring配置
spring:
datasource:
hikari:
connection-timeout: 30000 # 等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException, 默认:30秒
minimum-idle: 5 # 最小连接数
maximum-pool-size: 100 # 最大连接数
auto-commit: true # 事务自动提交
idle-timeout: 600000 # 连接超时的最大时长(毫秒),超时则被释放(retired),默认:10分钟
pool-name: DNA_Hikari_Poll # 连接池名字
max-lifetime: 1800000 # 连接的生命时长(毫秒),超时而且没被使用则被释放(retired),默认:30分钟 1800000ms
connection-test-query: SELECT 1 # 连接测试语句
dynamic:
primary: master #设置默认的数据源或者数据源组,默认值即为master
strict: true #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
datasource:
# 主库数据源
master:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://xx:xx/llm-mp?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&allowMultiQueries=true&rewriteBatchedStatements=true
username: xx
password: xx
# 从库数据源
slave:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://xx:xx/enetapp_test?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&allowMultiQueries=true&rewriteBatchedStatements=true
username: xx
password: xx
java代码
以两个数据库的两张表为例
Entity
两张表的映射DTO如下
来自于llm-mp
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("sys_user_role")
public class Role {
@TableField(value = "user_id")
private Integer userId;
@TableField(value = "role_id")
private Integer roleId;
}
来自于enetapp_test
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("llm_mp_train_data")
public class TrainData {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@TableField("source")
private Float source;
@TableField("completion")
private String completion;
}
Mapper
两个mapper分别对应两个数据库的两张表
public interface RoleMapper extends BaseMapper<Role> {
}
public interface TrainDataMapper extends BaseMapper<TrainData> {
}
Service
接口定义
public interface TrainDataService extends IService<TrainData> {
}
public interface RoleService extends IService<Role> {
}
实现类定义
@Service
@DS("master")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
}
/**
* 多数据源使用时请不要使用lambdaQuery()
* 不然会导致数据源切换失效
*/
@Service
@DS("slave")
public class TrainDataServiceImpl extends ServiceImpl<TrainDataMapper, TrainData> implements TrainDataService {
}
使用
ambdaQuery()
会导致从库切换失败,主库可以随便用lambdaQuery()
,因为yaml里面配置了默认数据源primary: master
@Service
@RequiredArgsConstructor
public class TestService {
private final TrainDataService trainDataService;
private final RoleService roleService;
public PageVo<TrainData> login() {
QueryWrapper<TrainData> wrapper = new QueryWrapper<>();
wrapper.last("limit 0,100");
List<TrainData> list = trainDataService.list(wrapper);
return PageUtil.getPageVo(new PageVo<>(),list);
}
public String login2() {
QueryWrapper<TrainData> wrapper = new QueryWrapper<>();
wrapper.eq(true,"id",3);
return trainDataService.getOne(wrapper).getCompletion();
}
}
评论区