我用spring-boot和neo4j创建了应用程序。以下是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的 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 如何在单个应用程序中使用它们。 如果错误,请让我纠正。 谢谢:))
答案 0 :(得分:1)
需要将MySQL
配置的DataSource
/ TransactionManager
bean注入应用程序类。 MySQL
Entities
/ DAO
和Neo4j
Node
/ Relationship
/ DAO
需要位于不同的包中。然后,您可以将这些相应的软件包提供给MySQL
和Neo4j
。
以下代码显示配置:
@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();
}
}