導入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”)公共類應用程序擴展了SpringBootServletIlItialializer {public static void main(string string [] args [] args [] args agrs = springapplication app = new springapplication.claps.class.class.class.class.class.class.class.class.class.class); app.run(args); //springapplication.run(application.class,args); } @Override Protected SpringApplicationBuilder configure(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(EntityManagerFactoryReref =“ EntityManagerFactoryPrimary”,basepackageclasses = {SystemRepository.Class})公共類Global global globaldataConfiguration {// @autowied私人jpaproperties jpaproperties; @Bean(name="primaryDataSource") @Primary @ConfigurationProperties(prefix="datasource.primary") public DataSource primaryDataSource() { System.out.println("-------------------- primaryDataSource init ---------------------");返回datasourcebuilder.create()。 build(); } @Bean(name="secondaryDataSource") @ConfigurationProperties(prefix="datasource.secondary") public DataSource secondaryDataSource() { System.out.println("-------------------- secondaryDataSource init ---------------------"); // datasourcebuilder factory = datasourcebuilder // .create(dbconfig.class.getClassLoader())// .driverClassName(dbconfig.getDriver() .password(dbconfig.getPassword()); // return Factory.build();返回datasourcebuilder.create()。build(); } // @bean(name =“ entityManagerPrimary”)// @primary // public entityManager entityManager(EntityManagerFactoryBuilder Builder){// return customerentityManagerFactory(buililder).getObjectory(builder).getObject()createNtityManager()。 //} @bean(name =“ entityManagerFactoryPrimary”)@primary localContainErentityManagerFactoryBean cultiserentityManagerFactory(EntityManagerFactoryBuilder Builder) .packages(sysuser.class).persistenceUnit(“ system”).build(); }私有地圖<字符串,字符串> getVendorProperties(dataSource dataSource){返回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; @enabletransactionManagement @enablejparepositories(EntityManagerFactoryRef =“ EntityManagerFactorySecondary”,TrassActionManagerRef =“ TrassActionManagerSecondary”,basepackageclasses = {BannerRepoSitory.Class prublic clupprie clupperrig secontraf, jpaproperties; @autowired@feolifier(“ SecontaryDataSource”)私人數據源數據; // @bean(name =“ entityManagerPrimary”)// @primary // public entityManager entityManager(EntityManagerFactoryBuilder Builder){//返回cultuertentityManagerFactory(buililder).getObject(buil builder).getObject(buitObject).getObject()。 //} @bean(name =“ EntityManagerFactorySecondary”)公共LocalContainerentityManagerFactoryBean customerentityManagerFactory(EntityManagerFactoryBuilder Builder){ .persistenceUnit(“客戶”).build(); }私有地圖<字符串,字符串> getVendorProperties(dataSource dataSource){返回jpaproperties.gethibernateproperties(dataSource); } @bean(name =“ TransActionManagerSecondary”)PlatformTransActionManager TrassActionManagerSecondary(EntityManagerFactoryBuilder builder){返回新的JpatransactionManager(CultuerentityManagerFactory(Builder(builder).getObject).getObject).getObject); }}}導入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 <banner,long> {@modifying @query(“ update banner m set m.name =?1 where m.id =?2”)public void dustafe(字符串bannername,long ID); } 1.5。注意:@@primary localcontainerentityManagerFactoryBean可以不用指定TransActionManager,Spring上下文自動使用默認的jpatransactionManager,但是對於第二個或第三個等等必須指定TransActionManager。可以參考springboot官方文檔中的相關章節。
總結
spring boot springjpa支持多個數據源的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持! ,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!