我有一个Spring Boot应用程序的问题。我想在我的Spring Boot应用程序中连接一个MongoDB数据库和一个MySql数据库。我想知道这是不是可能的,在肯定的情况下,我如何能使这种多重连接。我尝试了一个基于Mysql和Post的示例,但没有成功。所以我想知道是否有人有一个简单的例子来知道这个方法。谢谢
这样做是可能的,您将为不同的数据源创建不同的配置。这个链接在http://www.baeldung.com/spring-data-jpa-multiple-databases上有很好例子
另一个有用的stackoverflow问题:Spring Boot配置并使用两个数据源
要开始使用mongo和mysql,可以参考Spring.io指南中的示例。
我创建了这个示例,合并了上面的两个示例
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", "Alice@Smith.com"));
userRepository.save(new User("Bob", "Bob@Smith.com"));
// 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> {
}
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);
}
}
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);
}
}
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
我拉出了mysql docker映像,并用命令运行容器: 此时,springBoot在本地工作。它与MySQL连接: 现在,我希望springBoot应用程序也在单独的docker容器上,在同一服务器上。为此,我使用了DockerFile: 同样,它无法连接到数据库:
本文向大家介绍springboot配置mysql连接的实例代码,包括了springboot配置mysql连接的实例代码的使用技巧和注意事项,需要的朋友参考一下 一:导入pmo.xm配置包 mysql库连接、druid连接池、mybatis组件 配置扫描文件 二:application.yml文件配置 三:编写dao层接口 使用注解:@Mapper 四:编写xml文件sql语句 到此这篇关于spri
我正在尝试从Spring Boot应用程序连接到mySQL数据库。然而,当我试图运行它时,它显示出错误。 我如何解决这个问题? 错误 从我的文件中添加代码片段 pom。xml 应用属性 堆栈跟踪 我还没有在sql中手动创建表,因为我认为spring.jpa.hibernate.ddl-Auto=date应该这样做
如果问题模棱两可,请道歉。
我想连接payara服务器和MySQL服务器。 我的所作所为? 我为MySQL下载了Connector/J。 我把它放在C:\users\someuser\desktop\payara5\glassfish\domains\domain1\lib\ext中 我启动了payara服务器。 我在localhost:4848上创建了连接池。 我试着ping但每次都出错。 详情: > Payara版本:5