LMLPHP后院

spring boot jpa 多个数据源配置技术

maybe yes 发表于 2017-05-24 22:32

spring boot jpa 多数据源的配置,对于新手来讲还是不简单的,网上的很多资料都已经过时并且不靠谱。

application.properties 配置参考:

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.cache=false
spring.http.encoding.charset=utf8


spring.datasource.url=jdbc:mysql://192.168.1.250:3306/main?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.sql-script-encoding=utf-8
spring.datasource.maximum-pool-size=100
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
spring.datasource.validation-query=SELECT 1
spring.datasource.test-on-borrow=false
spring.datasource.test-while-idle=true
spring.datasource.time-between-eviction-runs-millis=18800

bar.datasource.url=jdbc:mysql://192.168.1.250:3306/project
bar.datasource.username=root
bar.datasource.password=
bar.datasource.driver-class-name=com.mysql.jdbc.Driver
bar.datasource.sql-script-encoding=utf-8

BarConfig 参考:

package com.lmlphp.nb.config;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
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;


@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "barEntityManagerFactory",
        transactionManagerRef = "barTransactionManager",
        basePackages = { "com.lmlphp.nb.domain.p" })
public class BarConfig {

    @Bean(name = "barDataSource")
    @ConfigurationProperties(prefix="bar.datasource")
    public DataSource barDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "barEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean barEntityManagerFactory(
            EntityManagerFactoryBuilder builder,
            @Qualifier("barDataSource") DataSource barDataSource) {
        return builder
                .dataSource(barDataSource)
                .packages("com.lmlphp.nb.domain.p")
                .persistenceUnit("bar")
                .build();
    }

    @Bean(name = "barTransactionManager")
    public PlatformTransactionManager barTransactionManager(
            @Qualifier("barEntityManagerFactory") EntityManagerFactory barEntityManagerFactory) {
        return new JpaTransactionManager(barEntityManagerFactory);
    }

}

FooConfig 参考:

ackage com.lmlphp.nb.config;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
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.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;


@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "entityManagerFactory",
        basePackages = { "com.lmlphp.nb.domain.s" })
public class FooConfig {

    @Primary
    @Bean(name = "dataSource")
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(
            EntityManagerFactoryBuilder builder,
            @Qualifier("dataSource") DataSource dataSource) {
        return builder
                .dataSource(dataSource)
                .packages("com.lmlphp.nb.domain.s")
                .persistenceUnit("foo")
                .build();
    }

    @Primary
    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager(
            @Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }

}

spring boot 启动类参考:

package com.lmlphp.nb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Start {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Start.class, args);
    }
}

我一开始折腾好久一直没有配置成功,看了好多资料,官方的文档,非官方的文档,网上的不靠谱教程,连接数据库后,提示找不到表名。后来才知道因为我按照官方的 Demo 一步一步的学习 Spring Boot 框架,官网的例子里启动使用的是 @EnableAutoConfiguration 注解。后来修改为 @SpringBootApplication 注解搞定。

写 Java 代码import 包名一定要写完整,千万不要使用 * 号,这样对于不熟悉的人非常不友好。很多教程的示例代码都是不包含 import 部分的,对于这样的代码我表示找包是一件非常痛苦的事情,不知道那些 Java 大神是怎么解决这样的问题的。

和一些 java 大神聊天,问过他们一些问题,有很多大神表示他们不熟悉 Java 框架,已经好久不做经典 Java web 项目了。对于这些大神,我是非常理解的,同时也很羡慕他们的,他们有自己的技术追求,可以不需要为了生存而去学一些不实用的东西

相关文章
评论列表
maybe yes
@-天尘子- 谢谢
-天尘子-
顶一下,写的很好!网上互相抄的太多了,君乃璞玉~~~
2024-12-22 12:03:49 1734840229 0.039758