我们直接使用spring-data jpa saveAll方法保存关系(边)试试
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Neo4jDemoApplication.class)
public class AppTest {
@Autowired
private LoverRepository loverRepository;
@Autowired
private PersonRepository personRepository;
@Test
public void addLoverRelations2() {
// 注意这里是直接new的节点,并不是从Neo中查的节点
Person xm = new Person(1, "小明", "155***");
Person xw = new Person(2, "小伟", "133***");
Person xh = new Person(3, "小红", "188***");
Person xf = new Person(4, "小芳", "186***");
Lover xmLoverxw = new Lover(xm, xw);
Lover xwLoverxh = new Lover(xw, xh);
Lover xhLoverxm = new Lover(xh, xm);
Lover xfLoverxh = new Lover(xf, xh);
Iterable<Lover> saveAll = loverRepository.saveAll(Arrays.asList(xmLoverxw, xwLoverxh, xhLoverxm, xfLoverxh));
saveAll.forEach(System.out::println);
}
结果

image.png

image.png
该如何呢?试试自定义saveAll方法吧
package com.fc.neo4j_demo.dao.repository;
import java.util.List;
import org.springframework.data.neo4j.annotation.Query; // 注意一定需要使用 这个Queru 不是ibatis中的!
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;// 注意Param类
import com.fc.neo4j_demo.entity.neo4j.relation.Lover;
import com.fc.neo4j_demo.entity.neo4j.relation.RelationIds;
// 这里是按userId判断是否是同一节点
public interface LoverRepository extends Neo4jRepository<Lover, Long> {
// 解绑一个集合 变成单个的 item 使用item中的参数做 match merge 节点间的 关系
@Query("UNWIND {lovers} as item MATCH(p1:Person{userId:item.startId}),(p2:Person{userId:item.endId}) MERGE (p1)-[r:Lover]->(p2) return r")
public List<Lover> saveAll(@Param("lovers") List<RelationIds> lovers);
}
public class RelationIds {
private Integer startId;
private Integer endId;
测试一下吧
先把已有的关系删除、重复的节点删除
match (n1) -[r:Lover]-> (n2) delete r;

image.png
match (n:Person:BaseNode) where id(n)>55 delete n;

image.png
测试自定义saveAll方法
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Neo4jDemoApplication.class)
public class AppTest {
@Autowired
private LoverRepository loverRepository;
@Test
public void addLoverRelationsPro() {
List<Lover> saveAll = loverRepository.saveAll(Arrays.asList(new RelationIds(1, 2), new RelationIds(2, 3),
new RelationIds(3, 1), new RelationIds(4, 3)));
saveAll.forEach(System.out::println);
}
结果

image.png

image.png
之前的关系保持,继续新增一个小明Lover小芳的关系
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Neo4jDemoApplication.class)
public class AppTest {
@Autowired
private LoverRepository loverRepository;
@Test
public void addLoverRelationsPro() {
List<Lover> saveAll = loverRepository.saveAll(Arrays.asList(new RelationIds(1, 2), new RelationIds(2, 3),
new RelationIds(3, 1), new RelationIds(4, 3), new RelationIds(1, 4)));//新增小明Lover小芳
saveAll.forEach(System.out::println);
}
结果

image.png

image.png
这里把节点批量merge的方法也发一下吧
public interface PersonRepository extends Neo4jRepository<Person, Long> {
public Person findByUserName(String userName);
@Query("UNWIND {persons} as item merge(p:Person{userId:item.userId,userName:item.userName,userPhone:item.userPhone}) return p")
public List<Person> saveAll(@Param("persons") List<Person> persons);
}
测试
@Test
public void savePersons() {
//创建一模一样的节点
Person xm = new Person(1, "小明", "155***");
Person xw = new Person(2, "小伟", "133***");
Person xh = new Person(3, "小红", "188***");
Person xf = new Person(4, "小芳", "186***");
// 调用自定义的批量merge方法
List<Person> saveAll = personRepository.saveAll(Arrays.asList(xm,xw,xh,xf));
saveAll.forEach(System.out::println);
}
结果

image.png

image.png
存在一个问题!
我改变小明的名字为大明再创建会怎么样呢?如果我只想用userId判断是否重复,其它属性对应更新,又该如何操作呢?
@Test
public void savePersons() {
//改变小明的名字为大明明 其它不变
Person xm = new Person(1, "大明明", "155***");
Person xw = new Person(2, "小伟", "133***");
Person xh = new Person(3, "小红", "188***");
Person xf = new Person(4, "小芳", "186***");
// 调用自定义的批量merge方法
List<Person> saveAll = personRepository.saveAll(Arrays.asList(xm,xw,xh,xf));
saveAll.forEach(System.out::println);
}

image.png
请听下回分解
网友评论