Import org.springframework.boot.springApplication; Import org.springframework.boot.autoconfigure.enableautoconfiguration; Import org.springframework.boot.builder.springApplicationBuilder; Import org.springframework.boot.context.web.springbootservletinitializer; org.springframework.context.annotation.componentscanをインポートします。 @componentscan @enableautoconfiguration // @enablejparepositories(basepackages = "com.sonychina.backend.repository")パブリッククラスアプリケーションspringbootservletinitializer {public static void main(string [] args){springアプリケーションアプリ=新しいスプリングアプリケーション(アプリケーション); app.run(args); //springApplication.run(Application.class、args); } @Override Protected SpringApplicationBuilder Configure(SpringApplicationBuilder Application){return application.sources(application.class); }}java.util.mapをインポートします。 javax.sql.datasourceをインポートします。 Import org.springframework.beans.factory.annotation.autowired; Import org.springframework.boot.autoconfigure.jdbc.datasourcebuilder; Import org.springframework.boot.autoconfigure.orm.jpa.entitymanagerfactorybuilder; import 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をインポートします。 Import org.springframework.data.jpa.repository.config.enablejparepositories; Import org.springframework.orm.jpa.localcontainerentitymanagerfactorybean; com.test.entity.statistic.sysuserをインポートします。 com.test.repository.system.systemRepositoryをインポートします。 @Configuration @EnableJParePositories(EntityManagerFactoryRef = "EntityManagerFactoryPrimary"、basepackageClasses = {systemRepository.class})public class globalDataconfiguration {// @autowired // private dbconfig dbconfig; @autowired private jpaproperties jpaproperties; @bean(name = "primarydatasource")@primary @configurationproperties(prefix = "dataSource.primary")public dataSource primarydatasource(){system.out.println( "-------------------------------------------------------------------------------------"); DataSourceBuilder.create()。build()を返します。 } @bean(name = "SecondaryDataSource")@configurationProperties(prefix = "dataSource.secondary")public dataSource secondarydatasource(){system.out.println( "------------------------------------------------------------------------------"); // DataSourceBuilder Factory = dataSourceBuilder // .create(dbconfig.class.getClassloader())// .dbconfig.getDriver())// .url(dbconfig.geturl())// .username(dbconfig.getuser()/////。 .password(dbconfig.getPassword()); // factory.build();を返します。 DataSourceBuilder.create()。build()を返します。 } // @bean(name = "entitymanagerprimary")// @primary // public entitymanager entitymanager(entitymanagerfactorybuilder builder){// customerentitymanagerfactory(ビルダー).getobject()。createentitymanager(); //} @bean(name = "entitymanagerfactoryprimary")@primary public localcontitymanagerfactorybean customerentitymanagerfactory(entitymanagerfactorybuilder builder){return builder.datasource(primarydatasource()).properties(getvendorproperties()) .packages(sysuser.class).persistenceunit( "system").build(); } private Map <String、String> getVendorProperties(dataSource dataSource){return jpaproperties.gethibernateProperties(dataSource); }}java.util.mapをインポートします。 javax.sql.datasourceをインポートします。 Import org.springframework.beans.factory.annotation.autowired; org.springframework.beans.factory.annotation.qualifierをインポートします。 Import org.springframework.boot.autoconfigure.orm.jpa.entitymanagerfactorybuilder; import org.springframework.boot.autoconfigure.orm.jpa.jpaproperties; org.springframework.context.annotation.beanをインポートします。 org.springframework.context.annotation.configurationをインポートします。 Import org.springframework.data.jpa.repository.config.enablejparepositories; org.springframework.orm.jpa.jpatransactionmanagerをインポートします。 Import org.springframework.orm.jpa.localcontainerentitymanagerfactorybean; Import org.springframework.transaction.platformtransactionmanager; Import 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})公開クラスセカンフェムフッコウィー{ jpaproperties; @autowired@qualifier( "SecondaryDataSource")Private DataSource DataSource; // @bean(name = "entitymanagerprimary")// @primary // public entitymanager entitymanager(entitymanagerfactorybuilder builder){// customerentitymanagerfactory(ビルダー).getobject()。createentitymanager(); //} @bean(name = "entitymanagerfactorysecondary")public localcontinerentitymanagerfactorybean customerentitymanagerfactory(entitymanagerfactorybuilder builder){return builder.datasource(datasource).properties(getvendorproperties(datasource).pack).pack(datasource).packiges(datasource).packers(datasource)。 .persistenceUnit( "customers").build(); } private Map <String、String> getVendorProperties(dataSource dataSource){return jpaproperties.gethibernateProperties(dataSource); } @bean(name = "transactionManagersecondary")PlatformTransactionManager TransactionManagerseCondary(EntityManagerFactoryBuilder Builder){return new JPatransactionManager(CustomerEntityManagerFactory(Builder).GetObject()); }}Import org.springframework.data.jpa.repository.jparePository; org.springframework.data.jpa.repository.modifiing; org.springframework.data.jpa.repository.Queryをインポートします。 com.test.entity.manage.bannerをインポートします。 Public Interface BannerRepositoryはjparePository <Banner、long> {@modifying @query( "update banner m set m.name =?1ここでm.id =?2")public void update(string bannername、long id); } 1.5。注意:对@Primary
总结
Spring Boot Springjpa