PlusMagi's Blog By Pitt Phunsanit Backend Spring Boot: connection SQL Server

Spring Boot: connection SQL Server

ในฐานะ Senior Architect การเชื่อมต่อแอปพลิเคชัน Java Spring Boot เข้ากับฐานข้อมูลเชิงสัมพันธ์อย่าง SQL Server เป็นงานพื้นฐานที่สำคัญที่สุดงานหนึ่ง การใช้ Spring Data JPA จะช่วยลดความซับซ้อนในการจัดการ Persistence Layer ทำให้เราสามารถโฟกัสกับการเขียน Business Logic ได้อย่างเต็มที่ บทความนี้จะนำเสนอขั้นตอนที่สมบูรณ์ในการตั้งค่าให้ Spring Boot สามารถสื่อสารกับ SQL Server ได้อย่างมีประสิทธิภาพ

1. การเพิ่ม Dependency ที่จำเป็นใน pom.xml

ขั้นตอนแรกคือการเพิ่มไลบรารี (Dependency) ที่จำเป็นในไฟล์ pom.xml ของโปรเจกต์ Maven คุณต้องใช้ Spring Data JPA เพื่อจัดการ ORM และใช้ JDBC Driver เฉพาะสำหรับ Microsoft SQL Server เพื่อให้ Spring Boot ทราบวิธีการเชื่อมต่อที่ถูกต้อง

โปรดเพิ่มโค้ดต่อไปนี้ในส่วน <dependencies>:

XML

<dependencies> 
    <!-- Spring Data JPA Starter --> 
    <dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-data-jpa</artifactId> 
    </dependency> 
    <!-- Microsoft SQL Server JDBC Driver --> 
    <dependency> 
        <groupId>com.microsoft.sqlserver</groupId> 
        <artifactId>mssql-jdbc</artifactId> 
        <scope>runtime</scope> 
    </dependency>
</dependencies>

2. การตั้งค่า Connection Properties ใน application.properties

หลังจากเพิ่ม Driver แล้ว ขั้นตอนถัดมาคือการกำหนด Connection String และรายละเอียดการเข้าถึงฐานข้อมูลในไฟล์ application.properties (หรือ application.yml) เนื่องจาก SQL Server เวอร์ชันใหม่ๆ มักมีการบังคับใช้ SSL/TLS เพื่อความปลอดภัย เราจึงต้องระบุค่า encrypt=true อย่างชัดเจน

ตัวอย่างการตั้งค่าที่สมบูรณ์:

SQL

# ------------------------------------------------------
# 1. Connection URL (เปลี่ยนชื่อ service และ port ให้ตรงกับ Docker Compose)
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=MyDatabase;encrypt=true;trustServerCertificate=true

# 2. Credentials
spring.datasource.username=sa
spring.datasource.password=YourStrongPassword

# 3. JPA/Hibernate Dialect (สำคัญมากสำหรับการสร้าง Schema)
spring.jpa.database-platform=org.hibernate.dialect.SQLServerDialect

# 4. Optional: แสดง SQL ที่ Hibernate สร้างขึ้นเพื่อ Debug
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

3. การจัดการการเชื่อมต่อในสภาพแวดล้อมจริง (Best Practice)

ในระดับสถาปัตยกรรม (Architectural Level) เราไม่ควร Hardcode ค่า Connection String ลงในไฟล์ application.properties โดยตรง ควรใช้ Environment Variables หรือ Spring Profiles เพื่อให้แอปพลิเคชันมีความยืดหยุ่นสูงเมื่อต้อง Deploy ในสภาพแวดล้อมที่แตกต่างกัน (เช่น Dev, Test, Prod)

ตัวอย่างการใช้ Profile:

Plain Text

# ค่าเริ่มต้นสำหรับ Development Environment
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=devdb;encrypt=true;trustServerCertificate=true

# ค่าสำหรับ Production Environment (ควรมาจาก Environment Variable)
spring.datasource.url=jdbc:sqlserver://${DB_HOST}:${DB_PORT};databaseName=prod_db;encrypt=true;trustServerCertificate=true
spring.profiles.active=prod

บทสรุป: การเชื่อมต่อที่สมบูรณ์แบบ

การเชื่อมต่อ Spring Boot กับ SQL Server ให้ประสบความสำเร็จนั้น ไม่ได้ขึ้นอยู่กับการใส่โค้ดเพียงอย่างเดียว แต่ต้องอาศัยความเข้าใจในองค์ประกอบทั้งหมด ตั้งแต่การเลือก Dependency ที่ถูกต้อง การระบุ Connection String ที่ปลอดภัย (โดยเฉพาะเรื่อง SSL/TLS) และการใช้ Best Practices ในการจัดการ Environment Variables ด้วยการใช้ Spring Profiles เมื่อทำตามขั้นตอนข้างต้นอย่างครบถ้วน แอปพลิเคชันของคุณก็จะสามารถจัดการ CRUD Operations กับ SQL Server ได้อย่างเสถียรและมีประสิทธิภาพในระดับ Production Architecture แล้ว

Exit mobile version