当前Neo4j版本:community-3.5.26
,SpringBoot版本:2.1.18.RELEASE
当前内容为使用SpringBoot连接Neo4j和使用SpringBoot-data方式操作Neo4j,当前内容主要用于本人学习和记录
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.18.RELEASEversion>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-neo4jartifactId>
dependency>
dependencies>
import java.util.LinkedHashMap;
import java.util.Map;
import org.neo4j.driver.internal.value.MapValue;
import org.neo4j.driver.internal.value.StringValue;
import org.neo4j.driver.v1.AuthTokens;
import org.neo4j.driver.v1.Driver;
import org.neo4j.driver.v1.GraphDatabase;
import org.neo4j.driver.v1.Session;
import org.neo4j.driver.v1.StatementResult;
import org.neo4j.driver.v1.Transaction;
import org.neo4j.driver.v1.TransactionWork;
import org.neo4j.driver.v1.Value;
/**
* @author admin
* @createTime 2021-02-27 10:55:01
* @description 一个官方的demo的测试操作
*
*/
public class HelloWorldExample implements AutoCloseable {
private final Driver driver;
public HelloWorldExample(String uri, String user, String password) {
// 创建驱动
driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password));
}
@Override
public void close() throws Exception {
// 关闭驱动
driver.close();
}
public void printGreeting(final String message) {
// 尝试在会话中实现操作
try (Session session = driver.session()) {
Map<String, Value> map = new LinkedHashMap<String, Value>();
map.put("message", new StringValue(message));
MapValue mapValue = new MapValue(map);
// 表示使用写事务方式开始执行操作
String greeting = session.writeTransaction(new TransactionWork<String>() {
@Override
public String execute(Transaction tx) {
StatementResult result = tx.run("CREATE (a:Greeting) " + "SET a.message = $message "
+ "RETURN a.message + ', from node ' + id(a)", mapValue);
return result.single().get(0).asString();
}
});
System.out.println(greeting);
}
}
public static void main(String... args) throws Exception {
try (HelloWorldExample greeter = new HelloWorldExample("bolt://192.168.1.100:7687", "neo4j", "123456")) {
greeter.printGreeting("hello, world");
}
}
}
上面这个demo是官方文档上面的demo,可以执行成功!
1. 主要配置类:Neo4jConfig .java
(这个类中要具有连接Neo4j的作用,主要通过打开连接Session工厂)
import org.neo4j.ogm.config.ClasspathConfigurationSource;
import org.neo4j.ogm.config.ConfigurationSource;
import org.neo4j.ogm.session.SessionFactory;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
import org.springframework.data.neo4j.transaction.Neo4jTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableNeo4jRepositories(basePackages = "com.hy.springboot.data.neo4j.dao")// 开启Neo4jRepositories的支持
@EntityScan(basePackages = "com.hy.springboot.data.neo4j.entity")// 开启实体类扫描
@EnableTransactionManagement // 开启事务
public class Neo4jConfig {
@Bean
public SessionFactory sessionFactory() {
// with domain entity base package(s)
return new SessionFactory(configuration(),"com.hy.springboot.data.neo4j.entity");
}
@Bean
public org.neo4j.ogm.config.Configuration configuration() {
// ConfigurationSource properties = new
// ClasspathConfigurationSource("ogm.properties");
org.neo4j.ogm.config.Configuration configuration = new org.neo4j.ogm.config.Configuration.Builder()
.uri("bolt://192.168.1.100:7687").credentials("neo4j", "123456").build();
return configuration;
}
@Bean
public Neo4jTransactionManager transactionManager() {
return new Neo4jTransactionManager(sessionFactory());
}
}
2.主要实体类:User.java
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
@NodeEntity
public class User {
@Id
private Integer id;
private String name;
// 省略get、set、等方法
}
主要使用org.neo4j.ogm.annotation这个包下面的注解
,通过源码可以发现的,类必须标记NodeEntity(个人感觉和Spring-data中的内容都差不多)
==3.主要接口:UserRepository.java
==
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.stereotype.Repository;
import com.hy.springboot.data.neo4j.entity.User;
@Repository
public interface UserRepository extends Neo4jRepository<User, Long>{
}
一个空接口
4.Controller层:UserController.java
import com.hy.springboot.data.neo4j.dao.UserRepository;
import com.hy.springboot.data.neo4j.entity.User;
@RestController
public class UserController {
@Autowired
UserRepository userRepository;
@GetMapping("/findAllUser")
public Object findAllUser() {
Iterable<User> findAll = userRepository.findAll();
return findAll;
}
@RequestMapping("/saveUser")
public String saveUser(User user) {
userRepository.save(user);
return "{\"msg\":\"添加用户成功\"}";
}
}
5.入口类:SpringBootNeo4jApp.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootNeo4jApp {
public static void main(String[] args) {
SpringApplication.run(SpringBootNeo4jApp.class, args);
}
}
1.只要导入了必要的pom依赖其他的都不是问题,任何依赖问题都解决
2.一定要在new SessionFactory(configuration(),"com.hy.springboot.data.neo4j.entity");
中指定实体类的包,否则会出现一些其他奇怪的错误信息
例如:
Caused by: java.lang.ClassNotFoundException: groovy.lang.GroovyObject
Caused by: java.lang.ClassNotFoundException: org.codehaus.janino.ScriptEvaluator
Caused by: java.lang.ClassNotFoundException: javax.mail.internet.AddressException
本人在一开始的时候由于未指定实体类的包,所以出现这些错误,其实这些错误并不是缺少包导致的,而是未写明实体类的包导致的错误
3.org.neo4j.ogm.exception.core.NotFoundException
:这个出现的原因是无法连接Neo4j导致的错误,个人解决了这些问题,所以对这些异常感觉很奇怪
4.踩了以上无的坑,终于成功的使用了!