2017-06-12 195 views
1

我和春天有啓動應用程序的問題。我想MongoDB數據庫和MySQL數據庫在我的春節,啓動應用程序連接。我想知道,如果可能的話,在積極的情況下,我怎樣才能讓這多個連接。我做了基於沒有成功與MySQL和張貼的例子一試。所以我想知道是否有人有一個簡單的例子來了解這種方法。 感謝春季啓動連接MySQL和MongoDB

回答

1

這是可以做到this.you將有創造不同的數據源不同的配置。此鏈接有上 http://www.baeldung.com/spring-data-jpa-multiple-databases

另一個有用的計算器問題很好的例子:Spring Boot Configure and Use Two DataSources

要開始使用蒙戈和MySQL,你可以按照例如從spring.io指南。

https://spring.io/guides/gs/accessing-data-mongodb/

https://spring.io/guides/gs/accessing-data-mysql/

編輯:

我已經創建這一個例子中,合併上述

package hello; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.CommandLineRunner; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; 

import hello.model.Customer; 
import hello.model.User; 
import hello.mongodao.CustomerRepository; 
import hello.mysqldao.UserRepository; 

@EnableMongoRepositories(basePackageClasses = CustomerRepository.class) 
@EnableJpaRepositories (basePackageClasses = UserRepository.class) 
@SpringBootApplication 
public class Application implements CommandLineRunner { 

    @Autowired 
    private CustomerRepository repository; 

    @Autowired 
    private UserRepository userRepository; 

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

     System.out.println("getting data from Mongo"); 
     repository.deleteAll(); 

     // save a couple of customers 
     repository.save(new Customer("Alice", "Smith")); 
     repository.save(new Customer("Bob", "Smith")); 

     // fetch all customers 
     System.out.println("Customers found with findAll():"); 
     System.out.println("-------------------------------"); 
     for (Customer customer : repository.findAll()) { 
      System.out.println(customer); 
     } 
     System.out.println(); 

     // fetch an individual customer 
     System.out.println("Customer found with findByFirstName('Alice'):"); 
     System.out.println("--------------------------------"); 
     System.out.println(repository.findByFirstName("Alice")); 

     System.out.println("Customers found with findByLastName('Smith'):"); 
     System.out.println("--------------------------------"); 
     for (Customer customer : repository.findByLastName("Smith")) { 
      System.out.println(customer); 
     } 

     System.out.println("gettting data from mysql"); 
     userRepository.deleteAll(); 

     // save a couple of customers 
     userRepository.save(new User("Alice", "[email protected]")); 
     userRepository.save(new User("Bob", "[email protected]")); 

     // fetch all customers 
     System.out.println("Users found with findAll():"); 
     System.out.println("-------------------------------"); 

     for (User user : userRepository.findAll()) { 
      System.out.println(user); 
     } 

    } 
} 

兩個樣品CustomerRepository.java

package hello.mongodao; 

import java.util.List; 

import org.springframework.data.mongodb.repository.MongoRepository; 

import hello.model.Customer; 

public interface CustomerRepository extends MongoRepository<Customer, String> { 

    public Customer findByFirstName(String firstName); 
    public List<Customer> findByLastName(String lastName); 

} 

UserRepository.java

package hello.mysqldao; 

import org.springframework.data.repository.CrudRepository; 

import hello.model.User; 

// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository 
// CRUD refers Create, Read, Update, Delete 

public interface UserRepository extends CrudRepository<User, Long> { 

} 

Customer.java

package hello.model; 

import org.springframework.data.annotation.Id; 


public class Customer { 

    @Id 
    public String id; 

    public String firstName; 
    public String lastName; 

    public Customer() {} 

    public Customer(String firstName, String lastName) { 
     this.firstName = firstName; 
     this.lastName = lastName; 
    } 

    @Override 
    public String toString() { 
     return String.format(
       "Customer[id=%s, firstName='%s', lastName='%s']", 
       id, firstName, lastName); 
    } 

} 

User.java

package hello.model; 

import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 

@Entity // This tells Hibernate to make a table out of this class 
public class User { 
    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    private Integer id; 

    private String name; 

    private String email; 
public User() { 
    // TODO Auto-generated constructor stub 
} 
    public User(String string, String string2) { 
     // TODO Auto-generated constructor stub 
     name = string; 
     email = string2; 
    } 

    public Integer getId() { 
     return id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    @Override 
    public String toString() { 
     return String.format(
       "User[id=%s, name='%s', email='%s']", 
       id, name, email); 
    } 


} 

application.properties

spring.jpa.hibernate.ddl-auto=create 
spring.datasource.url=jdbc:mysql://localhost:3306/db_example 
spring.datasource.username=springuser 
spring.datasource.password=ThePassword 
spring.data.mongodb.uri=mongodb://localhost:27017/local 
+0

我想在我的application.properties中使用兩個mongodb數據庫:'spring.data.mongodb.uri = mongodb:// username:password @ localhost:27017/databasename',當我想使用另一個mongodb數據庫時,我應該添加另一個相同的application.properties?以及我在使用時如何區分它? – Sucy

0
#first db 
spring.datasource.url = [url] 
spring.datasource.username = [username] 
spring.datasource.password = [password] 
spring.datasource.driverClassName = oracle.jdbc.OracleDriver 

#second db ... 
spring.secondDatasource.url = [url] 
spring.secondDatasource.username = [username] 
spring.secondDatasource.password = [password] 
spring.secondDatasource.driverClassName = oracle.jdbc.OracleDriver 


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

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

當我必須把這個配置Bean?