package org.canaan.neo4j.graph; import org.canaan.neo4j.graph.entity.UserNode; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.Date; import java.util.Optional; /** * @author Canaan * @date 2019/7/11 10:31 */ @RunWith(SpringRunner.class) @SpringBootTest public class UserNeo4jDaoTest { private static final Logger LOGGER = LoggerFactory.getLogger(UserNeo4jDaoTest.class); @Autowired private UserNeo4jDao userNeo4jDao; @Test public void queryUser() { Optional<UserNode> userNode = userNeo4jDao.findByUserId(4L); System.out.println(userNode); } /** * 若是不存在就建立,存在就更新屬性,不會更新【標籤】 * * @author Canaan * @date 2019/7/11 12:59 */ @Test public void addOrUpdateUser() { UserNode user1 = new UserNode(); user1.setUserId(1L); user1.setUserName("晃蓋"); //user1.setLabels(Collections.singleton(UserNode.LABEL_FOUNDER)); user1.setCreateTime(new Date()); user1 = userNeo4jDao.save(user1); LOGGER.info("{}", user1); //---- UserNode user2 = new UserNode(); user2.setUserId(2L); user2.setUserName("宋江"); //user2.setLabels(Collections.singleton(UserNode.LABEL_PLOUGH)); user2.setCreateTime(new Date()); user2 = userNeo4jDao.save(user2); LOGGER.info("{}", user2); } /** * 獲取已經存在的節點, 建立新的節點並關係 * * @author Canaan * @date 2019/7/11 14:16 */ @Test public void addNodeAdnRelation() { UserNode boss = this.userNeo4jDao.findById(2L).orElseThrow(RuntimeException::new); UserNode subUser1 = new UserNode(); subUser1.setUserId(3L); subUser1.setUserName("盧俊義"); //subUser1.setLabels(Collections.singleton(UserNode.LABEL_PLOUGH)); subUser1.setCreateTime(new Date()); UserNode subUser2 = new UserNode(); subUser2.setUserId(4L); subUser2.setUserName("吳用"); //subUser2.setLabels(Collections.singleton(UserNode.LABEL_PLOUGH)); subUser2.setCreateTime(new Date()); subUser2.setParentNode(boss); subUser1.setParentNode(boss); userNeo4jDao.save(subUser2); userNeo4jDao.save(subUser1); } /** * 若是節點不存在就建立,若是關係不存在就建立。不然只更新節點 * * @author Canaan * @date 2019/7/11 14:35 */ @Test public void addNodeAdnRelation2() { UserNode subUser1 = new UserNode(); subUser1.setUserId(4L); subUser1.setUserName("吳用"); //subUser1.setLabels(Collections.singleton(UserNode.LABEL_PLOUGH)); subUser1.setCreateTime(new Date()); UserNode subUser2 = new UserNode(); subUser2.setUserId(6L); subUser2.setUserName("燕青"); //subUser2.setLabels(Collections.singleton(UserNode.LABEL_SONUZ)); subUser2.setCreateTime(new Date()); subUser2.setParentNode(subUser1); userNeo4jDao.save(subUser2); } /** * 更新關係 * * @author Canaan * @date 2019/7/11 14:47 */ @Test public void updateRelation() { //userNeo4jDao.save(node); 或 userNeo4jDao.save(node,-1); //關係不限制的添加 //userNeo4jDao.save(node,0); //更新節點時,不更新關係 //userNeo4jDao.save(node, 1); //更新節點時,關係只維護1級 } /** * 在已經存在的兩個節點中添加關係。若是關係存在就不處理,不存在就添加 * * @author Canaan * @date 2019/7/11 13:08 */ @Test public void addRelation1() { UserNode u1 = this.userNeo4jDao.findById(1L).orElseThrow(RuntimeException::new); UserNode u2 = this.userNeo4jDao.findById(2L).orElseThrow(RuntimeException::new); u2.setParentNode(u1); this.userNeo4jDao.save(u2); } /** * 刪除節點,若是節點有關係的話,那麼同時刪除關係 * * @author Canaan * @date 2019/7/11 13:08 */ @Test public void deleteNodeAndRelation() { this.userNeo4jDao.deleteById(6L); } /** * 刪除關係 * * @author Canaan * @date 2019/7/11 14:37 */ @Test public void getNode() { Optional<UserNode> userNode; // 查詢指定節點 userNode = this.userNeo4jDao.findById(6L, 0); //查詢指定節點,及1跳關係 //userNode = this.userNeo4jDao.findById(6L); //userNode = this.userNeo4jDao.findById(6L, 1); // 注意: depth 必須大於 1 纔會級聯查詢 System.out.println(userNode.orElse(null)); } }
分頁查詢:html
事務:node