当前位置: 首页 > news >正文

秦皇岛网站建设哪里有百度电脑版网址

秦皇岛网站建设哪里有,百度电脑版网址,北京网站制作多少钱,组织建设六个方面内容Spring Boot应用中集成与使用多数据源 1. 前言 通过定义和使用多个数据源,能在Spring Boot应用中实现更复杂的数据管理场景,比如读写分离、数据冗余等。 2. 准备工作 环境准备:确保已经准备好Spring Boot的开发环境。数据库准备&#xff…

Spring Boot应用中集成与使用多数据源

1. 前言

通过定义和使用多个数据源,能在Spring Boot应用中实现更复杂的数据管理场景,比如读写分离、数据冗余等。

2. 准备工作
  • 环境准备:确保已经准备好Spring Boot的开发环境。
  • 数据库准备:在本地或云服务上创建两个数据库,如下文所示。

3. 创建Spring Boot项目
  1. 使用Spring Initializr创建项目:https://start.spring.io/。
  2. pom.xml中添加必要的依赖,包括JPA、Spring Boot Parent、数据库驱动等。

4. 配置多数据源

application.ymlapplication.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. 创建实体类及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. 服务层配置与使用多数据源
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);}
}

服务层同样应当遵循具体数据源的配置,确保通过合适的数据源进行持久化操作。

7. 事务与多数据源管理

针对跨数据源的事务操作,需要在@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));}
}

通过这种方式,可以确保同一个请求中的所有操作,要么全部成功,要么全部回滚。

8. 配置及测试

确保所有的Bean和配置类被正确注解,测试应用是否能够启动,数据源是否能够正确读写数据。

http://www.hengruixuexiao.com/news/28478.html

相关文章:

  • 安徽工程建设监管平台seo自动优化工具
  • 网站建设有哪些软件有哪些方面互联网营销顾问
  • 网站进度条源代码juqery-uiseo优化技术招聘
  • 在线java深圳百度首页优化
  • 做美工的网站营销培训机构哪家最专业
  • 济南网站建设 选聚搜网络seo网站关键词优化
  • 2w网站2w网站建设建设seo外包服务专家
  • 青岛企业网站设计制作八种营销模式
  • 可以做免费推广的网站百度seo技术
  • 重庆网站怎么设置模板建站
  • 杨浦做网站公司100个关键词
  • 河北网站建设团队百度seo收录
  • 武汉 外贸网站建设湖口网站建设
  • 创造与魔法官网站_一起做喜欢的事seo短期培训班
  • 网站关键词调整 收录网址搜索引擎
  • 用struts2框架做的网站推广信息发布平台
  • 南宁建站模板展示厦门网站建设公司
  • 信息类网站百度域名
  • 池州网站建设关键词推广优化排名如何
  • outlook企业邮箱谷歌seo网站运营
  • 高端网站建设公司推荐百度关键词投放
  • 做电商搜素材网站都是什么好口碑关键词优化地址
  • wordpress 载入时间杭州网络推广网络优化
  • 龙岗开发公司网站建设最新国际新闻10条
  • win2008 iis 新建网站2023年8月疫情严重吗
  • 做家电网站好今日要闻10条
  • 网站建设现状和前景交换友情链接
  • 深圳建设工程质量协会网站百度灰色关键词排名代做
  • 各行各业网站建设优化内容
  • 国内做视频课程的网站有哪些深圳百度总部