Spring Boot多个数据库

时间:2015-06-22 02:38:32

标签: spring spring-boot

我正在尝试使用多个数据库运行Spring Boot 我发现How to use 2 or more databases with spring?http://xantorohara.blogspot.com.au/2013/11/spring-boot-jdbc-with-multiple.html已经有了帮助。但是,类TomcatDataSourceConfiguration不包含在1.2.4版中。如何使用新版本实现此功能?

编辑:现在我收到错误No bean named 'entityManagerFactory' is defined

DatabaseConfiguration.java

@Configuration
@EnableAutoConfiguration
public class DatabaseConfiguration {

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

    @Bean
    public LocalContainerEntityManagerFactoryBean weatherManagerFactory(
            EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(weatherDataSource())
                .packages(User1.class)
                .persistenceUnit("user2")
                .build();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.weather_alerts")
    public DataSource weatherAlertsDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean weatherAlertsManagerFactory(
            EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(weatherAlertsDataSource())
                .packages(User1.class)
                .persistenceUnit("user1")
                .build();
    }
}

User1.java

@Entity
@Table(name = "users1")
public class User1 {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String name1;

    public User1() {}

    public User1(String name1) {
        this.name1 = name1;
    }

    // Getter Setters

}

User2.java

@Entity
@Table(name = "users2")
public class User2 {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String name1;

    public User2() {}

    public User1(String name1) {
        this.name2 = name2;
    }

    // Getter Setters

}

User1Dao.java

@Transactional
public interface User1Dao extends CrudRepository<User1, Long> {     
}

User2Dao.java

@Transactional
public interface User2Dao extends CrudRepository<User2, Long> {     
}

UserController.java

@Controller
public class UserController implements CommandLineRunner {

    @Autowired private User1Dao user1Dao;
    @Autowired private User2Dao user2Dao;

    @Override
    public void run(String... arg0) throws Exception {
        user1Dao.save(new User1("name 1"));
        user2Dao.save(new User2("name 2"));
    }

}

Application.java

@SpringBootApplication
public class Application {

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

}

application.properties

spring.weather_alerts.url=jdbc:mysql://localhost:3306/weather_alerts
spring.weather_alerts.username=root
spring.weather_alerts.password=

spring.weather.url=jdbc:mysql://localhost:3306/weather
spring.weather.username=root
spring.weather.password=

spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

1 个答案:

答案 0 :(得分:1)

Spring Data默认查找名为“entityManagerFactory”的EntityManagerFactory bean,请参阅docs。由于您没有该名称的EntityManagerFactory bean,因此您会看到错误。

解决方案是在配置中定义entityManagerFactoryRef名称,例如

@EnableJpaRepositories(entityManagerFactoryRef = "weatherManagerFactory")

您可能需要创建两个单独的配置类,每个配置类包含一个LocalContainerEntityManagerFactoryBean bean,并且每个类都有一个类级别@EnableJpaRepositories

旁注,不要忘记排除DataSourceAutoconfiguration中的Application.java类,因为您自己正在配置数据源。

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)