<div class="show-content-free"> <p>前面介紹了<a href="https://www.jianshu.com/p/4afc2652b6f5" target="_blank">Boot 1.5版本集成Neo4j</a>,Boot 2.0以上版本Neo4j變化較大。<br> 場景仍是<a href="https://www.jianshu.com/p/8222a9330b88" target="_blank">電影人員關係</a></p> <h4>Boot 2.0主要變化</h4> <ol> <li>GraphRepository在Boot2.0下不支持了,調整爲Neo4jRepository。<br> 對應的findById和deleteById也要調整。</li> <li>@GraphId也不支持須要改爲@Id @GeneratedValue 。</li> <li>連接方式調整爲bolt://192.168.1.8:7687</li> </ol> <h2>Demo</h2> <ol> <li>pom文件<br> 調整boot的版本</li> </ol> <pre class="hljs xml"><code class="xml"> <span class="hljs-tag"><<span class="hljs-name">parent</span>></span> <span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>org.springframework.boot<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span> <span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>spring-boot-starter-parent<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span> <span class="hljs-tag"><<span class="hljs-name">version</span>></span>2.1.0.RELEASE<span class="hljs-tag"></<span class="hljs-name">version</span>></span> <span class="hljs-tag"><<span class="hljs-name">relativePath</span>/></span> <span class="hljs-comment"><!-- lookup parent from repository --></span> <span class="hljs-tag"></<span class="hljs-name">parent</span>></span> </code></pre> <ol start="2"> <li>application.yml配置</li> </ol> <pre class="hljs cpp"><code class="cpp">spring: data: neo4j: uri: bolt:<span class="hljs-comment">//192.168.1.8:7687</span> username: neo4j password: <span class="hljs-number">123456</span> </code></pre> <ol start="3"> <li>模型類調整爲<br> Person類</li> </ol> <pre class="hljs java"><code class="java"><span class="hljs-keyword">package</span> com.github.davidji80.springboot.neo4j.model;css
<span class="hljs-keyword">import</span> org.neo4j.ogm.annotation.*;html
<span class="hljs-meta">@NodeEntity</span>(label = <span class="hljs-string">"Person"</span>) <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> </span>{ <span class="hljs-meta">@Id</span> <span class="hljs-meta">@GeneratedValue</span> <span class="hljs-keyword">private</span> Long nodeId;java
<span class="hljs-meta">@Property</span>(name = <span class="hljs-string">"name"</span>) <span class="hljs-keyword">private</span> String name; <span class="hljs-meta">@Property</span>(name = <span class="hljs-string">"born"</span>) <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> born; <span class="hljs-function"><span class="hljs-keyword">public</span> Long <span class="hljs-title">getNodeId</span><span class="hljs-params">()</span> </span>{ <span class="hljs-keyword">return</span> nodeId; } <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setNodeId</span><span class="hljs-params">(Long nodeId)</span> </span>{ <span class="hljs-keyword">this</span>.nodeId = nodeId; } <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getName</span><span class="hljs-params">()</span> </span>{ <span class="hljs-keyword">return</span> name; } <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setName</span><span class="hljs-params">(String name)</span> </span>{ <span class="hljs-keyword">this</span>.name = name; } <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getBorn</span><span class="hljs-params">()</span> </span>{ <span class="hljs-keyword">return</span> born; } <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setBorn</span><span class="hljs-params">(<span class="hljs-keyword">int</span> born)</span> </span>{ <span class="hljs-keyword">this</span>.born = born; }
} </code></pre>node
<ol start="4"> <li>DAO調整爲<br> PersonRepository</li> </ol> <pre class="hljs css"><code class="css"><span class="hljs-selector-tag">package</span> <span class="hljs-selector-tag">com</span><span class="hljs-selector-class">.github</span><span class="hljs-selector-class">.davidji80</span><span class="hljs-selector-class">.springboot</span><span class="hljs-selector-class">.neo4j</span><span class="hljs-selector-class">.dao</span>;git
<span class="hljs-selector-tag">import</span> <span class="hljs-selector-tag">com</span><span class="hljs-selector-class">.github</span><span class="hljs-selector-class">.davidji80</span><span class="hljs-selector-class">.springboot</span><span class="hljs-selector-class">.neo4j</span><span class="hljs-selector-class">.model</span><span class="hljs-selector-class">.Person</span>; <span class="hljs-selector-tag">import</span> <span class="hljs-selector-tag">org</span><span class="hljs-selector-class">.springframework</span><span class="hljs-selector-class">.data</span><span class="hljs-selector-class">.neo4j</span><span class="hljs-selector-class">.repository</span><span class="hljs-selector-class">.Neo4jRepository</span>;github
<span class="hljs-selector-tag">public</span> <span class="hljs-selector-tag">interface</span> <span class="hljs-selector-tag">PersonRepository</span> <span class="hljs-selector-tag">extends</span> <span class="hljs-selector-tag">Neo4jRepository</span><<span class="hljs-selector-tag">Person</span>, <span class="hljs-selector-tag">Long</span>> { } </code></pre>spring
<ol start="5"> <li>Service層調整</li> </ol> <pre class="hljs css"><code class="css"><span class="hljs-selector-tag">package</span> <span class="hljs-selector-tag">com</span><span class="hljs-selector-class">.github</span><span class="hljs-selector-class">.davidji80</span><span class="hljs-selector-class">.springboot</span><span class="hljs-selector-class">.neo4j</span><span class="hljs-selector-class">.service</span><span class="hljs-selector-class">.impl</span>;springboot
<span class="hljs-selector-tag">import</span> <span class="hljs-selector-tag">com</span><span class="hljs-selector-class">.github</span><span class="hljs-selector-class">.davidji80</span><span class="hljs-selector-class">.springboot</span><span class="hljs-selector-class">.neo4j</span><span class="hljs-selector-class">.dao</span><span class="hljs-selector-class">.ActedInRepository</span>; <span class="hljs-selector-tag">import</span> <span class="hljs-selector-tag">com</span><span class="hljs-selector-class">.github</span><span class="hljs-selector-class">.davidji80</span><span class="hljs-selector-class">.springboot</span><span class="hljs-selector-class">.neo4j</span><span class="hljs-selector-class">.dao</span><span class="hljs-selector-class">.DirectedRepository</span>; <span class="hljs-selector-tag">import</span> <span class="hljs-selector-tag">com</span><span class="hljs-selector-class">.github</span><span class="hljs-selector-class">.davidji80</span><span class="hljs-selector-class">.springboot</span><span class="hljs-selector-class">.neo4j</span><span class="hljs-selector-class">.dao</span><span class="hljs-selector-class">.MovieRepository</span>; <span class="hljs-selector-tag">import</span> <span class="hljs-selector-tag">com</span><span class="hljs-selector-class">.github</span><span class="hljs-selector-class">.davidji80</span><span class="hljs-selector-class">.springboot</span><span class="hljs-selector-class">.neo4j</span><span class="hljs-selector-class">.dao</span><span class="hljs-selector-class">.PersonRepository</span>; <span class="hljs-selector-tag">import</span> <span class="hljs-selector-tag">com</span><span class="hljs-selector-class">.github</span><span class="hljs-selector-class">.davidji80</span><span class="hljs-selector-class">.springboot</span><span class="hljs-selector-class">.neo4j</span><span class="hljs-selector-class">.model</span><span class="hljs-selector-class">.ActedIn</span>; <span class="hljs-selector-tag">import</span> <span class="hljs-selector-tag">com</span><span class="hljs-selector-class">.github</span><span class="hljs-selector-class">.davidji80</span><span class="hljs-selector-class">.springboot</span><span class="hljs-selector-class">.neo4j</span><span class="hljs-selector-class">.model</span><span class="hljs-selector-class">.Directed</span>; <span class="hljs-selector-tag">import</span> <span class="hljs-selector-tag">com</span><span class="hljs-selector-class">.github</span><span class="hljs-selector-class">.davidji80</span><span class="hljs-selector-class">.springboot</span><span class="hljs-selector-class">.neo4j</span><span class="hljs-selector-class">.model</span><span class="hljs-selector-class">.Movie</span>; <span class="hljs-selector-tag">import</span> <span class="hljs-selector-tag">com</span><span class="hljs-selector-class">.github</span><span class="hljs-selector-class">.davidji80</span><span class="hljs-selector-class">.springboot</span><span class="hljs-selector-class">.neo4j</span><span class="hljs-selector-class">.model</span><span class="hljs-selector-class">.Person</span>; <span class="hljs-selector-tag">import</span> <span class="hljs-selector-tag">com</span><span class="hljs-selector-class">.github</span><span class="hljs-selector-class">.davidji80</span><span class="hljs-selector-class">.springboot</span><span class="hljs-selector-class">.neo4j</span><span class="hljs-selector-class">.service</span><span class="hljs-selector-class">.MovieServer</span>; <span class="hljs-selector-tag">import</span> <span class="hljs-selector-tag">org</span><span class="hljs-selector-class">.springframework</span><span class="hljs-selector-class">.beans</span><span class="hljs-selector-class">.factory</span><span class="hljs-selector-class">.annotation</span><span class="hljs-selector-class">.Autowired</span>; <span class="hljs-selector-tag">import</span> <span class="hljs-selector-tag">org</span><span class="hljs-selector-class">.springframework</span><span class="hljs-selector-class">.stereotype</span><span class="hljs-selector-class">.Service</span>;app
<span class="hljs-selector-tag">import</span> <span class="hljs-selector-tag">java</span><span class="hljs-selector-class">.util</span><span class="hljs-selector-class">.Optional</span>;ide
@<span class="hljs-keyword">Service</span> public class MovieServiceImpl implements MovieServer {
@<span class="hljs-keyword">Autowired</span> private PersonRepository personRepository; @<span class="hljs-keyword">Autowired</span> private MovieRepository movieRepository; @<span class="hljs-keyword">Autowired</span> private DirectedRepository directedRepository; @<span class="hljs-keyword">Autowired</span> private ActedInRepository actedInRepository; @<span class="hljs-keyword">Override</span> public Person addPerson(Person person){ <span class="hljs-selector-tag">return</span> <span class="hljs-selector-tag">personRepository</span><span class="hljs-selector-class">.save</span>(<span class="hljs-selector-tag">person</span>); } @<span class="hljs-keyword">Override</span> public Person findOnePerson(long id){ <span class="hljs-selector-tag">return</span> <span class="hljs-selector-tag">personRepository</span><span class="hljs-selector-class">.findById</span>(<span class="hljs-selector-tag">id</span>)<span class="hljs-selector-class">.get</span>(); } @<span class="hljs-keyword">Override</span> public void deleteOnePerson(long id){ <span class="hljs-selector-tag">personRepository</span><span class="hljs-selector-class">.deleteById</span>(<span class="hljs-selector-tag">id</span>); } @<span class="hljs-keyword">Override</span> public Movie addMovie(Movie movie){ <span class="hljs-selector-tag">return</span> <span class="hljs-selector-tag">movieRepository</span><span class="hljs-selector-class">.save</span>(<span class="hljs-selector-tag">movie</span>); } @<span class="hljs-keyword">Override</span> public Movie findOneMovie(long id){ <span class="hljs-selector-tag">return</span> <span class="hljs-selector-tag">movieRepository</span><span class="hljs-selector-class">.findById</span>(<span class="hljs-selector-tag">id</span>)<span class="hljs-selector-class">.get</span>(); } @<span class="hljs-keyword">Override</span> public Directed directed(Directed directed){ <span class="hljs-selector-tag">return</span> <span class="hljs-selector-tag">directedRepository</span><span class="hljs-selector-class">.save</span>(<span class="hljs-selector-tag">directed</span>); } @<span class="hljs-keyword">Override</span> public ActedIn actedIn(ActedIn actedIn) { <span class="hljs-selector-tag">return</span> <span class="hljs-selector-tag">actedInRepository</span><span class="hljs-selector-class">.save</span>(<span class="hljs-selector-tag">actedIn</span>); }
} </code></pre>
<h2>代碼</h2> <p><a href="https://github.com/DavidJi80/springboot" target="_blank" rel="nofollow">https://github.com/DavidJi80/springboot</a><br> v0.8.1</p> <blockquote> <p>參考<br> <a href="https://blog.csdn.net/d597180714/article/details/81079848" target="_blank" rel="nofollow">https://blog.csdn.net/d597180714/article/details/81079848</a><br> <a href="https://www.cnblogs.com/zhangboyu/p/7580262.html" target="_blank" rel="nofollow">https://www.cnblogs.com/zhangboyu/p/7580262.html</a></p> </blockquote> 原文地址:https://www.jianshu.com/p/1aeeefb4fc7a </div>