哪些行業(yè)網(wǎng)站推廣做的多網(wǎng)絡(luò)營(yíng)銷(xiāo)軟文范例300字
Spring Boot應(yīng)用中集成與使用多數(shù)據(jù)源
1. 前言
通過(guò)定義和使用多個(gè)數(shù)據(jù)源,能在Spring Boot應(yīng)用中實(shí)現(xiàn)更復(fù)雜的數(shù)據(jù)管理場(chǎng)景,比如讀寫(xiě)分離、數(shù)據(jù)冗余等。
2. 準(zhǔn)備工作
- 環(huán)境準(zhǔn)備:確保已經(jīng)準(zhǔn)備好Spring Boot的開(kāi)發(fā)環(huán)境。
- 數(shù)據(jù)庫(kù)準(zhǔn)備:在本地或云服務(wù)上創(chuàng)建兩個(gè)數(shù)據(jù)庫(kù),如下文所示。
3. 創(chuàng)建Spring Boot項(xiàng)目
- 使用Spring Initializr創(chuàng)建項(xiàng)目:https://start.spring.io/。
- 在
pom.xml
中添加必要的依賴(lài),包括JPA、Spring Boot Parent、數(shù)據(jù)庫(kù)驅(qū)動(dòng)等。
4. 配置多數(shù)據(jù)源
在application.yml
或application.properties
中配置:
# application.yml
spring:datasource:primary:url: jdbc:mysql://localhost:3306/db1username: userpassword: passworddriver-class-name: com.mysql.jdbc.Driverhikari:connection-timeout: 30000maximum-pool-size: 20secondary:url: jdbc:mysql://localhost:3306/db2username: userpassword: passworddriver-class-name: com.mysql.jdbc.Driverhikari:connection-timeout: 30000maximum-pool-size: 20
5. 創(chuàng)建實(shí)體類(lèi)及Repository
Entity Class - User (For Primary Database):
package com.example.multidatasource.entity;import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;@Entity
@Table(name = "users")
public class User {@Idprivate Long id;private String name;private String email;// getter, setter, constructors
}
Entity Class - Product (For Secondary Database):
package com.example.multidatasource.entity;import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;@Entity
@Table(name = "products")
public class Product {@Idprivate Long id;private String name;private int price;// getter, setter, constructors
}
Repository (Primary):
package com.example.multidatasource.repository;import com.example.multidatasource.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
Repository (Secondary):
package com.example.multidatasource.repository;import com.example.multidatasource.entity.Product;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
}
6. 服務(wù)層配置與使用多數(shù)據(jù)源
package com.example.multidatasource.service;import com.example.multidatasource.entity.Product;
import com.example.multidatasource.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class ProductService {private final ProductRepository productRepository;@Autowiredpublic ProductService(ProductRepository productRepository) {this.productRepository = productRepository;}public Product createProduct(String name, int price) {Product product = new Product();product.setName(name);product.setPrice(price);return productRepository.save(product);}
}
服務(wù)層同樣應(yīng)當(dāng)遵循具體數(shù)據(jù)源的配置,確保通過(guò)合適的數(shù)據(jù)源進(jìn)行持久化操作。
7. 事務(wù)與多數(shù)據(jù)源管理
針對(duì)跨數(shù)據(jù)源的事務(wù)操作,需要在@Service中配置@Transactional
注解:
@Service
public class MultiDataSourceTransactionService {private final UserRepository userRepository;private final ProductRepository productRepository;@Autowiredpublic MultiDataSourceTransactionService(UserRepository userRepository, ProductRepository productRepository) {this.userRepository = userRepository;this.productRepository = productRepository;}// So that it's only using the primary dataSource@Transactional(propagation = Propagation.REQUIRED)public void performCreateUserAndProduct() {userRepository.save(new User("John Doe", "john@example.com"));productRepository.save(new Product("Widget", 1000));}
}
通過(guò)這種方式,可以確保同一個(gè)請(qǐng)求中的所有操作,要么全部成功,要么全部回滾。
8. 配置及測(cè)試
確保所有的Bean和配置類(lèi)被正確注解,測(cè)試應(yīng)用是否能夠啟動(dòng),數(shù)據(jù)源是否能夠正確讀寫(xiě)數(shù)據(jù)。