datai/datai-system/src/main/resources/mapper/system/SysDatasourceConfigMapper.xml
Kris b7163d555b feat: 实现动态数据源延迟加载功能 (2026-01-21-001)
## 功能概述
- 实现动态数据源的延迟加载机制,支持应用启动时只加载主库,从库按需动态加载和切换
- 从库配置存储在主库中,支持运行时灵活切换从库库名
- 使用现有的 @DataSource 注解进行数据源切换,业务代码无需修改

## 新增功能
- 动态数据源管理接口 IDynamicDataSourceManager
- 动态数据源服务接口 IDynamicDataSourceService
- 动态数据源服务实现 DynamicDataSourceServiceImpl
- 数据源管理控制器 DatasourceController
- 数据源配置表 sys_datasource_config

## 新增文档
- 需求文档: docs/requirements/2026-01-21-001-动态数据源延迟加载.md
- 设计文档: docs/design/2026-01-21-001-动态数据源延迟加载设计.md
- 决策记录: docs/decisions/2026-01-21-001-ADR-动态数据源延迟加载.md
- SQL 脚本: docs/sql/2026-01-21-001-sys_datasource_config.sql
- 提示词: docs/prompts/2026-01-21-001-动态数据源延迟加载代码生成提示词.md
- 会话记录: docs/sessions/2026-01-21-001-session.md
- 变更日志: docs/changelog/2026-01-21-001-changelog.md
- 复盘文档: docs/retros/2026-01-21-001-retro.md
- API 文档: docs/api-docs/2026-01-21-001-api.md
- 根目录变更日志: CHANGELOG.md

## 修改功能
- 扩展 DataSourceManager 类,添加动态数据源管理方法
- 扩展 SysDatasourceConfigMapper 接口,添加 selectSysDatasourceConfigByDsName 方法
- 更新项目索引和 Authentication.canvas

## 修复问题
- 修复循环依赖问题:创建 IDynamicDataSourceManager 接口解决 datai-system 和 datai-framework 互相依赖
- 修复导入错误:删除 DynamicDataSourceServiceImpl 中未使用的导入
- 修复异常处理:将 setFilters() 调用移到 try-catch 块内

## API 接口
- POST /system/datasource/loadSlave - 加载从库数据源
- POST /system/datasource/switchSlave/{dbName} - 切换从库库名
- GET /system/datasource/getSlaveConfig - 获取从库配置
- POST /system/datasource/switch/{dsName} - 切换数据源
- DELETE /system/datasource/{dsName} - 移除数据源
2026-01-21 18:24:24 +08:00

131 lines
6.6 KiB
XML

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.datai.system.mapper.SysDatasourceConfigMapper">
<resultMap type="SysDatasourceConfig" id="SysDatasourceConfigResult">
<result property="id" column="id" />
<result property="dsName" column="ds_name" />
<result property="dbName" column="db_name" />
<result property="dbHost" column="db_host" />
<result property="dbPort" column="db_port" />
<result property="username" column="username" />
<result property="password" column="password" />
<result property="dbType" column="db_type" />
<result property="status" column="status" />
<result property="remark" column="remark" />
<result property="createBy" column="create_by" />
<result property="createTime" column="create_time" />
<result property="updateBy" column="update_by" />
<result property="updateTime" column="update_time" />
</resultMap>
<sql id="selectSysDatasourceConfigVo">
select
sdc.id,
sdc.ds_name,
sdc.db_name,
sdc.db_host,
sdc.db_port,
sdc.username,
sdc.password,
sdc.db_type,
sdc.status,
sdc.remark,
sdc.create_by,
sdc.create_time,
sdc.update_by,
sdc.update_time
from sys_datasource_config sdc
</sql>
<select id="selectSysDatasourceConfigList" parameterType="SysDatasourceConfig" resultMap="SysDatasourceConfigResult">
<include refid="selectSysDatasourceConfigVo"/>
<where>
<if test="dsName != null and dsName != ''"> and sdc.ds_name like concat('%', #{dsName}, '%')</if>
<if test="dbName != null and dbName != ''"> and sdc.db_name like concat('%', #{dbName}, '%')</if>
<if test="dbHost != null and dbHost != ''"> and sdc.db_host = #{dbHost}</if>
<if test="dbPort != null "> and sdc.db_port = #{dbPort}</if>
<if test="username != null and username != ''"> and sdc.username like concat('%', #{username}, '%')</if>
<if test="password != null and password != ''"> and sdc.password = #{password}</if>
<if test="dbType != null and dbType != ''"> and sdc.db_type = #{dbType}</if>
<if test="status != null and status != ''"> and sdc.status = #{status}</if>
</where>
</select>
<select id="selectSysDatasourceConfigById" parameterType="Long" resultMap="SysDatasourceConfigResult">
<include refid="selectSysDatasourceConfigVo"/>
where sdc.id = #{id}
</select>
<select id="selectSysDatasourceConfigByDsName" parameterType="String" resultMap="SysDatasourceConfigResult">
<include refid="selectSysDatasourceConfigVo"/>
where sdc.ds_name = #{dsName}
</select>
<insert id="insertSysDatasourceConfig" parameterType="SysDatasourceConfig" useGeneratedKeys="true" keyProperty="id">
insert into sys_datasource_config
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="dsName != null and dsName != ''">ds_name,</if>
<if test="dbName != null and dbName != ''">db_name,</if>
<if test="dbHost != null and dbHost != ''">db_host,</if>
<if test="dbPort != null">db_port,</if>
<if test="username != null and username != ''">username,</if>
<if test="password != null and password != ''">password,</if>
<if test="dbType != null and dbType != ''">db_type,</if>
<if test="status != null and status != ''">status,</if>
<if test="remark != null">remark,</if>
<if test="createBy != null and createBy != ''">create_by,</if>
<if test="createTime != null">create_time,</if>
<if test="updateBy != null and updateBy != ''">update_by,</if>
<if test="updateTime != null">update_time,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="dsName != null and dsName != ''">#{dsName},</if>
<if test="dbName != null and dbName != ''">#{dbName},</if>
<if test="dbHost != null and dbHost != ''">#{dbHost},</if>
<if test="dbPort != null">#{dbPort},</if>
<if test="username != null and username != ''">#{username},</if>
<if test="password != null and password != ''">#{password},</if>
<if test="dbType != null and dbType != ''">#{dbType},</if>
<if test="status != null and status != ''">#{status},</if>
<if test="remark != null">#{remark},</if>
<if test="createBy != null and createBy != ''">#{createBy},</if>
<if test="createTime != null">#{createTime},</if>
<if test="updateBy != null and updateBy != ''">#{updateBy},</if>
<if test="updateTime != null">#{updateTime},</if>
</trim>
</insert>
<update id="updateSysDatasourceConfig" parameterType="SysDatasourceConfig">
update sys_datasource_config
<trim prefix="SET" suffixOverrides=",">
<if test="dsName != null and dsName != ''">ds_name = #{dsName},</if>
<if test="dbName != null and dbName != ''">db_name = #{dbName},</if>
<if test="dbHost != null and dbHost != ''">db_host = #{dbHost},</if>
<if test="dbPort != null">db_port = #{dbPort},</if>
<if test="username != null and username != ''">username = #{username},</if>
<if test="password != null and password != ''">password = #{password},</if>
<if test="dbType != null and dbType != ''">db_type = #{dbType},</if>
<if test="status != null and status != ''">status = #{status},</if>
<if test="remark != null">remark = #{remark},</if>
<if test="createBy != null and createBy != ''">create_by = #{createBy},</if>
<if test="createTime != null">create_time = #{createTime},</if>
<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
</trim>
where sys_datasource_config.id = #{id}
</update>
<delete id="deleteSysDatasourceConfigById" parameterType="Long">
delete from sys_datasource_config where id = #{id}
</delete>
<delete id="deleteSysDatasourceConfigByIds" parameterType="String">
delete from sys_datasource_config where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
</mapper>