官网语法,非常详尽:http://neo4j.com/docs/developer-manual/current/cypher/clauses/match/
A:请对应版本号,不同大版本可能会有很大区别
B:我会对我学习过程中遇到的难点详细说明,有些基本的东西会带过
C: 初学,有错误我以后会修改
=============================2017.11.02============================================
1、是什么:
简单来说就是用于图形计算的非关系行数据库。
2、安装:略
linux与windows版本,学习的话装windows即可,安装非常简单,这里不说了,百度会有好多。
进入如图:
3、使用:
版本:v3.2.6 2017.11.2日最新版本
a、基础语法CQL(网上语法很多,但都没有解释什么是什么):
节点
增:create (a:aDemo{dId:"001",name:"zhangsan"})
create:创建(创建出来的点叫节点);
a:我开始理解的是类似表别名、变量名(很可能不对,好像没有什么意义,先一放);
aDemo:经试验类似于表名,或者说一个类型的集合
create (a:aDemo{dId:"001",name:"zhangsan"})
create (a:aDemo{dId:"002",name:"lisi"})
create (a:aDemo{dId:"003",name:"wamhwu"})
create (a:aDemo{dId:"003",name:"wamhwu"})
create (a:aDemo{dId:"003",name:"wamhwu"})
依次执行,我创建了五个节点,每个节点内置了一个自增的唯一id
删:MATCH(n:aDemo {name:"lisi"}) DETACH DELETE n
查:MATCH (n) RETURN n 也可:start a = node(*) return a//查询所有集合中所有数据
MATCH (n:aDemo) RETURN n LIMIT 25//查询 aDemo集合中所有数据
MATCH (n:aDemo{name:"zhangsan"}) RETURN n LIMIT 25
改:MATCH (dc:aDemo{name:"zhangsan"})SET dc.newAttr = "new attr" RETURN dc
关系:
摘抄地址:W3C:https://www.w3cschool.cn/neo4j/neo4j_cql_relationship_basics.html
语法:
CREATE (<node1-details>)-[<relationship-details>]->(<node2-details>)
<node1-details>是“From Node”节点详细信息
<node2-details>是“到节点”节点详细信息
relationship-details>是关系详细信息
CREATE (n1:aDemo{name:"zhangsan"})-[r1:Relationship]->(n2:aDemo{name:"wangwu"})
此处值得注意的是,我有多个张三一个李四,只建立了一条关系(最后一条张三(id最大))
=========================================================================
基础语法不准备再补充了,有这点基础,就可以边百度边用了,下面是尝试集成到Spring4中,网上只有
集成到Spring-boot的资源与教程,但是为了加一个图数据库改动框架实在有些得不偿失。
首先看一下简单的JDBC:
demo写完就找不到了,那就不管了,下面是需求,结合SpringMVC使用Neo4j
===================================================================================20171110
今天正式填完了所有坑,下面记录下过程:
官方文档祭天:
https://docs.spring.io/spring-data/data-neo4j/docs/4.2.0.RELEASE/reference/html/#preface.requirements
首先对应版本:
3. Requirements
Spring Data Neo4j 4.2.x at minimum, requires:
JDK Version 8 and above.
Neo4j Graph Database 2.3.x / 3.0.x / 3.1.x and above.
Spring Framework 4.3.6.RELEASE and above.
If you plan on altering the version of the OGM make sure it is only in the 2.1.1+
release family.
项目结构:
升级jdk(主要是把编译器换成1.8的):
根据要求配置依赖包:
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.3.12.RELEASE</spring.version><!-- 声明版本号,以后统一在这里进行管理 --> </properties> <dependencies> <!-- neo4j-data以及依赖包 --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-neo4j</artifactId> <version>4.2.8.RELEASE</version><!-- 4.1.1.RELEASE --> </dependency> <dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j-ogm-core</artifactId> <version>2.1.1</version> <scope>compile</scope> </dependency> <!-- Only add if you're using the Bolt driver这里我选择了bolt协议 --> <dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j-ogm-bolt-driver</artifactId> <version>2.1.1</version> <scope>runtime</scope> </dependency> <!-- Only add if you're using the HTTP driver --> <!-- <dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j-ogm-http-driver</artifactId> <version>2.1.1</version> <scope>runtime</scope> </dependency> --> <!-- Only add if you're using the Embedded driver --> <!-- <dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j-ogm-embedded-driver</artifactId> <version>2.1.1 RELEASE</version> <scope>runtime</scope> </dependency> --> 因为升级了spring版本,对其它组件版本进行了微调,这个不难,也就不列举了
配置启动类(有xml与方法配置的方式,这里使用了后者):MyConfiguration类: package com.neo.conf; import org.neo4j.ogm.session.SessionFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories; import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration @EnableNeo4jRepositories(basePackages = "com.neo.repository")//相当于service、执行库的位置 @EnableTransactionManagement public class MyConfiguration { @Bean public org.neo4j.ogm.config.Configuration getConfiguration() { org.neo4j.ogm.config.Configuration config = new org.neo4j.ogm.config.Configuration(); // TODO: Temporary uses the embedded driver. We need to switch to http // driver. Then we can horizontally scale neo4j config.driverConfiguration() .setDriverClassName("org.neo4j.ogm.drivers.bolt.driver.BoltDriver") .setURI("bolt://neo4j:123456@localhost") .setConnectionPoolSize(150); return config; } @Bean public SessionFactory getSessionFactory() { // Return the session factory which also includes the persistent entities return new SessionFactory(getConfiguration(), "com.neo.entitys"); } } Repository类: /** * @author Michael Hunger * @author Mark Angrish */ public interface MovieRepository extends GraphRepository<Movie>{ Movie findByTitle(@Param("title") String title); Collection<Movie> findByTitleLike(@Param("title") String title); @Query("MATCH (m:Movie)<-[r:ACTED_IN]-(a:Person) RETURN m,r,a LIMIT {limit}") Collection<Movie> graph(@Param("limit") int limit); } @Repository public interface PersonRepository extends GraphRepository<PersonBak> { } public interface RoleRepository extends GraphRepository<Role> {} Entity类: @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id") @NodeEntity public class Movie { @GraphId private Long id; private String title; private int released; private String tagline; @Relationship(type = "ACTED_IN", direction = Relationship.INCOMING) private List<Role> roles = new ArrayList<>(); public Movie() { } public Movie(Long id,String title, int released) { this.id=id; this.title = title; this.released = released; } public Movie(String title, int released) { this.title = title; this.released = released; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getTitle() { return title; } public int getReleased() { return released; } public String getTagline() { return tagline; } public Collection<Role> getRoles() { return roles; } public void addRole(Role role) { this.roles.add(role); } } @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id") @NodeEntity public class Person { @GraphId private Long id; private String name; private int born; @Relationship(type = "ACTED_IN") private List<Movie> movies = new ArrayList<>(); public Person() { } public Person(String name) { this.name = name; } public Person( Long id,String name) { this.id = id; this.name = name; } public Long getId() { return id; } public String getName() { return name; } public int getBorn() { return born; } public List<Movie> getMovies() { return movies; } } @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id") @RelationshipEntity(type = "ACTED_IN") public class Role { @GraphId private Long id; private Collection<String> roles = new ArrayList<>(); @StartNode private Person person; @EndNode private Movie movie; public Role() { } public Role(Movie movie, Person actor) { this.movie = movie; this.person = actor; } public Long getId() { return id; } public Collection<String> getRoles() { return roles; } public Person getPerson() { return person; } public Movie getMovie() { return movie; } public void addRoleName(String name) { this.roles.add(name); } } 使用: @Autowired private PersonRepository personRepository; @Autowired MovieRepository movieRepository; @Autowired RoleRepository roleRepository; Public void test(){ Person persontmp1=new Person("anmuxi12222"); Movie movieTmp=new Movie(66l,"title22222222",1); Role ro = new Role(movieTmp,persontmp1); roleRepository.save(ro); Collection<Movie> graph = movieRepository.graph(10); Iterator<Movie> iterator = graph.iterator(); while (iterator.hasNext()) { Movie next = iterator.next(); System.out.println(next.getId()); } }
这时候发现,存在中文乱码情况:查看源码,打印了下系统编码
String csn = Charset.defaultCharset().name();
System.err.println(csn);//结果是GBK配置环境变量:
“JAVA_TOOL_OPTIONS” 变量,值为“-Dfile.encoding=UTF-8” over