Spring Boot එක්ක MySQL Database සම්බන්ධ කරමු - Ultimate SC Guide

ආයුබෝවන්, Spring Boot යාළුවනේ! Databases සම්බන්ධ කරමුද?
ආයුබෝවන් කට්ටිය! කොහොමද ඉතින්? මම හිතනවා හැමෝම හොදින් ඇති කියලා. අද අපි කතා කරන්න යන්නේ Spring Boot development වලදී ගොඩක්ම වැදගත්, අත්යවශ්ය දෙයක් ගැන. ඒ තමයි අපේ applications වලට databases සම්බන්ධ කරගන්නේ කොහොමද කියන එක.
Software development වලදී Database එකක් නැතුව වැඩක් කරන්න බෑනේ. අපේ application එකේ data ටික save කරන්න, read කරන්න, update කරන්න, delete කරන්න වගේ ගොඩක් දේවල් වලට අපිට Database එකක් අනිවාර්යයි. මේක හරියට application එකක හදවත වගේ. Data නැත්නම් application එකට ජීවයක් නෑනේ. ඉතින්, අද අපි මේ article එකෙන් බලමු, Spring Boot project එකකට MySQL database එකක් හරියටම configure කරගෙන, ඒකත් එක්ක වැඩ කරන්නේ කොහොමද කියලා. ඔයාලට මේක MySQL වලට වගේම PostgreSQL, H2 වගේ ඕනම relational database එකකට apply කරගන්න පුලුවන්.
ඉතින්, අපි පටන් ගමුද?
Database එකක් කියන්නේ මොකක්ද?
ගොඩක් වෙලාවට beginners ලට තියෙන ලොකුම ප්රශ්නයක් තමයි "Database එකක් කියන්නේ මොකක්ද? ඒක අපිට කොහොමද උදව් වෙන්නේ?" කියන එක. සරලවම කිව්වොත්, Database එකක් කියන්නේ අපේ දත්ත organize කරලා, පහසුවෙන් access කරන්න පුළුවන් විදියට store කරලා තියන තැනක්. හිතන්නකෝ ඔයාලගේ පොත් ගොඩක් තියෙනවා නම්, ඒවා අහක දාලා තියනවට වඩා පොත් රාක්කයක විෂය අනුව පිළිවෙලට තියනවා නම් කොච්චර පහසුද? Database එකත් හරියට ඒ වගේ තමයි.
Software applications වලදී, පරිශීලකයන්ගේ තොරතුරු, නිෂ්පාදන විස්තර, ගනුදෙනු වාර්තා වගේ විවිධ වර්ගයේ දත්ත තියෙනවා. මේවා තාවකාලිකව memory එකේ තියාගන්න පුළුවන් වුණත්, application එක close කරාම ඒ දත්ත නැතිවෙලා යනවා. ඒකට තමයි අපිට data ටික ස්ථිරව තියාගන්න database එකක් ඕනේ වෙන්නේ. Relational databases (RDBMS) තමයි ගොඩක් වෙලාවට මේ සඳහා භාවිතා කරන්නේ. MySQL, PostgreSQL, SQL Server, Oracle වගේ ඒවා ඒ වර්ගයට අයිතියි. මේවා Table-based structure එකක් භාවිතා කරන අතර, දත්ත Rows සහ Columns වලට බෙදා organize කරනවා. මේ Tables අතර Relationships හදන්නත් පුළුවන්. ඒ වගේම NoSQL databases (MongoDB, Cassandra) වගේ ඒවාත් දැන් ගොඩක් ජනප්රියයි, ඒවයින් unstructured data store කරන්න පුළුවන්.
අපේ Spring Boot applications වලට මේ database සම්බන්ධ කරගන්න එක තමයි අද අපේ ප්රධාන මාතෘකාව. Spring Boot මේ වැඩේ ගොඩක් පහසු කරලා තියනවා. අපි ඒ ගැන ඊළඟ section එකේ කතා කරමු.
Spring Boot වලට Database සම්බන්ධ කරන්නේ ඇයි?
දැන් ඔයාලට database එකක් කියන්නේ මොකක්ද කියලා පොඩි අදහසක් ඇතිනේ. එහෙනම් අපි බලමු අපේ Spring Boot application එකට database එකක් සම්බන්ධ කරගන්න එකේ තියෙන වාසි මොනවද කියලා.
- Data Persistence: application එක වහලා ආයෙත් open කරත් අපේ data ටික නැති වෙන්නේ නැතුව තියෙනවා. මේක තමයි ප්රධානම වාසිය.
- Organized Data: Tables, columns, relationships හරහා දත්ත පිළිවෙලකට තියාගන්න පුළුවන්. මේකෙන් data retrieval සහ management පහසු වෙනවා.
- Scalability: applications විශාල වන විට සහ වැඩි දත්ත ප්රමාණයක් සමඟ වැඩ කරන විට database එකක් අත්යවශ්යයි. database servers වලට scalability solutions තියෙනවා.
- Concurrency Control: එකම data එකට එකම වෙලාවේ usersලා ගොඩක් access කරනකොට data inconsistencies ඇති වෙන්න පුළුවන්. database එකක් මඟින් මේවා පාලනය කරන්න පුළුවන්.
- Security: databases වලට data security සඳහා ගොඩක් features තියෙනවා. Access control, encryption වගේ දේවල් ඒ අතරින් ප්රධානයි.
Spring Boot කියන්නේ Java based applications ඉක්මනින් develop කරන්න උදව් වෙන framework එකක්. මේකේ තියෙන auto-configuration feature එක නිසා database සම්බන්ධ කරන එක කලින්ට වඩා ගොඩක් පහසු වෙලා තියනවා. අපිට අවශ්ය database driver එක classpath එකට add කරලා, application.properties
(නැත්නම් application.yml
) file එකේ database connection details ටික දුන්නාම, Spring Boot ඉතිරි ටික තනියම බලාගන්නවා. මේකෙන් developers ලට infrastructure configuration වලට වැය වෙන කාලය අඩු කරගෙන, business logic development එකට වැඩි අවධානයක් යොමු කරන්න පුළුවන්.
අපි MySQL Database එකක් සෙට් කරමු!
Spring Boot project එකට database එකක් සම්බන්ධ කරන්න කලින්, අපිට database server එකක් ඕනේ. මේකට MySQL server එකක් install කරගන්න පුළුවන්. ඔයාලට පහසුවෙන් XAMPP, WAMP වගේ bundles භාවිතා කරන්න පුළුවන්, නැත්නම් Docker එකෙන් MySQL image එකක් run කරගන්නත් පුළුවන්.
මම හිතනවා ඔයාලා ළඟ දැනටමත් MySQL server එකක් install කරලා ඇති කියලා. එහෙම නැත්නම්, පහලින් තියෙන link වලින් එකක් පාවිච්චි කරලා ඉක්මනටම set කරගන්න පුළුවන්:
- XAMPP Download
- WAMP Server Download
- Docker Desktop Download (Docker users:
docker run --name mysql-db -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=myapp_db -p 3306:3306 -d mysql:8
)
දැන් අපි Database එකක් හදමු. MySQL Workbench, DBeaver, නැත්නම් command line එක වුණත් භාවිතා කරන්න පුළුවන්. මම මෙතනදි MySQL command line client එකෙන් කරන විදිය පෙන්නන්නම්. මුලින්ම MySQL server එකට connect වෙන්න:
mysql -u root -p
password එක ඉල්ලන තැනට ඔයාලගේ root password එක දෙන්න. (XAMPP/WAMP වල default එක හිස් වෙන්න පුළුවන්).
දැන් අපි අලුත් database එකක් හදමු. අපේ application එකට myapp_db
කියලා database එකක් හදමු.
CREATE DATABASE myapp_db;
හදපු database එක select කරගමු.
USE myapp_db;
දැන් ඒ database එක ඇතුලේ අපි test කරන්න table එකක් හදමු. users
කියලා table එකක් හදමු, ඒකේ id
, name
, email
කියන columns තුන තියෙයි.
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE
);
හරි, දැන් අපේ database එකයි, ඒක ඇතුලේ test table එකයි ready. ඊළඟට අපි බලමු මේක Spring Boot project එකකට සම්බන්ධ කරගන්නේ කොහොමද කියලා.
Spring Boot Project එකට MySQL සම්බන්ධ කරමු!
මේක තමයි මේ post එකේ ප්රධානම කොටස. අපි මේක පියවරෙන් පියවර බලමු.
පියවර 1: Project එකට අවශ්ය Dependencies එකතු කරමු
මුලින්ම, ඔයාලගේ Spring Boot project එකේ pom.xml
(Maven නම්) නැත්නම් build.gradle
(Gradle නම්) file එකට අවශ්ය dependency ටික එකතු කරන්න ඕනේ. අපිට මෙතනදි ප්රධාන වශයෙන්ම spring-boot-starter-data-jpa
සහ mysql-connector-java
කියන dependencies දෙක ඕනේ වෙනවා.
pom.xml
(Maven):
<dependencies>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Starter Data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- MySQL Connector/J -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version> <!-- ඔබේ MySQL version එකට අනුව වෙනස් විය හැක -->
</dependency>
<!-- Spring Boot Starter Test (Optional) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
build.gradle
(Gradle):
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'mysql:mysql-connector-java:8.0.33' // ඔබේ MySQL version එකට අනුව වෙනස් විය හැක
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
මතක තියාගන්න, mysql-connector-java
එකේ version එක ඔබේ MySQL server version එකට ගැලපෙන එකක් තෝරාගන්න.
පියවර 2: application.properties Configuration එක සකස් කරමු
දැන් අපි අපේ database connection details ටික src/main/resources/application.properties
file එකට එකතු කරමු. මේක තමයි Spring Boot application එකේ configurations තියෙන තැන.
# Database Connection Settings
spring.datasource.url=jdbc:mysql://localhost:3306/myapp_db?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=your_mysql_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# JPA/Hibernate Settings
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect # ඔබේ MySQL version එකට අනුව වෙනස් විය හැක
මේවා ගැන පොඩ්ඩක් පැහැදිලි කරමු:
spring.datasource.url
: මේකෙන් කියවෙන්නේ database එක තියෙන තැන.localhost:3306
කියන්නේ ඔයාගේ පරිගණකයේ 3306 port එකේ MySQL server එක run වෙනවා කියන එක.myapp_db
කියන්නේ අපි කලින් හදපු database එකේ නම.useSSL=false
සහserverTimezone=UTC
වගේ parameters දාන්නේ connection issues avoid කරන්න.spring.datasource.username
: MySQL database එකට connect වෙන්න පාවිච්චි කරන username එක. (default root)spring.datasource.password
: MySQL database එකට connect වෙන්න පාවිච්චි කරන password එක. (ඔබේ password එක මෙතනට දාන්න)spring.datasource.driver-class-name
: අපි භාවිතා කරන database driver එක. MySQL වලටcom.mysql.cj.jdbc.Driver
.spring.jpa.hibernate.ddl-auto
: මේක Hibernate (JPA implement කරන එකක්) property එකක්.none
: කිසිම DDL (Data Definition Language) operation එකක් කරන්නේ නෑ.update
: Entity class වලට අනුව database schema එක update කරනවා. (development වලදි හොඳයි)create
: සෑම වතාවකම application එක run කරනකොට අලුත් schema එකක් හදනවා. කලින් තිබ්බ data ටික නැති වෙනවා.create-drop
: Application එක run කරනකොට schema එක හදලා, application එක shutdown කරනකොට drop කරනවා. (testing වලට හොඳයි)validate
: Database schema එක සහ Entity classes වලට අනුකූලද කියලා validate කරනවා. අනුකූල නැත්නම් error එකක් දෙනවා. (production වලට හොඳයි)
spring.jpa.show-sql=true
: Application එකෙන් execute කරන SQL queries console එකේ පෙන්නනවා. debugging වලට වැදගත්.spring.jpa.properties.hibernate.dialect
: අපි භාවිතා කරන database එකට ගැලපෙන Hibernate dialect එක තෝරාගන්න. MySQL 8 වලටorg.hibernate.dialect.MySQL8Dialect
.
පියවර 3: Simple Entity එකක් සහ Repository එකක් හදමු
දැන් අපි Spring Data JPA භාවිතා කරලා අපේ database එකට data save කරන්න සහ retrieve කරන්න අවශ්ය classes ටික හදමු. මුලින්ම User
කියන Entity class එක හදමු.
src/main/java/com/example/myapp/model/User.java
:
package com.example.myapp.model;
import jakarta.persistence.Entity; // For Spring Boot 3+
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Column;
import jakarta.persistence.Table;
// For Spring Boot 2.x, use:
// import javax.persistence.Entity;
// import javax.persistence.GeneratedValue;
// import javax.persistence.GenerationType;
// import javax.persistence.Id;
// import javax.persistence.Column;
// import javax.persistence.Table;
@Entity
@Table(name = "users") // Database table name
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false, unique = true)
private String email;
// Default constructor
public User() {
}
// Constructor with fields
public User(String name, String email) {
this.name = name;
this.email = email;
}
// Getters and Setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
'}';
}
}
@Entity
annotation එකෙන් කියවෙන්නේ මේ class එක database table එකකට map වෙනවා කියන එක. @Table(name="users")
එකෙන් database එකේ table එකේ නම define කරනවා. @Id
කියන්නේ primary key එකට. @GeneratedValue(strategy = GenerationType.IDENTITY)
එකෙන් කියවෙන්නේ database එකට auto-increment වෙන්න දෙන්න කියලා. @Column
එකෙන් column properties define කරන්න පුළුවන් (nullable, unique වගේ).
දැන් අපි මේ User
entity එකට data save කරන්න, find කරන්න අවශ්ය Repository interface එක හදමු.
src/main/java/com/example/myapp/repository/UserRepository.java
:
package com.example.myapp.repository;
import com.example.myapp.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
// Spring Data JPA automatically provides methods like save(), findById(), findAll(), delete()
// You can also define custom query methods here, e.g., findByEmail(String email);
}
JpaRepository
extend කරනකොට අපි Entity type එකයි (User
) ඒකේ Primary Key type එකයි (Long
) දෙන්න ඕනේ. JpaRepository
එකෙන් අපිට data operations (save, find, delete) සඳහා methods ගොඩක් free එකේ ලැබෙනවා. මේකෙන් අපේ time එක ගොඩක් save වෙනවා.
පියවර 4: Connection එක Test කරමු
දැන් අපි බලමු මේ connection එක හරියට වැඩ කරනවද කියලා. අපේ Spring Boot application එක run වෙනකොටම data ටිකක් database එකට save කරලා, ආයෙත් read කරලා බලමු.
ඔයාලගේ main application class එකට CommandLineRunner
implement කරලා මේ වගේ code එකක් ලියන්න පුළුවන්:
src/main/java/com/example/myapp/MyappApplication.java
(or a separate class implementing CommandLineRunner):
package com.example.myapp;
import com.example.myapp.model.User;
import com.example.myapp.repository.UserRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class MyappApplication {
public static void main(String[] args) {
SpringApplication.run(MyappApplication.class, args);
}
@Bean
public CommandLineRunner demo(UserRepository userRepository) {
return (args) -> {
// Save a few users
userRepository.save(new User("Kamal Perera", "[email protected]"));
userRepository.save(new User("Nimal Siripala", "[email protected]"));
userRepository.save(new User("Sunil Shantha", "[email protected]"));
// Fetch all users
System.out.println("Users found with findAll():");
System.out.println("------------------------------------");
for (User user : userRepository.findAll()) {
System.out.println(user);
}
System.out.println("");
// Fetch an individual user by ID
userRepository.findById(1L)
.ifPresent(user -> {
System.out.println("User found with findById(1L):");
System.out.println("------------------------------------");
System.out.println(user);
System.out.println("");
});
// Update a user (Example: change email of user with ID 1)
userRepository.findById(1L)
.ifPresent(user -> {
user.setEmail("[email protected]");
userRepository.save(user);
System.out.println("Updated user 1: " + user);
});
};
}
}
දැන් ඔයාලගේ Spring Boot application එක run කරන්න. Console එකේ Spring Boot start වෙනකොට Database connection එක establish වෙන විදියත්, අපි save කරපු users ලා save වෙන විදියත්, retrieve වෙන විදියත් දැකගන්න පුළුවන් වෙයි. ඒ වගේම spring.jpa.show-sql=true
දාපු නිසා execute වෙන SQL queries ටිකත් පෙන්වයි. database එක check කරොත් data ටික add වෙලා තියෙනවා දැකගන්න පුළුවන්.
පොඩි පොඩි Tips
Database configuration කියන්නේ ටිකක් ගැඹුරු මාතෘකාවක්. මෙතනදි අපි මූලික දේවල් කතා කරාට, තව ගොඩක් දේවල් තියෙනවා.
- Connection Pooling: Spring Boot විසින් default එකෙන් HikariCP කියන connection pool එක භාවිතා කරනවා. මේක හරිම efficient connection pool එකක්. applications වල performance වැඩි කරන්න මේකෙන් ලොකු උදව්වක් ලැබෙනවා.
- Environment Variables: Production environments වලදී database username, password වගේ sensitive data කෙලින්ම
application.properties
file එකේ දානවා වෙනුවට environment variables විදියට දාන එක ගොඩක් ආරක්ෂාකාරීයි. Spring Boot වලට environment variables වලින් properties load කරන්න පුළුවන්. - Multiple Data Sources: සමහර complex applications වලදී databases කිහිපයක් එක්ක වැඩ කරන්න වෙනවා. Spring Boot වලදී Multiple Data Sources configure කරන්නත් පුළුවන්.
- Error Handling: Database connection errors, SQL exceptions වගේ දේවල් නිවැරදිව handle කරන්න පුලුවන් විදියට code කරන්න පුරුදු වෙන්න.
- Production Configuration: Development වලදී
ddl-auto=update
හොඳ වුණත්, production වලදීvalidate
නැත්නම්none
වගේ දේවල් භාවිතා කරන එක වඩා ආරක්ෂාකාරීයි. Database migrations (Flyway, Liquibase) වගේ tools production environments වලදී schema changes manage කරන්න භාවිතා කරනවා.
ඉතින්, Database Configuration දැන් හරිද?
ඉතින් යාළුවනේ, මේ ලිපියෙන් අපි Spring Boot application එකකට MySQL database එකක් සම්බන්ධ කරගන්නේ කොහොමද කියන එක පියවරෙන් පියවර කතා කරා. Database එකක් කියන්නේ මොකක්ද, Spring Boot වලට Database එකක් සම්බන්ධ කරගන්න එකේ වැදගත්කම, MySQL database එකක් setup කරන විදිය, සහ Spring Boot project එකට අවශ්ය dependencies, configurations, Entity, Repository classes හදලා connection එක test කරන විදිය ඔයාලා දැන් දන්නවා.
මේක පොඩි පටන් ගැන්මක් විතරයි. තව ගොඩක් දේවල් මේ ගැන ඉගෙන ගන්න තියෙනවා. Distributed transactions, caching, advanced JPA queries වගේ දේවල් ගැනත් ඔයාලට ඉගෙන ගන්න පුළුවන්.
මම හිතනවා මේ article එක ඔයාලට ගොඩක් ප්රයෝජනවත් වෙන්න ඇති කියලා. ඔයාලත් මේක try කරලා බලන්න. මොනවා හරි ප්රශ්න තියෙනවා නම්, comment section එකේ අහන්න! ඔයාලගේ අදහස්, යෝජනා අපිට ගොඩක් වටිනවා. මේ වගේම තවත් වැදගත් ලිපියකින් හමුවෙමු.
ආයෙත් මේ වගේ අලුත් දෙයක් අරගෙන එනකල්, ඔයාලට සුබ දවසක්!
#SoftwareDevelopment #SpringBoot #MySQL #Database #JPA #Backend #Programming #SriLanka #Tutorial #Tech