如何在同一项目中连接两个数据库MySQL和MongoDB?可能吗?

当前,我在Spring上使用Hibernate(MySQL),该配置对我来说运行良好,但是一旦我配置了另一个配置mongo-

config.xml文件并尝试使用mongodb运行测试用例,就会 从第一种配置开始。

<context:annotation-config />

<context:component-scan base-package="com.test.mongo" />

<context:property-placeholder location="classpath:mongo-dao.properties" />

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">

<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />

</bean>

<bean id="mongoDbFactory" class="org.springframework.data.mongodb.core.MongoFactoryBean">

<property name="driverClassName" value="${spring.datasource.driverClassName}" />

<property name="host" value="${spring.data.mongodb.host}" />

<property name="port" value="${spring.data.mongodb.port}" />

<property name="databaseName" value="${spring.data.mongodb.database}" />

<context:component-scan base-package="com.hb.dao" />

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="dataSource"

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName" value="${db.jdbc.driverClassName}" />

<property name="url" value="${db.jdbc.url}" />

<property name="username" value="${db.jdbc.username}" />

<property name="password" value="${db.jdbc.password}" />

</bean>

<bean id="sessionFactory"

class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

<property name="packagesToScan">

<list>

<value>com.hb..dao.domain.entity</value>

</list>

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">${hibernate.dialect}</prop>

<prop key="hibernate.show_sql">${hibernate.show_sql:false}</prop>

<prop key="hibernate.format_sql">${hibernate.format_sql:false}</prop>

</props>

</property>

</bean>

<bean id="transactionManager"

class="org.springframework.orm.hibernate4.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory" />

</bean>

java.lang.IllegalStateException: Failed to load ApplicationContext

at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)

at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:101)

at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)

at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)

at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:331)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:213)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:290)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

依赖注释:

    public class MongoQuestionsTest extends BaseDaoMongoTest{   

private static final Logger logger = LogManager.getLogger(MongoQuestionsTest.class);

@Autowired

private MongoTestDao mongoTestDaoImpl;

@Test

public void saveQuestions(){

MongoQuestions mongoQuestions = new MongoQuestions();

mongoQuestions.setUsername("Hi");

mongoQuestions.setPassword("Hello");

mongoTestDaoImpl.save(mongoQuestions);

logger.debug("Mongo User Set with id " + mongoQuestions.getId());

}

and **BaseDaoMongoTest**---

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations={"classpath:/mongo-config-test.xml"})

public class BaseDaoMongoTest {

}

在MongoTestDaoImpl类中,我只是自动连接MongoTemplate并调用了save()方法。

回答:

我不确定这是否是最佳解决方案。但是,它对我有用。如果您有两个使用Jpa模块的关系数据库,那么我建议您创建entitytransaction管理Bean来读取每个数据源配置。

当你希望有一个组合SQLNoSQL,我会创造entitytranscation对经理豆类MySQL数据库,因为它与运作良好Jpa。并保持原样配置Mongo(直接从读取配置application.properties)。

@Configuration

@PropertySource("classpath:persistence-multiple-db.properties")

@EnableJpaRepositories(basePackages = "com.springdata.dao.mysql", entityManagerFactoryRef = "mysqlEntityManager", transactionManagerRef = "mysqlTransactionManager")

public class MySQLConfiguration {

@Autowired

private Environment env;

@Bean

@Primary

public LocalContainerEntityManagerFactoryBean mysqlEntityManager() {

LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();

em.setDataSource(myMySQLDataSource());

em.setPackagesToScan(new String[] { "com.springdata.models" });

HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();

em.setJpaVendorAdapter(vendorAdapter);

HashMap<String, Object> properties = new HashMap<String, Object>();

properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));

em.setJpaPropertyMap(properties);

return em;

}

@Bean

@Primary

public DataSource myMySQLDataSource() {

DriverManagerDataSource dataSource = new DriverManagerDataSource();

dataSource.setDriverClassName(env.getProperty("spring.mysql.jdbc.driverClassName"));

dataSource.setUrl(env.getProperty("spring.mysql.jdbc.url"));

dataSource.setUsername(env.getProperty("spring.mysql.user"));

dataSource.setPassword(env.getProperty("spring.mysql.pass"));

return dataSource;

}

@Bean

@Primary

public PlatformTransactionManager mysqlTransactionManager() {

JpaTransactionManager transactionManager = new JpaTransactionManager();

transactionManager.setEntityManagerFactory(mysqlEntityManager().getObject());

return transactionManager;

}

上面的数据源配置参数是从classpath:persistence-multiple-db.properties类路径中的文件中读取的。

# mysql jdbc connections

spring.mysql.jdbc.driverClassName=com.mysql.jdbc.Driver

spring.mysql.jdbc.url=jdbc:mysql://localhost:3306/test?autoReconnect=true&useSSL=false

spring.mysql.user=root

spring.mysql.pass=password1

# hibernate.X

hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

以上配置足以处理MySQL数据源。要在项目中进行mongo配置,请将以下行添加到中application.properties

# mongo configuration

spring.data.mongodb.uri=mongodb://localhost

spring.data.mongodb.database=test

Springboot将自动创建必要的mongo数据源bean,并使它们随时可供Spring容器使用。

现在,为MySQL和Mongo数据源创建存储库接口。

@Transactional

public interface MyMongoRepository extends MongoRepository<Users, String>{

}

@Transactional

public interface MySQLRepository extends JpaRepository<Users, String>{

}

@Entity

@Table(name = "users")

@Document(collection="users")

@Data

public class Users {

@Id

@javax.persistence.Id

private String id;

private String name;

private Integer age;

}

添加了以下注释以启用mongorepositoreis并将组件扫描到springboot主类。

@SpringBootApplication

@ComponentScan(basePackages = { "com.springdata" })

@EnableMongoRepositories(basePackages={"com.springdata.dao.mongo"})

public class SpringbootmysqlmongoApplication {

public static void main(String[] args) {

SpringApplication.run(SpringbootmysqlmongoApplication.class, args);

}

}

最后,要测试一些代码。

@Service

public class MyRepositoryImpl {

@Autowired

private MyMongoRepository myMongoRepository;

@Autowired

private MySQLRepository mySQLRepository;

@PostConstruct

public void extractUsers(){

myMongoRepository.findAll().forEach((user) -> System.out.println("user name from mongo is : "+user.getName()));

mySQLRepository.findAll().forEach((user) -> System.out.println("User name from mysql is : "+user.getName()));

}

}

usersmysql test数据库中创建了表并在数据库中进行了users收集mongo test

最后,已将我的代码上传到git存储库。

https://github.com/harshavmb/springbootmysqlmongo

以上是 如何在同一项目中连接两个数据库MySQL和MongoDB?可能吗? 的全部内容, 来源链接: utcz.com/qa/413360.html

回到顶部