欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring boot springjpa 支持多个数据源的实例代码

程序员文章站 2023-12-09 19:25:27
1.springboot的程序启动类 import org.springframework.boot.springapplication; import or...

1.springboot的程序启动类

import org.springframework.boot.springapplication; 
import org.springframework.boot.autoconfigure.enableautoconfiguration; 
import org.springframework.boot.builder.springapplicationbuilder; 
import org.springframework.boot.context.web.springbootservletinitializer; 
import org.springframework.context.annotation.componentscan; 
@componentscan 
@enableautoconfiguration 
//@enablejparepositories(basepackages = "com.sonychina.backend.repository") 
public class application extends springbootservletinitializer { 
  public static void main(string[] args) { 
    springapplication app = new springapplication(application.class); 
    app.run(args); 
    //springapplication.run(application.class, args); 
  } 
  @override 
  protected springapplicationbuilder configure(springapplicationbuilder application) { 
    return application.sources(application.class); 
  } 
} 

 2.双数据源配置类

import java.util.map; 
import 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; 
import org.springframework.boot.context.properties.configurationproperties; 
import org.springframework.context.annotation.bean; 
import org.springframework.context.annotation.configuration; 
import org.springframework.context.annotation.primary; 
import org.springframework.data.jpa.repository.config.enablejparepositories; 
import org.springframework.orm.jpa.localcontainerentitymanagerfactorybean; 
import com.test.entity.statistic.sysuser; 
import 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("-------------------- primarydatasource init ---------------------"); 
    return 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())  
//       .url(dbconfig.geturl())  
//       .username(dbconfig.getuser())  
//       .password(dbconfig.getpassword());  
//   return factory.build(); 
    return datasourcebuilder.create().build(); 
  } 
// @bean(name = "entitymanagerprimary") 
// @primary 
// public entitymanager entitymanager(entitymanagerfactorybuilder builder) { 
//   return customerentitymanagerfactory(builder).getobject().createentitymanager(); 
// } 
  @bean(name="entitymanagerfactoryprimary") 
  @primary 
  public localcontainerentitymanagerfactorybean customerentitymanagerfactory(entitymanagerfactorybuilder builder) { 
    return builder.datasource(primarydatasource()) 
          .properties(getvendorproperties(primarydatasource())) 
          .packages(sysuser.class) 
          .persistenceunit("system") 
          .build(); 
  } 
  private map<string, string> getvendorproperties(datasource datasource) { 
    return jpaproperties.gethibernateproperties(datasource); 
  } 
} 

3.第二个jpa实体管理器

import java.util.map; 
import javax.sql.datasource; 
import org.springframework.beans.factory.annotation.autowired; 
import org.springframework.beans.factory.annotation.qualifier; 
import org.springframework.boot.autoconfigure.orm.jpa.entitymanagerfactorybuilder; 
import org.springframework.boot.autoconfigure.orm.jpa.jpaproperties; 
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.transaction.platformtransactionmanager; 
import org.springframework.transaction.annotation.enabletransactionmanagement; 
import com.test.entity.manage.banner; 
import com.test.repository.manage.bannerrepository; 
@configuration 
@enabletransactionmanagement 
@enablejparepositories(entitymanagerfactoryref="entitymanagerfactorysecondary", 
  transactionmanagerref="transactionmanagersecondary", 
  basepackageclasses= {bannerrepository.class}) 
public class secondemfbconfig { 
  @autowired 
  private jpaproperties jpaproperties; 
  @autowired@qualifier("secondarydatasource") 
  private datasource datasource; 
// @bean(name = "entitymanagerprimary") 
// @primary 
// public entitymanager entitymanager(entitymanagerfactorybuilder builder) { 
//   return customerentitymanagerfactory(builder).getobject().createentitymanager(); 
// } 
  @bean(name="entitymanagerfactorysecondary") 
  public localcontainerentitymanagerfactorybean customerentitymanagerfactory(entitymanagerfactorybuilder builder) { 
    return builder.datasource(datasource) 
          .properties(getvendorproperties(datasource)) 
          .packages(banner.class) 
          .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()); 
  } 
} 

4.repository类举例

import org.springframework.data.jpa.repository.jparepository; 
import org.springframework.data.jpa.repository.modifying; 
import org.springframework.data.jpa.repository.query; 
import com.test.entity.manage.banner; 
public interface bannerrepository extends jparepository<banner, long> { 
  @modifying 
  @query("update banner m set m.name=?1 where m.id=?2") 
  public void update(string bannername, long id); 
} 

1.5.注意:对@primary修饰的localcontainerentitymanagerfactorybean可以不用指定transactionmanager,spring上下文自动使用默认的jpatransactionmanager,但是对于第二个或第三个等等必须指定transactionmanager。可以参考springboot官方文档中的相关章节。 

总结

以上所述是小编给大家介绍的spring boot springjpa 支持多个数据源的实例代码,希望对大家有所帮助