时间:2021-05-02
1.springboot的程序启动类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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.双数据源配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 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实体管理器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 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类举例
1 2 3 4 5 6 7 8 9 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 支持多个数据源的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:https://blog.csdn.net/ba5189tsl/article/details/47341425
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
什么是多数据源支持?简单的说,就是一个项目里,同时可以访问多个不同的数据库。实现原理单个数据源在配置时会绑定一套mybatis配置,多个数据源时,不同的数据源绑
二、广泛的数据源连接-整合多系统数据 提供超过35个数据源驱动用于访问任何相关数据源 支持在一份报表中整合多个数据源 三、可视化设计环境-快速上手,自
在日常工作中,我们可能需要连接多个MongoDB数据源,比如用户库user,日志库log。本章我们来记录连接多个数据源的步骤,以两个数据源为例,多个数据源类推。
1.概述项目中经常会遇到一个应用需要访问多个数据源的情况,本文介绍在SpringBoot项目中利用SpringDataJpa技术如何支持多个数据库的数据源。具体
SpringBoot如何快速配置数据源;有如下两种方式:通过spring-boot-starter-jdbc快速配置数据源自定义数据源DataSource首先我