mysql - 如何在单个 spring-boot 应用程序中使用 MySQL 和 neo4j

标签 mysql neo4j spring-boot

我已经使用 spring-boot 和 neo4j 创建了应用程序。以下是使用 neo4j 的 spring boot 应用程序

@Configuration
@ComponentScan({ "myproject" })
@EnableAutoConfiguration
@EnableNeo4jRepositories(basePackages = "myproject")
public class Neo4jServer extends Neo4jConfiguration implements CommandLineRunner
{

  public Server()
  {
      setBasePackage("myproject");
  }

  @Bean
  SpringRestGraphDatabase graphDatabaseService()
  {
      return new SpringRestGraphDatabase("http://localhost:7474/db/data");
  }

  public void run(String... args) throws Exception
  {
  }

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

}

下面是带有 mysql 的 spring boot 应用程序

@Configuration
@ComponentScan({ "myproject" })
@EnableAutoConfiguration
@EnableJpaRepositories(basePackages = "myproject.persistence")
@PropertySource("myproject.properties")
public class MySQLServer extends DataSourceAutoConfiguration implements CommandLineRunner
{
  public void run(String... args) throws Exception
  {
  }

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

}

我已经在资源包中创建了属性文件。

spring.datasource.url=jdbc:mysql://localhost:3306/myDB
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver

所以,现在有两个应用程序,一个是用 neo4j,即 Neo4jServer.java,一个是用 MySQL,即 MySQLServer.java 如何在单个应用程序中同时使用两者。 如果错了请让我更正。 谢谢:)

最佳答案

需要注入(inject) MySQL 配置的 DataSource/TransactionManager bean 到应用程序类。 MySQL 实体/DAONeo4j 节点/关系/DAO 需要在不同的包中。然后您可以提供这些相应的包以扫描到 MySQLNeo4j

以下代码显示配置:

@Configuration
@ComponentScan({ "myproject" })
@EnableJpaRepositories(basePackages = "myproject.persistence.mysql")
@EnableAutoConfiguration
@EnableNeo4jRepositories(basePackages = "myproject.persistence.neo4j")
public class Application extends Neo4jConfiguration
{
    @Autowired
    LocalContainerEntityManagerFactoryBean entityManagerFactory;

    public Application()
    {
        setBasePackage("myproject.persistence.neo4j");
    }

    @Bean
    SpringRestGraphDatabase graphDatabaseService()
    {
        return new SpringRestGraphDatabase("http://localhost:7474/db/data");
    }

    @Bean(destroyMethod = "close")
    public DataSource dataSource()
    {
        DataSource dataSource = new DataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/dbname");
        dataSource.setUsername("mysqluser");
        dataSource.setPassword("mysqlpassword");
        return dataSource;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() 
    {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactory.setDataSource(dataSource());
        entityManagerFactory.setPackagesToScan("myproject.persistence.mysql");
        entityManagerFactory.setJpaDialect(new HibernateJpaDialect());
        Map<String, String> jpaProperties = new HashMap<String, String>();
        jpaProperties.put("hibernate.connection.charSet", "UTF-8");
        jpaProperties.put("hibernate.ejb.naming_strategy", "org.hibernate.cfg.EJB3NamingStrategy");
        jpaProperties.put("hibernate.bytecode.provider", "javassist");
        jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
        entityManagerFactory.setJpaPropertyMap(jpaProperties);
        entityManagerFactory.setPersistenceProvider(new HibernatePersistence());
        return entityManagerFactory;
    }

    @Override
    @Bean(name = "transactionManager")
    public PlatformTransactionManager neo4jTransactionManager() throws Exception 
    {
        return new ChainedTransactionManager(new JpaTransactionManager(entityManagerFactory.getObject()),
            new JtaTransactionManagerFactoryBean(graphDatabaseService()).getObject());
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() 
    {
        return new PersistenceExceptionTranslationPostProcessor();
    }
}

关于mysql - 如何在单个 spring-boot 应用程序中使用 MySQL 和 neo4j,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26292243/

相关文章:

java - ConstraintValidation 不适用于 Rest Api 请求

spring-boot - Kotlin Springboot 应用程序无法找到 Azure Application Insight 的连接字符串

mysql - 为什么 Master 认为它是 Reboot 的 Slave?

php - drupal CCK 字段中的允许值列表

mysql - SQL 查询从表的一行中提取一些行

javascript - 查找与 Node 列表有关系的 Node

java - 如何在 Heroku 上托管 spring-boot 微服务 dockerized(docker compose) 应用程序

php - 错误 : Undefined class constant 'MYSQL_ATTR_USE_BUFFERED_QUERY'

node.js - Nodejitsu 支持 neo4j 吗?

neo4j - 在 Neo4j 中,我可以强制执行关系+节点值约束吗?