我和春天有启动应用程序的问题。我想MongoDB数据库和MySQL数据库在我的春节,启动应用程序连接。我想知道,如果可能的话,在积极的情况下,我怎样才能让这多个连接。我做了基于没有成功与MySQL和张贴的例子一试。所以我想知道是否有人有一个简单的例子来了解这种方法。 感谢春季启动连接MySQL和MongoDB
1
A
回答
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
#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?
相关问题
- 1. 春季启动MYSQL连接
- 2. 春季启动mysql睡眠连接
- 3. 春季启动:用MongoDB的
- 4. 与多个数据源连接到mysql和mongodb的春季启动
- 5. MongoDB的连接不是在春季启动封闭得到
- 6. 春季启动1.5.1 MongoDB的连接错误
- 7. 从春季启动应用程序连接到MongoDB使用ssl
- 8. 春季启动MongoDB的连接 - 凭证与URI
- 9. 春季启动连接池的理解
- 10. 春季启动AMQP接收
- 11. 我的春季启动项目连接到MySQL数据库
- 12. 春季启动mongodb xml配置
- 13. 春季启动MongoDB的审计错误
- 14. 春季启动OAuth2和UserDetails
- 15. Hazelcast和春季启动
- 16. 春季启动“CommandLineRunner”和json
- 17. 防止春季启动启动失败的couchbase连接错误
- 18. 配置问题与春季启动OpenId连接和Gluu
- 19. 春季启动 - 启动
- 20. 春季安全春季启动4.x
- 21. 春季连接池
- 22. 在春季启动
- 23. 在春季启动
- 24. SimpMessagingTemplate春季启动
- 25. 春季启动HttpMediaTypeNotSupportedException
- 26. 春季启动OAuth2
- 27. 春季启动ApplicationPidListener
- 28. 春季启动:为
- 29. 在春季启动
- 30. 春季启动 - 从
我想在我的application.properties中使用两个mongodb数据库:'spring.data.mongodb.uri = mongodb:// username:password @ localhost:27017/databasename',当我想使用另一个mongodb数据库时,我应该添加另一个相同的application.properties?以及我在使用时如何区分它? – Sucy