java 整合 neo4j 5.9.0简单使用

由于查到的视频资料都是老版本的neo4j,新版本中并没有之前的那些注解。所以这里简单写几个自学的例子。

大家看下面这个实体类:

package com.example.neo4jtest.entity;

import com.example.neo4jtest.convert.MyConverter;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.data.neo4j.core.convert.ConvertWith;
import org.springframework.data.neo4j.core.schema.*;

import java.util.*;

@EqualsAndHashCode(callSuper = true)
@Data
@Node(labels = {"Person"})
public class Person extends BaseNode{

    @Property(name = "pid")
    private Long pid;
 
    @Property(name = "name")
    private String name;
 
    @Property(name = "biography")
    private String biography;

    @Property(name = "birth")
    //方式1:@DateString(format = "yyyy-MM-dd")
    //方式2:@ConvertWith
    @ConvertWith(converter = MyConverter.class)
    private Date birth;

    @Property(name = "birthplace")
    private String birthplace;

    @Property(name = "death")
    private String death;

    @Relationship(type = "test")
    private List<Person> personList;

    @Relationship(type = "test",direction = Relationship.Direction.OUTGOING)
    private Set<SomeRelation> relationSet = new HashSet<>();

//    @CompositeProperty(converter = MyConverter2.class)
//    private Map<String, Address> address;
}

变化1:
@NodeEntity()注解变成了@Node注解。并且它可以有多个labels
变化2:
删除了@RelationshipEntity注解,变成了@Relationship,type是关系名称,比如

 @Query("match (n:Person{name:$from}),(m:Person{name:$to}) create (n)-[:测试关系{relation:$relationName}]->(m)")

这一段中的 【测试关系】 就是type。
这个注解目前有两种用法:
方式1:关注与当前节点有关系的对象,那么就是

@Relationship(type = "test")
private List<Person> personList;

使用示例:

	/**
     * 查询 拥有test关系的 名称为name的Person所有节点
     *
     * @return
     */
    @Query("match p=(:Person{name:$name})-[r:test] ->() return p")
    Person queryRelationForPersonByName(@Param("name") String name);

在这里插入图片描述
方式2:关注关系+关系对象

	@Relationship(type = "test",direction = Relationship.Direction.OUTGOING)
    private Set<SomeRelation> relationSet = new HashSet<>();

使用示例:

@RelationshipProperties
public class SomeRelation {

    @RelationshipId
    private Long id;

    private Integer num;

    @TargetNode
    private Person targetPerson;

}

在这里插入图片描述
@ConvertWith(converter = MyConverter.class)注解:
自定义转换方式。比如String转Date,或者String转Object(某个自定义实体对象)
示例代码如下:

/**
 * @Author ztc
 * @Description 自定义转换器  实体中属性为Date
 * @Date 2023/7/28 16:13
 */
public class MyConverter implements Neo4jPersistentPropertyConverter<Date> {


    @Override
    public Value write(Date source) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(source);
        return Values.value(format);
    }

    @Override
    public Date read(Value source) {
        String dateString = source.asString();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(dateString);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}

都是简单的东西,只是希望和我一样不会的人少走点弯路。蟹蟹。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值