简体   繁体   English

Spring Boot - 连接到Neo4j和MySQL数据源

[英]Spring Boot - Connect to Neo4j and MySQL datasources

I'm trying to connect to two datasources, MySQL and Neo4j . 我正在尝试连接两个数据源, MySQLNeo4j I tried following this example but I have different versions of dependencies. 我尝试了这个例子,但我有不同版本的依赖项。

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>easy-notes</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>easy-notes</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.M7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <neo4j-ogm.version>3.0.0</neo4j-ogm.version>
        <spring-data-releasetrain.version>Kay-RELEASE</spring-data-releasetrain.version>
    </properties>



    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-neo4j</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>


        <!--                -->
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-ogm-bolt-driver</artifactId>
            <version>${neo4j-ogm.version}</version>
        </dependency>

        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-jdbc-driver</artifactId>
            <version>3.0</version>
        </dependency>
        <!-- -->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

my DatastoresConfiguration class: 我的DatastoresConfiguration类:

@Configuration
@EnableJpaRepositories(basePackages = "com.example.easynotes.repository.relational")
@EnableNeo4jRepositories(basePackages = "com.example.easynotes.repository.graph")
@EnableTransactionManagement
public class DatastoresConfiguration {

    @Bean
    public org.neo4j.ogm.config.Configuration configuration(){
        return new org.neo4j.ogm.config.Configuration.Builder().uri("bolt://127.0.0.1")
                .credentials("neo4j","123456").build();
    }

    @Bean
    public SessionFactory sessionFactory(){
        return new SessionFactory(configuration(), "com.example.easynotes.models.graph");
    }

    @Bean
    public Neo4jTransactionManager neo4jTransactionManager(){
        return new Neo4jTransactionManager(sessionFactory());
    }

    @Bean
    public Session getSession()  {
       return neo4jTransactionManager().getSessionFactory().openSession();
    }


    @Primary
    @Bean(name = "dataSource")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder
                .create()
                .driverClassName("com.mysql.jdbc.Driver")
                .build();
    }


    @Primary
    @Bean
    @Autowired
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactory.setDataSource(dataSource);
        entityManagerFactory.setPackagesToScan("com.example.easynotes.repository.relational");
        entityManagerFactory.setJpaDialect(new HibernateJpaDialect());
        Map<String, String> jpaProperties = new HashMap<>();
        jpaProperties.put("hibernate.connection.charSet", "UTF-8");
        jpaProperties.put("spring.jpa.hibernate.ddl-auto", "none");
        jpaProperties.put("spring.jpa.hibernate.naming-strategy", "org.springframework.boot.orm.jpa.SpringNamingStrategy");
        jpaProperties.put("hibernate.bytecode.provider", "javassist");
        jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
        jpaProperties.put("hibernate.hbm2ddl.auto", "none");
        jpaProperties.put("hibernate.order_inserts", "true");
        jpaProperties.put("hibernate.jdbc.batch_size", "50");

        entityManagerFactory.setJpaPropertyMap(jpaProperties);
        entityManagerFactory.setPersistenceProvider(new HibernatePersistenceProvider());
        return entityManagerFactory;
    }

    @Autowired
    @Primary
    @Bean(name = "mysqlTransactionManager")
    public JpaTransactionManager mysqlTransactionManager(LocalContainerEntityManagerFactoryBean entityManagerFactory)
            throws Exception {
        return new JpaTransactionManager(entityManagerFactory.getObject());
    }


    @Autowired
    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager(Neo4jTransactionManager neo4jTransactionManager,
                                                         JpaTransactionManager mysqlTransactionManager) {
        return new ChainedTransactionManager(
                mysqlTransactionManager,
                neo4jTransactionManager
        );
    }
}

first of all I'm getting an error here 首先,我在这里收到错误

 @Autowired
    @Primary
    @Bean(name = "mysqlTransactionManager")
    public JpaTransactionManager mysqlTransactionManager(LocalContainerEntityManagerFactoryBean entityManagerFactory)
            throws Exception {
        return new JpaTransactionManager(entityManagerFactory.getObject());
    }

Could not autowire, no beans of 'LocalContainerEntityManagerFactoryBean' type found

Full project is on github 完整项目在github上

Change your entityManagerFactory method as below. 更改您的entityManagerFactory方法,如下所示。

    @Primary
    @Bean
    @Autowired
    public EntityManagerFactory entityManagerFactory(DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactory.setDataSource(dataSource);
        entityManagerFactory.setPackagesToScan("com.example.easynotes.repository.relational");
        entityManagerFactory.setJpaDialect(new HibernateJpaDialect());
        Map<String, String> jpaProperties = new HashMap<>();
        jpaProperties.put("hibernate.connection.charSet", "UTF-8");
        jpaProperties.put("spring.jpa.hibernate.ddl-auto", "none");
        jpaProperties.put("spring.jpa.hibernate.naming-strategy", "org.springframework.boot.orm.jpa.SpringNamingStrategy");
        jpaProperties.put("hibernate.bytecode.provider", "javassist");
        jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
        jpaProperties.put("hibernate.hbm2ddl.auto", "none");
        jpaProperties.put("hibernate.order_inserts", "true");
        jpaProperties.put("hibernate.jdbc.batch_size", "50");

        entityManagerFactory.setJpaPropertyMap(jpaProperties);
        entityManagerFactory.setPersistenceProvider(new HibernatePersistenceProvider());

        entityManagerFactory.afterPropertiesSet();
        return (EntityManagerFactory) entityManagerFactory.getObject();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM