纯Spring方式使用spring-data-neo4j

笔者以spring-data-neo4j-5.0.5为例,侧重使用纯spring的方式使用neo4j实现图库的增删改查。

spring-boot的方式网上资料很多,不是笔者今天的关注点,因为大部分工作spring-boot都做好了,开发者要做的很少。

这里简单使用一个domain,和一个repository模拟一个节点插入的场景。

节点定义

AssetNodeNeo.java

@NodeEntity
@Getter
@Setter
@RequiredArgsConstructor
public class AssetNodeNeo {
    @Id
    @GeneratedValue
    private Long id;
    private Long assetId;
    private String logicName;
    private String physicsName;
    private Long storageId;
    private long modifyTime;
    
    public AssetNodeNeo(Long assetId, String logicName, String physicsName, Long storageId, long modifyTime) {
        this.assetId = assetId;
        this.logicName = logicName;
        this.physicsName = physicsName;
        this.storageId = storageId;
        this.modifyTime = modifyTime;
    }
}

持久层定义

AssetNodeRepository.java

public interface AssetNodeRepository extends Neo4jRepository<AssetNodeNeo,Long>,CustomizedRepository {
}

这里继承Neo4jRepository,既spring-data-neo4j-4.x的GraphRepository,对增删改查做好了封装,相当于关系型的jdbcTemplate。

逻辑层定义

TestService.java

@Service
public class TestService {
    @Autowired
    private AssetNodeRepository assetNodeRepository;
    public void createNode() {
        AssetNodeNeo a1 = new AssetNodeNeo(257L, "a1", "a1", 6L,
            System.currentTimeMillis());
        assetNodeRepository.save(a1);
    }
}

笔者模拟的逻辑很简单,加载spring容器,然后使用TestService将a1节点插入图库中。
下面分别使用java config和xml两种方式来实现:

java config方式运行

neo4j配置:

@Configuration
@ComponentScan(basePackages = {"com.yq.service"})
@EnableNeo4jRepositories(basePackages = "com.yq.repository")
public class Neo4jConfiguration {

    @Bean
    public org.neo4j.ogm.config.Configuration configuration() {
        Neo4jProperties properties = new Neo4jProperties();
        properties.setUri("bolt://localhost:7688");
        properties.setUsername("neo4j");
        properties.setPassword("123123");
        org.neo4j.ogm.config.Configuration configuration = properties.createConfiguration();
        return configuration;
    }

    @Bean
    public SessionFactory sessionFactory(org.neo4j.ogm.config.Configuration configuration) {
        return new SessionFactory(configuration, "com.yq.domain");
    }

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

@Configuration 生命这是一个spring配置类
@ComponentScan(basePackages = {“com.yq.service”}) 扫描逻辑层的所有service
@EnableNeo4jRepositories(basePackages = “com.yq.repository”) 扫描neo4j的持久层
分别声明了session工厂、事物管理器等3个bean,和官网类似

启动类:

public class SpringApplication {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Neo4jConfiguration.class);

        TestService testService = context.getBean(TestService.class);
        testService.createNode();
        context.stop();
    }
}

xml方式运行

xml配置:

spring.xml


<beans xmlns:context="http://www.springframework.org/schema/context"
    xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/data/neo4j
        http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd">

    <context:component-scan base-package="com.yq"/>

    <bean id="sessionFactory" class="com.yq.conf.Neo4jSessionFactory" factory-method="getSessionFactory">
        <constructor-arg name="uri" value="bolt://localhost:7688"/>
        <constructor-arg name="username" value="neo4j"/>
        <constructor-arg name="password" value="123123"/>
        <constructor-arg name="domainPackage" value="com.yq.domain"/>
    bean>

    <bean id="transactionManager" class="org.springframework.data.neo4j.transaction.Neo4jTransactionManager">
        <constructor-arg ref="sessionFactory"/>
    bean>

    <neo4j:repositories base-package="com.yq.repository"/>
beans>

扫描业务层
扫描neo4j持久层
分别声明了session工厂、事物管理器2个bean,和java config方式类似

由于org.neo4j.ogm.session.SessionFactory构造方法入参比较复杂,这里笔者封装了Neo4jSessionFactory静态工厂方法用于构造sessionFactory对象,具体如下:
Neo4jSessionFactory.java

@Setter
public class Neo4jSessionFactory {

    /**
     * Create instance of  {@link SessionFactory}.
     *
     * @param uri URI used by the driver. Auto-detected by default.
     * @param username Login user of the server.
     * @param password Login password of the server.
     * @param domainPackage The packages to scan for domain objects.
     */
    public static SessionFactory getSessionFactory(String uri, String username, String password, String domainPackage) {
        Neo4jProperties properties = new Neo4jProperties();
        properties.setUri(uri);
        properties.setUsername(username);
        properties.setPassword(password);
        org.neo4j.ogm.config.Configuration configuration = properties.createConfiguration();
        return new SessionFactory(configuration, domainPackage);
    }
}

启动类:

public class SpringApplication {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "classpath:spring.xml");

        TestService testService = context.getBean(TestService.class);
        testService.createNode();
        context.stop();
    }
}

上面两种方式经测试皆可行,执行后,查看图库:
纯Spring方式使用spring-data-neo4j_第1张图片


参考文档:
spring-data-neo4j

你可能感兴趣的:(spring)