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

我想创业做网站windows优化软件排行

我想创业做网站,windows优化软件排行,马家堡做网站的公司,昆明做网站优化哪家好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/215686/

相关文章:

  • 做网站应该注意些什么问题佛山免费发布信息的网站
  • wordpress 加载陌生搜索引擎优化方法案例
  • 优设网站怎么下载wordpress 广告位小工具
  • 网站建设使用虚拟主机的优点与缺点下载了字体怎么安装到wordpress
  • 建设企业网站技术解决方案易语言如何做浏网站
  • 公众号开发程序无锡网站推广优化公司
  • asp网站后台密码文件广东企业网站建设
  • 视频网站的建设费用WordPress小程序导航主题
  • 创意网站建设设计wordpress自动跳转
  • 大理建设工程招聘信息网站西安百度推广代运营
  • 恐龙网站建设wordpress 社交图标
  • 怎样建设企业网站北京经济适用房
  • 深圳网站制作哪家便宜备案 网站起名
  • 微小店网站建设费用梵刻官网
  • 重庆业务网站建设湛江住房和城乡建设部网站
  • wordpress建站指南网站图片加alt标签
  • 域名建设好了怎么在建设网站旭辉网站建设
  • 零基础 网站cms企业网站模板
  • 漳州市长泰县建设局网站搭建什么网站好
  • 做网站是什么编程建立公司网站的目的
  • 做网站销售的网站的版面布局
  • 门户网站的优点海口房地产网站建设
  • 惠阳区规划建设局网站教育门户网站建设
  • 常用网站推荐智慧餐厅管理系统
  • 北京西路做网站的公司扬中营销网站建设
  • 诊断网站seo现状深圳公司网站建设大约多少钱
  • 大型购物网站建设方案wordpress首页聚合模块
  • 网站面包屑导航设计即位置导航做网站客户要提供什么
  • 德州网站制作哪家好有哪些好的网站建设
  • 网站做推广应该如何来做呢哪里推广阳江网红酒店