一、安装Neo4j图数据库我下载的是免安装zip版本的
1、解压ZIP包数据
2、配置环境变量
创建主目录环境变量NEO4J_HOME,并把主目录设置为变量值。win10系统环境变量设置步骤NEO4J_HOME = F:\neo4j- community-3.4.0 -- 根据自己的具体情况、Path = %NEO4J_HOME%\bin
3、验证Neo4j的安装和环境变量配置是否成功 -- 测试
neo4j.bat console
4、册NEO4J服务不用每次启动的时候都要去点击启动neo4j.bat文件
neo4j install-service
5、开启NEO4J服务
启动neo4j start、停止 neo4j stop、重启neo4j restart
6、然后打开浏览器输入地址http://localhost:7474记住数据库的用户名和密码,一般默认的是:
用户:neo4j,密码:neo4j
我把把用户/密码修改成:neo4j/admin123
7、打开浏览器输入http://localhost:7474就会跳到登录界面
二、spring boot集成neo4j
因为我的项目已经有了数据库oracle了所以还需要在集成neo4j比较麻烦一点,因为需要管理数据源所以为了不影响原来的数据源咱们直接用驱动获取Neo4j数据源的session然后在项目中直接执行cql语句。
1、配置neo4j的文件【neo4j.properties】数据库登录账号密码
#neo4j图数据库配置文件
spring.data.neo4j.uri=bolt://ip/:7687
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=neo4j
2、pom.xml配置
org.neo4j.driver
neo4j-java-driver
1.5.0
3、创建配置文件Neo4jConfig把neo4j的数据源驱动放到spring的容器内
import org.neo4j.driver.v1.AuthTokens;
import org.neo4j.driver.v1.Config;
import org.neo4j.driver.v1.Driver;
import org.neo4j.driver.v1.GraphDatabase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.InputStream;
import java.util.Properties;
@Configuration
public class Neo4jConfig {
private static Logger log = LoggerFactory.getLogger(Neo4jConfig.class);
private static String NEO4J_URI;
private static String NEO4J_USER;
private static String NEO4J_PASSWORD;
static {
try {
Properties properties = new Properties();
InputStream in = Neo4jConfig.class.getClassLoader().getResourceAsStream("conf/neo4j.properties");
properties.load(in);
NEO4J_URI = properties.getProperty("spring.data.neo4j.uri");
NEO4J_USER = properties.getProperty("spring.data.neo4j.username");
NEO4J_PASSWORD = properties.getProperty("spring.data.neo4j.password");
} catch (Exception e) {
log.error("读取neo4j配置文件失败" + e);
}
}
/**
* 将数据源注入到Spring容器
*
* @return
*/
@Bean("neo4jDriver")
public Driver session() {
//配置连接池大小
Config config = Config.build().withEncryption()
.withTrustStrategy(Config.TrustStrategy.trustAllCertificates())
.withMaxConnectionPoolSize(20).toConfig();
Driver driver = GraphDatabase.driver(NEO4J_URI, AuthTokens.basic(NEO4J_USER, NEO4J_PASSWORD), config);
return driver;
}
}
4、编写调用执行类方法
import org.neo4j.driver.v1.Driver;
import org.neo4j.driver.v1.Session;
import org.neo4j.driver.v1.StatementResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
public class Neo4jApiUtil {
private static final Logger log = LoggerFactory.getLogger(Neo4jApiUtil.class);
@Autowired
private Driver neo4jDriver;
Session session = null;
/**
* 新增标签到图数据库
*
* @param createCql CREATE (a:Person {name: {name}, title: {title}})
* @return
*/
public void saveNeo4jInfo(String createCql) {
try {
if (!StringUtils.isEmpty(createCql)) {
session = neo4jDriver.session();
StatementResult result = session.run(createCql);
}
} catch (Exception e) {
log.error("新增图数据库标签异常" + e);
} finally {
if (session != null) {
session.close();
}
}
return jd;
}
/**
* 查询数据
*
* @param queryCql MATCH (a:Person) WHERE a.name = {name} RETURN a.name AS name, a.title AS title"
* @return
*/
public void queryNeo4jInfo(String queryCql) {
try {
if (!StringUtils.isEmpty(queryCql)) {
session = neo4jDriver.session();
StatementResult result = session.run(queryCql);
}
} catch (Exception e) {
log.error("查询异常" + e);
} finally {
if (session != null) {
session.close();
}
}
}
三、执行结果