i am new to spring boot. My requirement is to use both mysql and mongo database. For some transaction related operation i will use mysql and for fetching purpose i will store/retrieve the data to mongo. I went through a lots of exampe, now i am completely confused. i want some structure like JPArepository, where i can use the inbuilt methods .e.g
public interface CustomerRepository extends MongoRepository<Customer, String> {}
and
public interface UserRepository extends CrudRepository<User, Long> { }
Just mention the connection and that will point to the right database. i have lots of tables in both the database. A sample example will also be helpful how to write queries using the different connections.
答案 0 :(得分:0)
这是一个例子 @EnableJpaRepositories
使用basePackages指定要扫描带注释组件的包。
使用em.setPackagesToScan设置是否对类路径中的实体类使用基于Spring的扫描。
让我们在代码之后创建mysql和mongo配置类。
对于MySQL
@Configuration
@EnableJpaRepositories(basePackages = {"yourpackage.mysql.repositories"},
entityManagerFactoryRef = "mysqlEntityManagerFactory",
transactionManagerRef = "mysqlTransactionManager")
@EnableTransactionManagement
public class mysqlConfig {
@Bean(name = "mysqlEntityManagerFactory")
@Primary
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSourceWrite());
em.setPackagesToScan(new String[]{"yourpackage.mysql.entities"});
em.setPersistenceUnitName("trueid");
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
@Bean(name = "dataSourceMysql",destroyMethod = "close")
public DataSource dataSource() {
...
return dataSource;
}
@Bean(name = "mysqlTransactionManager")
public PlatformTransactionManager transactionManager(@Qualifier(value = "entityManagerFactory") EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}
@Bean(name = "mysqlExceptionTranslation")
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", ...);
properties.setProperty("hibernate.show_sql", "false");
return properties;
}
}
对于Mongo
@Configuration
@EnableJpaRepositories(basePackages = {"yourpackage.mongo.repositories"},
entityManagerFactoryRef = "mongoEntityManagerFactory",
transactionManagerRef = "mongoTransactionManager")
@EnableTransactionManagement
public class mongoConfig {
@Bean(name = "mongoEntityManagerFactory")
@Primary
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSourceWrite());
em.setPackagesToScan(new String[]{"yourpackage.mongo.entities"});
em.setPersistenceUnitName("trueid");
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
@Bean(name = "dataSourceMongo",destroyMethod = "close")
public DataSource dataSource() {
...
return dataSource;
}
@Bean(name = "mongoTransactionManager")
public PlatformTransactionManager transactionManager(@Qualifier(value = "mongoEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}
@Bean(name = "mongoExceptionTranslation")
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", ...);
properties.setProperty("hibernate.show_sql", "false");
return properties;
}
}
实体和存储库类
我希望这会有所帮助