2014-10-10 79 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(); 
    } 
} 
相關問題