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

手机网站怎么切图可信赖的广州做网站

手机网站怎么切图,可信赖的广州做网站,公司集团网站开发,网站为什么需要空间在现代微服务架构中#xff0c;通常需要与多个数据库交互的服务。这可能是由于各种原因#xff0c;例如遗留系统集成、不同类型的数据存储需求#xff0c;或者仅仅是为了优化性能。Spring Boot 具有灵活的配置和强大的数据访问库#xff0c;可以轻松配置多个数据库。在本综…在现代微服务架构中通常需要与多个数据库交互的服务。这可能是由于各种原因例如遗留系统集成、不同类型的数据存储需求或者仅仅是为了优化性能。Spring Boot 具有灵活的配置和强大的数据访问库可以轻松配置多个数据库。在本综合指南中我们将探讨如何在 Spring Boot 微服务中设置和管理多个数据库连接。 1. 简介 微服务通常需要与各种数据库交互。每个微服务可能需要不同类型的数据库例如用于事务数据的 SQL 数据库和用于非结构化数据的 NoSQL 数据库。Spring Boot 为配置和管理多个数据源提供了出色的支持使其成为现代微服务架构的理想选择。 2.为什么要使用多个数据库 您可能需要在微服务中使用多个数据库的原因如下 遗留系统集成与遗留系统的现有数据库集成。优化性能使用针对特定类型的数据例如关系型与非关系型优化的不同数据库。数据隔离出于安全、合规或组织原因分离数据。可扩展性在不同的数据库之间分配数据负载以提高性能。 3.设置 Spring Boot 项目 首先创建一个新的 Spring Boot 项目。您可以使用 Spring Initializr 或您喜欢的 IDE 来设置项目。 Maven 依赖项 在您的 中pom.xml包含 Spring Data JPA 和您将使用的数据库的依赖项例如内存中的 H2、PostgreSQL、MySQL 等。 dependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-jpa/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdcom.h2database/groupIdartifactIdh2/artifactIdscoperuntime/scope/dependencydependencygroupIdorg.postgresql/groupIdartifactIdpostgresql/artifactIdscoperuntime/scope/dependency /dependencies 4.配置多个数据源 在application.yml或application.properties文件中配置每个数据库的连接属性。 application.yml spring:datasource:primary:url: jdbc:h2:mem:primarydbdriver-class-name: org.h2.Driverusername: sapassword: passwordsecondary:url: jdbc:postgresql://localhost:5432/secondarydbdriver-class-name: org.postgresql.Driverusername: postgrespassword: password jpa:primary:database-platform: org.hibernate.dialect.H2Dialecthibernate:ddl-auto: updatesecondary:database-platform: org.hibernate.dialect.PostgreSQLDialecthibernate:ddl-auto: update 5.创建数据源配置类 接下来为每个数据源创建单独的配置类。 主数据源配置 package com.example.config; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.PlatformTransactionManager; import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; Configuration EnableJpaRepositories(basePackages com.example.primary.repository,entityManagerFactoryRef primaryEntityManagerFactory,transactionManagerRef primaryTransactionManager ) public class PrimaryDataSourceConfig {Bean(name primaryDataSource)ConfigurationProperties(prefix spring.datasource.primary)public DataSource primaryDataSource() {return DataSourceBuilder.create().build();}Bean(name primaryEntityManagerFactory)public LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory(Qualifier(primaryDataSource) DataSource dataSource) {LocalContainerEntityManagerFactoryBean em new LocalContainerEntityManagerFactoryBean();em.setDataSource(dataSource);em.setPackagesToScan(new String[] { com.example.primary.entity });HibernateJpaVendorAdapter vendorAdapter new HibernateJpaVendorAdapter();em.setJpaVendorAdapter(vendorAdapter);return em;}Bean(name primaryTransactionManager)public PlatformTransactionManager primaryTransactionManager(Qualifier(primaryEntityManagerFactory) EntityManagerFactory entityManagerFactory) {return new JpaTransactionManager(entityManagerFactory);} } 辅助数据源配置 package com.example.config; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.PlatformTransactionManager; import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; Configuration EnableJpaRepositories(basePackages com.example.secondary.repository,entityManagerFactoryRef secondaryEntityManagerFactory,transactionManagerRef secondaryTransactionManager ) public class SecondaryDataSourceConfig {Bean(name secondaryDataSource)ConfigurationProperties(prefix spring.datasource.secondary)public DataSource secondaryDataSource() {return DataSourceBuilder.create().build();}Bean(name secondaryEntityManagerFactory)public LocalContainerEntityManagerFactoryBean secondaryEntityManagerFactory(Qualifier(secondaryDataSource) DataSource dataSource) {LocalContainerEntityManagerFactoryBean em new LocalContainerEntityManagerFactoryBean();em.setDataSource(dataSource);em.setPackagesToScan(new String[] { com.example.secondary.entity });HibernateJpaVendorAdapter vendorAdapter new HibernateJpaVendorAdapter();em.setJpaVendorAdapter(vendorAdapter);return em;}Bean(name secondaryTransactionManager)public PlatformTransactionManager secondaryTransactionManager(Qualifier(secondaryEntityManagerFactory) EntityManagerFactory entityManagerFactory) {return new JpaTransactionManager(entityManagerFactory);} } 6. 定义实体管理器 为每个数据库定义实体类。确保将它们放在配置类中指定的相应包中。 主数据库实体 package com.example.primary.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; Entity public class PrimaryEntity {IdGeneratedValue(strategy GenerationType.IDENTITY)private Long id;private String name;// getters and setters } 辅助数据库实体 package com.example.secondary.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; Entity public class SecondaryEntity {IdGeneratedValue(strategy GenerationType.IDENTITY)private Long id;private String description;// getters and setters } 7. 创建存储库 为每个数据库创建存储库接口确保它们按照配置放置在正确的包中。 主存储库 package com.example.primary.repository; import com.example.secondary.entity.SecondaryEntity; import org.springframework.data.jpa.repository.JpaRepository; public interface SecondaryRepository extends JpaRepositorySecondaryEntity, Long { } 二级存储库 package com.example.secondary.repository; import com.example.secondary.entity.SecondaryEntity; import org.springframework.data.jpa.repository.JpaRepository; public interface SecondaryRepository extends JpaRepositorySecondaryEntity, Long { } 8.测试配置 最后创建一个简单的 REST 控制器来测试设置。此控制器将使用两个存储库来执行 CRUD 操作。 package com.example.controller; import com.example.primary.entity.PrimaryEntity; import com.example.primary.repository.PrimaryRepository; import com.example.secondary.entity.SecondaryEntity; import com.example.secondary.repository.SecondaryRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; RestController public class TestController {Autowiredprivate PrimaryRepository primaryRepository;Autowiredprivate SecondaryRepository secondaryRepository;GetMapping(/test)public String test() {PrimaryEntity primaryEntity new PrimaryEntity();primaryEntity.setName(Primary Entity);primaryRepository.save(primaryEntity);SecondaryEntity secondaryEntity new SecondaryEntity();secondaryEntity.setDescription(Secondary Entity);secondaryRepository.save(secondaryEntity);return Entities saved!;} }
http://www.w-s-a.com/news/903300/

相关文章:

  • 哪些网络公司可以做机票预订网站网站新闻后台怎么做
  • 微网站 域名企业网站怎么做推广
  • 兴安盟住房和城乡建设部网站在国外做网站
  • 南雄市建设局网站搜索关键词的方法
  • 网站建设维护工作经验深圳定制展会
  • 新闻类网站备案WordPress评论昵称显示错误
  • 如何建立一个个人网站自己做一个购物网站
  • 吴忠网站建设公司中国建筑股份有限公司 官网
  • 深圳电商网站开发公司page list wordpress
  • 长安外贸网站建设顺德区网站设计建设企业
  • 临沂市建设局网站简介专业建设网站开发
  • 肇庆网站制作设计中国企业500强招聘
  • 苏州厂房装修宁波seo网络推广外包报价
  • 文山知名网站建设惠州哪家做网站好
  • 物流网站风格网站登录密码保存在哪里设置
  • 免费网站怎么建立icodepython基础教程
  • 无障碍网站建设方案wordpress 任务管理系统
  • iis5.1发布网站中小企业网络营销存在的问题研究论文
  • 阳泉软件定制网站建设网站可以做多语言的吗
  • 建设网站的目的及功能定位主要包括哪些内容百度关键词优化
  • 开一个小程序要多少钱宁波seo网络推广外包报价
  • 网站备案最新备案号电子商务网站建设的规章制度
  • wordpress制作单页网站导航页面鞍山信息港招聘信息
  • 屏蔽ip地址访问网站自己做衣服的网站
  • 网站建设 域名业务 邮箱哪里有网站建设中心
  • 免费网站赚钱重庆建设摩托车股份有限公司
  • 合肥水运建设工程监理网站自己买服务器能在wordpress建网站
  • wordpress积分商城主题整站seo排名要多少钱
  • 鲜花网站建设的利息分析网站设计与制作专业
  • 深圳网站建设排名做网站的公司高创