นำเข้า org.springframework.boot.springapplication; นำเข้า org.springframework.boot.autoconfigure.enableautoconfiguration; นำเข้า org.springframework.boot.builder.springapplicationbuilder; นำเข้า org.springframework.boot.context.web.springbootservletInitializer; นำเข้า org.springframework.context.annotation.componentscan; @componentscan @enableautoconfiguration // @enablejparepositories (basepackages = "com.sonychina.backend.repository") แอปพลิเคชันระดับสาธารณะขยาย SpringbootservletInitializer app.run (args); //springapplication.run(application.class, args); } @Override Protected SpringApplicationBuilder กำหนดค่า (แอปพลิเคชัน SpringApplicationBuilder) {return application.Sources (Application.class); -นำเข้า java.util.map; นำเข้า Javax.sql.Datasource; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.boot.autoconfigure.jdbc.datasourcebuilder; นำเข้า org.springframework.boot.autoconfigure.orm.jpa.entityManagerFactoryBuilder; นำเข้า org.springframework.boot.autoconfigure.orm.jpa.jpaproperties; นำเข้า org.springframework.boot.context.properties.configurationProperties; นำเข้า org.springframework.context.annotation.bean; นำเข้า org.springframework.context.annotation.configuration; นำเข้า org.springframework.context.annotation.primary; นำเข้า org.springframework.data.jpa.repository.config.enablejparepositories; นำเข้า org.springframework.orm.jpa.localcontainerentityManagerFactoryBean; นำเข้า com.test.entity.statistic.sysuser; นำเข้า com.test.repository.system.systemrepository; @configuration @enablejparepositories (entityManagerFactoryRef = "entityManagerFactoryPrimary", basepackageclasses = {systemrepository.class}) ระดับสาธารณะ globaldataconfiguration {// @autowired // dbconfig dbconfig ส่วนตัว; @autowired ส่วนตัว jpaproperties jpaproperties; @Bean (name = "PriminmalDataSource") @Primary @ConfigurationProperties (คำนำหน้า = "DataSource.primary") Publicource PrimaryDataSource () {System.out.println ("-------------------- ส่งคืน dataSourceBuilder.create (). build (); } @Bean (name = "secondaryDataSource") @ConfigurationProperties (คำนำหน้า = "dataSource.secondary") DataSource Public SecondaryDataSource () {System.out.println ("-------------------- // DataSourceBuilder Factory = DataSourceBuilder // .create (dbconfig.class.getClassloader ()) // .driverclassName (dbconfig.getDriver ()) // .url (dbconfig.geturl () //. .password (dbconfig.getPassword ()); // return Factory.build (); ส่งคืน dataSourceBuilder.create (). build (); } // @Bean (name = "EntityManagerPrimary") // @Primary // Public EntityManager EntityManager (EntityManagerFactoryBuilder Builder) {// ส่งคืนลูกค้า //} @Bean (name = "EntityManagerFactoryPrimary") @Primary Public LocalContainerEntityManagerFactoryBean CustomerEntityManagerFactory (EntityManagerFactoryBuilder Builder) {Builder.Datasource .packages (sysuser.class) .persistenceUnit ("ระบบ") .build (); } แผนที่ส่วนตัว <สตริง, สตริง> getVendorProperties (DataSource DataSource) {return jpaproperties.getHibernateProperties (DataSource); -นำเข้า java.util.map; นำเข้า Javax.sql.Datasource; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.beans.factory.annotation.qualifier; นำเข้า org.springframework.boot.autoconfigure.orm.jpa.entityManagerFactoryBuilder; นำเข้า org.springframework.boot.autoconfigure.orm.jpa.jpaproperties; นำเข้า org.springframework.context.annotation.bean; นำเข้า org.springframework.context.annotation.configuration; นำเข้า org.springframework.data.jpa.repository.config.enablejparepositories; นำเข้า org.springframework.orm.jpa.jpatransactionmanager; นำเข้า org.springframework.orm.jpa.localcontainerentityManagerFactoryBean; นำเข้า org.springframework.transaction.platformTransactionManager; นำเข้า org.springframework.transaction.annotation.enableTransactionManagement; นำเข้า com.test.entity.Manage.banner; นำเข้า com.test.Repository.Manage.BannerRepository; @Configuration @EnableTransactionManagement @enablejparepositories (entityManagerFactoryRef = "EntityManagerFactorySecondary", TransactionManagerRef = "TransactionManagersecondary", basepackageclasses = {bannerRepository.class} @AutoWired@Qualifier ("SecondaryDataSource") DataSource DataSource ส่วนตัว; // @Bean (name = "EntityManagerPrimary") // @Primary // Public EntityManager EntityManager (EntityManagerFactoryBuilder Builder) {// ส่งคืนลูกค้า //} @Bean (name = "EntityManagerFactorySecondary") Public LocalContainerEntityManagerFactoryBean CustomerEntityManagerFactory (EntityManagerFactoryBuilder Builder) {Return Builder.Datasource (DataSource) .PersistenceUnit ("ลูกค้า"). build (); } แผนที่ส่วนตัว <สตริง, สตริง> getVendorProperties (DataSource DataSource) {return jpaproperties.getHibernateProperties (DataSource); } @Bean (name = "TransactionManagersecondary") PlatformTransactionManager TransactionManagersecondary (EntityManagerFactoryBuilder Builder) {ส่งคืน JPatransactionManager ใหม่ -นำเข้า org.springframework.data.jpa.repository.jparepository; นำเข้า org.springframework.data.jpa.repository.modifying; นำเข้า org.springframework.data.jpa.repository.query; นำเข้า com.test.entity.Manage.banner; อินเทอร์เฟซสาธารณะ BannerRepository ขยาย jParePository <แบนเนอร์, long> {@modifying @Query ("Update Banner M SET M.NAME =? 1 โดยที่ M.ID =? 2") การอัปเดตโมฆะสาธารณะ - 1.5. 注意: 对 @primary 修饰的 localContainerentityManagerFactoryBean 可以不用指定 TransactionManager, ฤดูใบไม้ผลิ上下文自动使用默认的 JPatransactionManager, transactionManager 。可以参考 Springboot 官方文档中的相关章节。
总结
以上所述是小编给大家介绍的 Spring Boot Springjpa 支持多个数据源的实例代码, 希望对大家有所帮助, 如果大家有任何疑问请给我留言, 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持! 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!