springboot -- 使用jpa和hibernate的方式配置多数据源 ( sqlite 和 mysql )

x33g5p2x  于2021-12-24 转载在 其他  
字(5.2k)|赞(0)|评价(0)|浏览(479)

原文地址:http://www.cnblogs.com/niuzhifa/p/9279543.html

pom.xml配置

<!--mysql依赖-->
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<!--sqllite依赖-->
<!--...-->

application.properties配置

#数据库配置
mysql.spring.datasource.jdbc-url =jdbc:mysql://127.0.0.1:3306/test
mysql.spring.datasource.username=admin
mysql.spring.datasource.password=123456
mysql.spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#数据库连接池
mysql.spring.datasource.max-idle=100
mysql.spring.datasource.max-wait=10000
mysql.spring.datasource.min-idle=5
mysql.spring.datasource.initial-size=5

#sqlite数据库的配置
sqlite.spring.datasource.jdbc-url = jdbc:sqlite:test.db
sqlite.spring.datasource.driver-class-name = org.sqlite.JDBC
sqlite.spring.datasource.username =
sqlite.spring.datasource.password =

#关闭hibernate的自动创建表结构的机制
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true

DataSourceConfig配置

@Configuration
public class DataSourceConfig {
    @Bean(name = "sqliteDataSource")
    @ConfigurationProperties(prefix = "sqlite.spring.datasource")   
    public DataSource sqliteDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Primary    //配置该数据源为主数据源
    @Bean(name = "mysqlDataSource")
    @ConfigurationProperties(prefix = "mysql.spring.datasource")
    public DataSource mysqlDataSource(){
        return DataSourceBuilder.create().build();
    }
}

MysqlDataSourceConfig配置

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
    entityManagerFactoryRef = "entityManagerFactoryMysql",   //EntityManagerFactory引用
    transactionManagerRef = "transactionManagerMysql",   //transactionManager引用
    basePackages = {"com.xxx.mysql"})
public class MysqlDataSourceConfig {
    /**
     * 注入 mysql数据源
     */
    @Resource(name = "mysqlDataSource")
    private DataSource mysqlDataSource;

    /**
     * 注入JPA配置实体
     */
    @Autowired
    private JpaProperties jpaProperties;

    /**
     *  这里其实不需要配置数据库的方言.
     *  像hibernate.hbm2ddl.auto 可以在这里配置.但是我的是在application.properties中配置的.
     */
    private Map<String, Object> getVendorProperties() {
        HibernateSettings hibernateSettings = new HibernateSettings();
        return jpaProperties.getHibernateProperties(hibernateSettings);
    }

    /**
     * 配置EntityManagerFactory实体
     * @param builder
     * @return 实体管理工厂
     * packages     扫描@Entity注释的软件包名称
     * persistenceUnit  持久性单元的名称。 如果只建立一个EntityManagerFactory,可以省略,
                        但是如果在同一个应用程序中有多个,应该给它们不同的名字
     * properties       标准JPA或供应商特定配置的通用属性。 这些属性覆盖构造函数中提供的任何值。
     */
    @Primary
    @Bean(name = "entityManagerFactoryMysql")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryMysql(
             EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(mysqlDataSource)
                .properties(getVendorProperties())
                .packages("com.xxx.mysql")
                .persistenceUnit("mysqlPersistenceUnit")
                .build();
    }

    /**
     * 配置EntityManager实体
     * @param builder
     * @return 实体管理器
     */
    @Primary
    @Bean(name = "entityManagerMysql")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryMysql(builder).getObject().createEntityManager();
    }

    /**
     * 配置事务transactionManager
     * @param builder
     * @return 事务管理器
     */
    @Primary
    @Bean(name="transactionManagerMysql")
    public PlatformTransactionManager transactionManagerMysql(
             EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryMysql(builder).getObject());
    }
}

SqliteDataSourceConfig配置

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
    entityManagerFactoryRef="entityManagerFactorySqlite",  //EntityManagerFactory引用 
    transactionManagerRef="transactionManagerSqlite", //transactionManager引用
    basePackages={"com.xxx.sqlite"})
public class SqliteDataSourceConfig {
    /**
     * 注入 sqlite数据源
     */
    @Resource(name="sqliteDataSource")
    private DataSource sqliteDataSource;

    /**
     * 注入JPA配置实体
     */
    @Autowired
    private JpaProperties jpaProperties;

    /**
     * 这里其实不需要配置数据库的方言.
     */
    private Map<String, Object> getVendorProperties() {
        HibernateSettings hibernateSettings=new HibernateSettings();
        return jpaProperties.getHibernateProperties(hibernateSettings);
    }

    /**
     * 配置EntityManagerFactory实体
     * @param builder
     * @return 实体管理工厂
     * packages     扫描@Entity注释的软件包名称
     * persistenceUnit  
     * properties       标准JPA或供应商特定配置的通用属性。 这些属性覆盖构造函数中提供的任何值。
     */
    @Bean(name="entityManagerFactorySqlite")
    public LocalContainerEntityManagerFactoryBean entityManagerFactorySqlite(
              EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(sqliteDataSource)
                .properties(getVendorProperties())
                .packages("com.xxx.sqlite")
                .persistenceUnit("sqlitePersistenceUnit")
                .build();
    }

    /**
     * 配置EntityManager实体
     * @param builder
     * @return 实体管理器
     */
    @Bean(name="entityManagerSqlite")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactorySqlite(builder).getObject().createEntityManager();
    }

    /**
     * 配置事务transactionManager
     * @param builder
     * @return 事务管理器
     */
    @Bean(name="transactionManagerSqlite")
    public PlatformTransactionManager transactionManagerSqlite(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactorySqlite(builder).getObject());
    }
}

然后com.xxx.mysql和com.xxx.sqlite目录下就可以放各自的domain和repository了.跟单数据源没有区别.

参考:

https://www.cnblogs.com/sxdcgaq8080/p/7978205.html   (springboot-1.5.9)

https://my.oschina.net/chinesedragon/blog/1647846  (springboot-2.0.0)

相关文章