evoToK3Cloud/.trae/documents/Add SQL Server Query Integration.md
2025-12-30 21:00:03 +08:00

36 lines
1.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## 配置示例(建议)
- 新增独立数据源前缀:`spring.datasource.mssql`(与主库区分)
- Spring Boot 标准键名使用短横线:`driver-class-name`
- SQL Server URL 推荐:`jdbc:sqlserver://主机:端口;databaseName=库名;encrypt=false;trustServerCertificate=true`
- 不要使用 MySQL 专用参数(如 `rewriteBatchedStatements`
示例:
```
spring:
datasource:
mssql:
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
url: jdbc:sqlserver://localhost:1433;databaseName=tempdb;encrypt=false;trustServerCertificate=true
username: SA
password: root
hikari:
maximum-pool-size: 10
connection-timeout: 30000
read-only: true
```
说明:
- `DatabaseName` 可写为 `databaseName`(微软驱动两者等效,统一小写更常见)
- `SelectMethod=cursor` 是旧驱动参数,通常不需要;保留仅在游标大结果集特殊场景
- `rewriteBatchedStatements` 是 MySQL 参数,不适用于 SQL Server需去掉
- 生产环境请勿使用 `SA/root`,改为只读账号并限制权限
## 集成方式
- 若项目已用动态数据源:注册名为 `mssql` 的数据源,查询处使用 `@DS("mssql")`
- 若项目未用动态数据源:为上述 `spring.datasource.mssql` 创建 `DataSource` + `JdbcTemplate` BeanService 里使用模板执行参数化 SQL
## 验证步骤
- 启动后编写一条简单查询TOP 10验证连通
- 确认连接池参数和超时生效;异常时输出清晰日志
确认后我按你选择的“动态数据源/JdbcTemplate”方式补全配置、注册 Bean并提供示例查询接口与 Service。