前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot 1.0 && 2.0 + JPA 多数据源配置与使用

Spring Boot 1.0 && 2.0 + JPA 多数据源配置与使用

作者头像
happyJared
发布2018-12-14 16:19:23
1.5K0
发布2018-12-14 16:19:23
举报
文章被收录于专栏:happyJaredhappyJared

环境说明

  • Spring Boot 1.5.17.RELEASE Spring Boot 2.1.0.RELEASE
  • MySQL v5.6.19
  • PostgreSQL v10.4

无特殊说明,以下所说的环境均指 Spring Boot 2.1.0.RELEASE,如果使用的是 Spring Boot 1.5.17.RELEASE 这个版本,只需要调整下面有做说明的几处地方

连接配置

application.yml中定义如下信息:

代码语言:javascript
复制
spring:
  jpa:
    hibernate:
      # 多数据源下,该属性不生效,需要在配置中额外指定,这里仅表示普通定义
      ddl-auto: create-drop
    properties:
      hibernate:
        show_sql: true
        format_sql: true
        jdbc:
          lob:
            non_contextual_creation: true
    open-in-view: false
  # 定义不同数据源的连接信息
  datasource:
    hikari:
      mysql:
        # Spring Boot 1.0+ 版本:使用spring.datasource.url
        # Spring Boot 2.0+ 版本:使用spring.datasource.hikari.jdbc-url
        jdbc-url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
        username: root
        password: root
        # Spring Boot 1.0+ 版本:使用com.mysql.jdbc.Driver
        # Spring Boot 2.0+ 版本:使用com.mysql.cj.jdbc.Driver
        driver-class-name: com.mysql.cj.jdbc.Driver
      postgres:
        jdbc-url: jdbc:postgresql://localhost:5432/postgres
        username: postgres
        password: postgres
        driver-class-name: org.postgresql.Driver

配置数据源

根据上面定义的配置信息,配置这两个数据源:

代码语言:javascript
复制
// Spring Boot 1.0+ ,DataSourceBuilder所在包位置为:org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder
// Spring Boot 2.0+ ,DataSourceBuilder所在包位置为:org.springframework.boot.jdbc.DataSourceBuilder
@Configuration
public class DataSourceConfig {

    @Primary
    @Bean
    @ConfigurationProperties("spring.datasource.hikari.mysql")
    public DataSource mysqlDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties("spring.datasource.hikari.postgres")
    public DataSource postgresDataSource() {
        return DataSourceBuilder.create().build();
    }

}

JPA 支持

添加 mysql 对应数据源的 JPA 支持:

代码语言:javascript
复制
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "mysqlEntityManagerFactory",
        transactionManagerRef = "mysqlTransactionManager",
        // 数据层所在的包位置
        basePackages = "cn.mariojd.springboot.multiple.datasource.jpa.mysql.repository")
public class MysqlDataSourceConfig {

    @Resource
    private Environment environment;

    @Resource
    @Qualifier("mysqlDataSource")
    private DataSource dataSource;

    @Bean
    @Primary
    public LocalContainerEntityManagerFactoryBean mysqlEntityManagerFactory(EntityManagerFactoryBuilder builder) {
        Map<String, Object> properties = new HashMap<>(4);
        // Spring Boot 1.0+ ,使用MySQLDialect
        // Spring Boot 2.0+ ,指定MySQLDialect会默认使用MyISAM引擎,改成MySQL55Dialect即可
        properties.put("hibernate.dialect", "org.hibernate.dialect.MySQL55Dialect");
        properties.put("hibernate.hbm2ddl.auto", environment.getProperty("spring.jpa.hibernate.ddl-auto"));
        return builder.dataSource(dataSource)
                .properties(properties)
                // 实体所在的包位置
                .packages("cn.mariojd.springboot.multiple.datasource.jpa.mysql.entity")
                .persistenceUnit("jpa-mysql")
                .build();
    }

    @Bean
    @Primary
    public PlatformTransactionManager mysqlTransactionManager(@Qualifier("mysqlEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }

}

添加 postgres 对应数据源的 JPA 支持:

代码语言:javascript
复制
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "postgresEntityManagerFactory",
        transactionManagerRef = "postgresTransactionManager",
        // 数据层所在的包位置
        basePackages = "cn.mariojd.springboot.multiple.datasource.jpa.postgres.repository")
public class PostgresDataSourceConfig {

    @Resource
    private Environment environment;

    @Resource
    @Qualifier("postgresDataSource")
    private DataSource dataSource;

    @Bean
    public LocalContainerEntityManagerFactoryBean postgresEntityManagerFactory(EntityManagerFactoryBuilder builder) {
        Map<String, Object> properties = new HashMap<>(4);
        properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
        properties.put("hibernate.hbm2ddl.auto", environment.getProperty("spring.jpa.hibernate.ddl-auto"));
        return builder.dataSource(dataSource)
                // 实体所在的包位置
                .properties(properties)
                .packages("cn.mariojd.springboot.multiple.datasource.jpa.postgres.entity")
                .persistenceUnit("jpa-postgres")
                .build();
    }

    @Bean
    public PlatformTransactionManager postgresTransactionManager(@Qualifier("postgresEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }

}

相关定义

mysql 对应的数据源配置中,定义了实体 Student 和对应的数据层接口 StudentRepository

代码语言:javascript
复制
@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String name;

    public Student(String name) {
        this.name = name;
    }
}
代码语言:javascript
复制
public interface StudentRepository extends JpaRepository<Student, Integer> {
}

postgres 对应的数据源配置中,定义了实体 Teacher 和对应的数据层接口 TeacherRepository

代码语言:javascript
复制
@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
public class Teacher {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String name;

    public Teacher(String name) {
        this.name = name;
    }
}
代码语言:javascript
复制
public interface TeacherRepository extends JpaRepository<Teacher, Integer> {
}

单元测试

代码语言:javascript
复制
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootJpaMultipleDataSourceTest {

    @Resource
    private StudentRepository studentRepository;

    @Resource
    private TeacherRepository teacherRepository;

    @Test
    public void test() {
        studentRepository.save(new Student("张三"));
        studentRepository.save(new Student("李四"));
        studentRepository.save(new Student("王五"));

        Assert.assertEquals(3, studentRepository.findAll().size());

        teacherRepository.save(new Teacher("张老师"));
        teacherRepository.save(new Teacher("李老师"));
        teacherRepository.save(new Teacher("王老师"));
        Assert.assertEquals(3, teacherRepository.findAll().size());
    }

}

参考链接

Using multiple datasources with Spring Boot and Spring Data Spring JPA – Multiple Databases Spring Boot多数据源配置与使用 How to connect to Multiple databases with Spring Data JPA Springboot2.0中Hibernate默认创建的mysql表为myisam引擎问题 关于springboot2.0.0配置多数据源出现jdbcUrl is required with driverClassName的错误 解决mysql java.sql.SQLException: The server time zone value ...

示例源码 欢迎关注我的个人公众号:超级码里奥 如果这对您有帮助,欢迎点赞和分享,转载请注明出处

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.11.21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 环境说明
  • 连接配置
  • 配置数据源
  • JPA 支持
  • 相关定义
  • 单元测试
  • 参考链接
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档