自己下载neo4j数据库后:
启动后,在springboot项目集成.
pom文件
org.springframework.boot spring-boot-starter-parent2.1.4.RELEASE com.bjsxt shiro_01_demo1.0-SNAPSHOT 1.8 org.springframework.boot spring-boot-starter-data-neo4jorg.springframework.boot spring-boot-starter-testtest org.projectlombok lombokorg.springframework.boot spring-boot-starter-weborg.neo4j neo4j-ogm-http-driver3.2.1 org.assertj assertj-coreorg.apache.commons commons-lang3org.apache.commons commons-collections44.1 application.yml中: org.springframework.boot spring-boot-maven-plugin
server:
port: 18016
max-http-header-size: 8192
spring:
application:
name: spring-boot-neo4j
neo4j: # neo4j配置
uri: bolt://127.0.0.1:7687
username: neo4j
password: 123456
如果出现以下报错:
org.springframework.transaction.CannotCreateTransactionException: Could not open Neo4j Session for transaction; nested exception is org.neo4j.driver.v1.exceptions.AuthenticationException: Unsupported authentication token, scheme 'none' is only allowed when auth is disabled.
在启动类里面添加:
@Bean
public org.neo4j.ogm.config.Configuration getConfiguration() {
return new org.neo4j.ogm.config.Configuration.Builder().credentials("neo4j", "123456").uri("bolt://localhost:7687").build();
entity对象:
@Data
//节点的属性
@NodeEntity(label="person")
@NoArgsConstructor
public class Person implements Serializable {
@Id
@GeneratedValue
private Long id;
@Property
private String name;
}
dao层:
@Repository public interface PersonRepository extends Neo4jRepository{ @Query("match (n:person{name:{name}}) return n LIMIT 1") Person queryPersonByName(@Param("name") String name); @Query("Match (p:person) -[*]->(s:person) where id(p)={0} return s") List findChildList(Long pId); }
service层:
@org.springframework.stereotype.Service
@Slf4j
public class Neo4jServiceImpl implements Neo4jService {
@Autowired
private PersonRepository personRepository;
@Autowired
private PersonRelationRepository personRelationRepository;
@Autowired
private StarRelationRepository starRelationRepository;
@Override
public Person queryPerson() {
Person person = personRepository.findById(4876L).get();
return person;
}
@Override
public List queryAllPerson() {
Iterable lists = personRepository.findAll();
//Iterable转换为List
List persons = IterableUtils.toList(lists);
return persons;
}
@Override
public void savePerson() {
try {
Person person = new Person();
person.setName("蔡情原");
personRepository.save(person);
} catch (Exception e) {
log.error("neo4J保存person失败:{}", e);
}
}
@Override
public Person queryPersonByName(Person person) throws Exception {
//Optional判空检验
Optional.ofNullable(person).orElseThrow(() -> new Exception("传入对象为空"));
//根据名称/id查询
if (StringUtils.isNotEmpty(person.getName())) {
return personRepository.queryPersonByName(person.getName());
} else {
return personRepository.findChildList(person.getId()).get(0);
}
}
@Override
public void createRelation() {
personRelationRepository.createRelation("狮驼王", "brother", "蛟魔王");
}
@Override
public List queryParentNode(String parentNode) {
return personRelationRepository.queryParentNode(parentNode);
}
@Override
public List createRelationship() {
return starRelationRepository.createRelationship();
}
}
controller层:
@RestController
public class Neo4jController {
@Autowired
private Neo4jService neo4jService;
@RequestMapping(value = "queryPersonById", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
public Person queryPerson() {
return neo4jService.queryPerson();
}
@RequestMapping(value = "queryAllPerson", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
public List queryAllPerson() {
return neo4jService.queryAllPerson();
}
@RequestMapping(value = "savePerson", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
public void savePerson() {
neo4jService.savePerson();
}
@RequestMapping(value = "queryPersonByName", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
public Person queryPersonByName(@RequestBody Person person) throws Exception {
return neo4jService.queryPersonByName(person);
}
@RequestMapping(value = "createRelation", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
public void createRelation() {
neo4jService.createRelation();
}
@RequestMapping(value = "queryParentNode", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
public List queryParentNode(@RequestParam String parentNode) {
return neo4jService.queryParentNode(parentNode);
}
@RequestMapping(value = "createRelationship", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
public List createRelationship() {
return neo4jService.createRelationship();
}
}
postman测试结果:



