2014-10-10 91 views
1

我已经使用spring-boot和neo4j创建了应用程序。以下是Neo4j的如何在单弹簧引导应用程序中使用MySQL和neo4j

@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

@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 如何既单一应用程序中使用。 如果错了,请让我纠正。 谢谢:)

+0

创建属性文件的MySQL数据库性能。但不知道该怎么做? – 2014-10-10 06:55:35

+1

查看http://spring.io/guides/gs/accessing-data-jpa/ – geoand 2014-10-10 07:01:52

+0

以下是如何在引导应用程序中使用neo4j-jdbc:https://github.com/neo4j-contrib/developer- resources/tree/gh-pages/language-guides/java/spring-boot-jdbc – 2014-10-10 15:02:31

回答

1

需要注入MySQL配置DataSource/TransactionManager bean到应用程序类。 MySQLEntities/DAONeo4jNode/Relationship/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(); 
    } 
} 
相关问题