赞
踩
这篇文章主要为基础配置,后续代码及功能查看后续文章。
默认neo4j已完成安装,且运行成功了,如果需要可以查看他人博客:点击查看安装教程
注意它里面 有其它java的JDK 和 没有其它JDK的区别
基础配置及环境信息:
1、meysql:5.7
2、neo4j:4.4.5社区版
3、idea + springBoot + myBatis
4、java:JDK1.8 + JDK11
5、windows10
此时默认已配置并写好了MySql。后续仅为连接neo4j。
给出一个文件包 ,保存所有需要用到的neo4j代码;
1.1、说明:所有neo4j代码都在neo4j文件夹下
config:自测时所需配置文件
controller:Controller
entry:实体对象
repository:sql层,类似与dao
service:接口层
导包(此处仅neo4j包),以maven导入neo4j4.4.5为例:
仅用maven导包时间会比较长,可以先编写后续代码
注意
:导入完成后spring-boot-starter-data-neo4j包的driver可能会报错,这是驱动不匹配的原因,后续第二个包就是引用对应的驱动,引入成功后不用管前一个包的报错信息
<!-- neo4j -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
<dependency>
<groupId>org.neo4j.driver</groupId>
<artifactId>neo4j-java-driver</artifactId>
<version>4.4.5</version>
</dependency>
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j; import com.google.common.base.Function; import com.google.common.base.Optional; import com.google.common.base.Predicate; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.RequestHandler; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 @EnableKnife4j public class SwaggerConfig { // 定义分隔符,配置Swagger多包 private static final String splitor = ";"; /** * 是否开启swagger,生产环境一般关闭 */ @Value("${swagger.enabled}") private boolean enabled; @Bean public Docket CreatRestApi() { return new Docket(DocumentationType.SWAGGER_2) // 详细定制 .apiInfo(apiInfo()) .select() // 指定当前包路径,这里就添加了两个包,注意方法变成了basePackage,中间加上成员变量splitor .apis(basePackage("com.xxxxxx.web"+splitor+"com.xxxxxx.neo4j.controller")) // 扫描所有 .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder().title("项目API接口").description("接口列表") .version("1.0").build(); } /** * 重写basePackage方法,使能够实现多包访问,复制贴上去 * @param basePackage 多包字符串,用 ";"分隔 * @return com.google.common.base.Predicate<springfox.documentation.RequestHandler> */ public static Predicate<RequestHandler> basePackage(final String basePackage) { //获取下面两个包中所有类(扫描) return input -> declaringClass(input).transform(handlerPackage(basePackage)).or(true); } /** * 新增处理,分割包,并合并扫描 * @param basePackage * @return */ private static Function<Class<?>, Boolean> handlerPackage(final String basePackage) { //循环判断,是否是包含包路径 return input -> { // 循环判断匹配 for (String strPackage : basePackage.split(splitor)) { if (input.getPackage().getName().startsWith(strPackage)) { return true; } } return false; }; } private static Optional<? extends Class<?>> declaringClass(RequestHandler input) { return Optional.fromNullable(input.getClass().getEnclosingClass()); } }
import org.neo4j.driver.AuthTokens; import org.neo4j.driver.Driver; import org.neo4j.driver.GraphDatabase; import org.neo4j.driver.Session; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class Neo4jConfig { @Bean public Driver neo4jDriver() { return GraphDatabase.driver("neo4j://localhost:7687", AuthTokens.basic("neo4j","123456")); } @Bean public Session neo4jSession() { return neo4jDriver().session(); } }
GraphDatabase.driver:配置驱动
neo4j://localhost:7687:进入neo4j后可以看到:
AuthTokens.basic(“neo4j”,“123456”):账号、密码
import org.springframework.stereotype.Service;
@Service
public interface ExampleNeo4jService {
void executeNeo4jQuery() ;
}
ExampleNeo4jServiceImpl.java: 放到neo4j.service.impl下
import com.xxxxxx.neo4j.service.ExampleNeo4jService; import org.neo4j.driver.*; import org.neo4j.driver.types.Node; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class ExampleNeo4jServiceImpl implements ExampleNeo4jService { private static final Logger logger = LoggerFactory.getLogger(ExampleNeo4jServiceImpl.class); @Autowired private Session neo4jSession; public void executeNeo4jQuery() { String query = "MATCH (n:separation) RETURN n LIMIT 25"; neo4jSession.readTransaction(tx -> { Result result = tx.run(query); while (result.hasNext()) { Record record = result.next(); Node node = record.get("n").asNode(); logger.info("Node name: {}", node.get("separation_name").asString()); } return null; }); } }
注意大小写
只要有数据就说明连接成功
直接触发就好,不论是junit 还是 Controller 都行,下面我用的是junit
test.java:放在 xxx.src.test.java.impl.test.java
import com.xxxxxx.DrawingApplication; import com.xxxxxx.neo4j.service.ExampleNeo4jService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; @RunWith(SpringRunner.class) @SpringBootTest(classes = XXXApplication.class) public class test { @Resource private ExampleNeo4jService exampleNeo4jService; @Test public void testAddStation2() { exampleNeo4jService.executeNeo4jQuery(); } }
安装配置neo4j:点击跳转
neo4j 实体对象增删查询: 还没写
neo4j 实体及实体关系和对应关系对象查询: 还没写
3.1.实体对象关系查询改写:还没研究出来
sql及neo4j联动: 还没写
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。