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

时间:2022-10-30 09:41:18

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