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

c2c模式的网站常州网站建设报价

c2c模式的网站,常州网站建设报价,中国建设银行信用卡官方网站,虚拟空间网站回收池有什么作用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.yayakq.cn/news/157647/

相关文章:

  • 做个游戏网站多少钱建设网站的要求吗
  • 抚顺网站网站建设软文发布推广平台
  • 襄阳网站seo公司陕西省国家示范校建设专题网站
  • 兼容移动端网站开发企业推广服务
  • 网站建设哪家更专业浏览器网站大全
  • 建设网站范文网站推广策略包括哪些内容
  • 学会网站建设电商网站建设的现实意义是什么
  • 4399小游戏网页在线玩西安seo网站公司
  • 沈阳公司网站制作园林建设网站
  • 响应式网站模板百度云省水利工程建设信息网站
  • 数码产品网站开发背景大发 wordpress ifanr
  • 企业门户网站主要功能wordpress主题代码哪里
  • 网站建设合作协议申请现在公众号做电影网站的发展
  • 株洲新站建设黑龙江省建设厅官网查询
  • 贵阳网站建设蜜蜂wordpress 用户字段
  • 捡个将军做男友啥网站能看北京推广服务
  • thinkphp 企业网站企业还做网站吗
  • 网站建设哪里学通栏 网站模板
  • php做网站安全性企业官网免费
  • 南山商城网站建设多少钱建设企业银行客户号在哪里看
  • 国内网站设计制作网站空间和流量
  • 企业官方网站推广河南建设安全监督网站
  • 廊坊建设局网站6国外做游戏评测的视频网站
  • 南宁专业网站制作设计重庆付费网站推广
  • 淘宝网站建设基本流程wordpress只显示摘要
  • h5网站程序收录批量查询工具
  • 建设专题网站在线之家官网
  • 大庆建设网站首页软件工程师工资
  • 两个人能用的一个公司做网站吗网页制作专业人才培养规格
  • 广州找人做网站房产网站建设方案