SpingBoot myBatis neo4j整合项目案例
此项目为spring boot - myBatis - neo4j数据库整合项目。
有增删改查(节点关系)、动态分页条件排序等一些示例。
git下载地址:git clone https://github.com/wsm1217395196/my-project-demo.git 项目名:springboot-mybatis-neo4j
运行此项目前请先到http://localhost:7474/browser/运行cql(确定安装了neo4j数据库)
create (u:user{name:'wsm',sex:'男',age:23}) - [l:like] -> (y:user{name:'yy',sex:'女',age:21})
pom文件依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>springboot-neo4j-mybatis<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>springboot-neo4j-mybatis<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">version</span>></span>1.0-SNAPSHOT<span class="hljs-tag"></<span class="hljs-name">version</span>></span>
<span class="hljs-tag"><<span class="hljs-name">dependencies</span>></span>
<span class="hljs-comment"><!--spring boot--></span>
<span class="hljs-tag"><<span class="hljs-name">dependency</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-web<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">version</span>></span>2.1.2.RELEASE<span class="hljs-tag"></<span class="hljs-name">version</span>></span>
<span class="hljs-tag"></<span class="hljs-name">dependency</span>></span>
<span class="hljs-tag"><<span class="hljs-name">dependency</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-test<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">scope</span>></span>test<span class="hljs-tag"></<span class="hljs-name">scope</span>></span>
<span class="hljs-tag"><<span class="hljs-name">version</span>></span>2.1.2.RELEASE<span class="hljs-tag"></<span class="hljs-name">version</span>></span>
<span class="hljs-tag"></<span class="hljs-name">dependency</span>></span>
<span class="hljs-comment"><!--org.json--></span>
<span class="hljs-tag"><<span class="hljs-name">dependency</span>></span>
<span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>org.json<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>json<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">version</span>></span>20180813<span class="hljs-tag"></<span class="hljs-name">version</span>></span>
<span class="hljs-tag"></<span class="hljs-name">dependency</span>></span>
<span class="hljs-comment"><!--neo4j-jdbc-driver--></span>
<span class="hljs-tag"><<span class="hljs-name">dependency</span>></span>
<span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>org.neo4j<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>neo4j-jdbc-driver<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">version</span>></span>3.4.0<span class="hljs-tag"></<span class="hljs-name">version</span>></span>
<span class="hljs-tag"></<span class="hljs-name">dependency</span>></span>
<span class="hljs-comment"><!--mybatis-spring-boot--></span>
<span class="hljs-tag"><<span class="hljs-name">dependency</span>></span>
<span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>org.mybatis.spring.boot<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>mybatis-spring-boot-starter<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">version</span>></span>2.0.1<span class="hljs-tag"></<span class="hljs-name">version</span>></span>
<span class="hljs-tag"></<span class="hljs-name">dependency</span>></span>
<span class="hljs-tag"></<span class="hljs-name">dependencies</span>></span>
<span class="hljs-tag"><<span class="hljs-name">build</span>></span>
<span class="hljs-tag"><<span class="hljs-name">plugins</span>></span>
<span class="hljs-tag"><<span class="hljs-name">plugin</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-maven-plugin<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span>
<span class="hljs-tag"></<span class="hljs-name">plugin</span>></span>
<span class="hljs-tag"></<span class="hljs-name">plugins</span>></span>
<span class="hljs-tag"></<span class="hljs-name">build</span>></span>
</project>
application.yml
#端口号
server:
port: 8018
spring:
application:
name: springboot-mybatis-neo4j #服务名
datasource:
driver-class-name: org.neo4j.jdbc.http.HttpDriver
name: neo4j
password: root
url: jdbc:neo4j:http://localhost:7474
mybatis:
mapper-locations: classpath:mapper/*.xml
UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.study.mapper.UserMapper">
<span class="hljs-comment"><!-- 自定义结果集--></span>
<span class="hljs-tag"><<span class="hljs-name">resultMap</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"userMap"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"com.study.model.UserModel"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">id</span> <span class="hljs-attr">property</span>=<span class="hljs-string">"id"</span> <span class="hljs-attr">column</span>=<span class="hljs-string">"id"</span>/></span>
<span class="hljs-tag"><<span class="hljs-name">result</span> <span class="hljs-attr">property</span>=<span class="hljs-string">"name"</span> <span class="hljs-attr">column</span>=<span class="hljs-string">"name"</span>/></span>
<span class="hljs-tag"><<span class="hljs-name">result</span> <span class="hljs-attr">property</span>=<span class="hljs-string">"age"</span> <span class="hljs-attr">column</span>=<span class="hljs-string">"age"</span>/></span>
<span class="hljs-tag"><<span class="hljs-name">result</span> <span class="hljs-attr">property</span>=<span class="hljs-string">"sex"</span> <span class="hljs-attr">column</span>=<span class="hljs-string">"sex"</span>/></span>
<span class="hljs-tag"><<span class="hljs-name">collection</span> <span class="hljs-attr">property</span>=<span class="hljs-string">"userModels"</span> <span class="hljs-attr">ofType</span>=<span class="hljs-string">"com.study.model.UserModel"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">id</span> <span class="hljs-attr">property</span>=<span class="hljs-string">"id"</span> <span class="hljs-attr">column</span>=<span class="hljs-string">"id1"</span>/></span>
<span class="hljs-tag"><<span class="hljs-name">result</span> <span class="hljs-attr">property</span>=<span class="hljs-string">"name"</span> <span class="hljs-attr">column</span>=<span class="hljs-string">"name1"</span>/></span>
<span class="hljs-tag"><<span class="hljs-name">result</span> <span class="hljs-attr">property</span>=<span class="hljs-string">"age"</span> <span class="hljs-attr">column</span>=<span class="hljs-string">"age1"</span>/></span>
<span class="hljs-tag"><<span class="hljs-name">result</span> <span class="hljs-attr">property</span>=<span class="hljs-string">"sex"</span> <span class="hljs-attr">column</span>=<span class="hljs-string">"sex1"</span>/></span>
<span class="hljs-tag"></<span class="hljs-name">collection</span>></span>
<span class="hljs-tag"></<span class="hljs-name">resultMap</span>></span>
<span class="hljs-comment"><!--这里CQL语句返回的字段名必须与上面定义的结果集的字段名一致,若不一致需要重命名,否则会报空指针异常 --></span>
<span class="hljs-tag"><<span class="hljs-name">select</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"getPage"</span> <span class="hljs-attr">resultMap</span>=<span class="hljs-string">"userMap"</span>></span>
match (u:user)
<span class="hljs-tag"><<span class="hljs-name">where</span>></span>
<span class="hljs-tag"><<span class="hljs-name">if</span> <span class="hljs-attr">test</span>=<span class="hljs-string">"name != null and name != ''"</span>></span>
u.name =~ #{name}
<span class="hljs-tag"></<span class="hljs-name">if</span>></span>
<span class="hljs-tag"><<span class="hljs-name">if</span> <span class="hljs-attr">test</span>=<span class="hljs-string">"sex != null and sex != ''"</span>></span>
and u.sex = #{sex}
<span class="hljs-tag"></<span class="hljs-name">if</span>></span>
<span class="hljs-tag"></<span class="hljs-name">where</span>></span>
return
id(u) as id,u.name as name,u.age as age,u.sex as sex
<span class="hljs-tag"><<span class="hljs-name">if</span> <span class="hljs-attr">test</span>=<span class="hljs-string">"sort != null and sort != ''"</span>></span>
order by ${sort}
<span class="hljs-tag"></<span class="hljs-name">if</span>></span>
<span class="hljs-tag"><<span class="hljs-name">if</span> <span class="hljs-attr">test</span>=<span class="hljs-string">"pageStart >= 0 and pageSize > 0"</span>></span>
skip #{pageStart} limit #{pageSize}
<span class="hljs-tag"></<span class="hljs-name">if</span>></span>
<span class="hljs-tag"></<span class="hljs-name">select</span>></span>
<span class="hljs-tag"><<span class="hljs-name">select</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"getPageTotal"</span> <span class="hljs-attr">resultType</span>=<span class="hljs-string">"int"</span>></span>
match (u:user)
<span class="hljs-tag"><<span class="hljs-name">where</span>></span>
<span class="hljs-tag"><<span class="hljs-name">if</span> <span class="hljs-attr">test</span>=<span class="hljs-string">"name != null and name != ''"</span>></span>
u.name =~ #{name}
<span class="hljs-tag"></<span class="hljs-name">if</span>></span>
<span class="hljs-tag"><<span class="hljs-name">if</span> <span class="hljs-attr">test</span>=<span class="hljs-string">"sex != null and sex != ''"</span>></span>
and u.sex = #{sex}
<span class="hljs-tag"></<span class="hljs-name">if</span>></span>
<span class="hljs-tag"></<span class="hljs-name">where</span>></span>
return count(*)
<span class="hljs-tag"></<span class="hljs-name">select</span>></span>
<span class="hljs-tag"><<span class="hljs-name">select</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"getAll"</span> <span class="hljs-attr">resultMap</span>=<span class="hljs-string">"userMap"</span>></span>
match
(u:user)
return
id(u) as id,u.name as name,u.age as age,u.sex as sex
<span class="hljs-tag"></<span class="hljs-name">select</span>></span>
<span class="hljs-tag"><<span class="hljs-name">select</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"getById"</span> <span class="hljs-attr">resultMap</span>=<span class="hljs-string">"userMap"</span>></span>
match
(u:user)- [l:like] -> (u1:user)
where
id(u) = #{id}
return
id(u) as id,u.name as name,u.age as age,u.sex as sex,
id(u1) as id1,u1.name as name1,u1.age as age1,u1.sex as sex1
<span class="hljs-tag"></<span class="hljs-name">select</span>></span>
<span class="hljs-tag"><<span class="hljs-name">insert</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"add"</span>></span>
create(u:user{name:#{model.name},age:#{model.age},sex:#{model.sex}})
<span class="hljs-tag"></<span class="hljs-name">insert</span>></span>
<span class="hljs-tag"><<span class="hljs-name">update</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"update"</span>></span>
match (u:user)
where id(u) = #{model.id}
set u.name = #{model.name},u.age = #{model.age},u.sex = #{model.sex}
<span class="hljs-tag"></<span class="hljs-name">update</span>></span>
<span class="hljs-tag"><<span class="hljs-name">delete</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"deleteById"</span>></span>
match (u:user) where id(u) = #{id} delete u
<span class="hljs-tag"></<span class="hljs-name">delete</span>></span>
</mapper>
UserMapper接口
package com.study.mapper;
import com.study.model.UserModel;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface UserMapper {
<span class="hljs-built_in">List</span><UserModel> getPage(<span class="hljs-meta">@Param</span>(<span class="hljs-string">"pageStart"</span>) <span class="hljs-built_in">int</span> pageStart, <span class="hljs-meta">@Param</span>(<span class="hljs-string">"pageSize"</span>) <span class="hljs-built_in">int</span> pageSize, <span class="hljs-meta">@Param</span>(<span class="hljs-string">"sort"</span>) <span class="hljs-built_in">String</span> sort, <span class="hljs-meta">@Param</span>(<span class="hljs-string">"name"</span>) <span class="hljs-built_in">String</span> name, <span class="hljs-meta">@Param</span>(<span class="hljs-string">"sex"</span>) <span class="hljs-built_in">String</span> sex);
<span class="hljs-built_in">int</span> getPageTotal(<span class="hljs-meta">@Param</span>(<span class="hljs-string">"name"</span>) <span class="hljs-built_in">String</span> name, <span class="hljs-meta">@Param</span>(<span class="hljs-string">"sex"</span>) <span class="hljs-built_in">String</span> sex);
<span class="hljs-built_in">List</span><UserModel> getAll();
UserModel getById(<span class="hljs-meta">@Param</span>(<span class="hljs-string">"id"</span>) Long id);
<span class="hljs-built_in">int</span> add(<span class="hljs-meta">@Param</span>(<span class="hljs-string">"model"</span>) UserModel model);
<span class="hljs-built_in">int</span> update(<span class="hljs-meta">@Param</span>(<span class="hljs-string">"model"</span>) UserModel model);
<span class="hljs-built_in">int</span> deleteById(<span class="hljs-meta">@Param</span>(<span class="hljs-string">"id"</span>) Long id);
}
userModel实体类
package com.study.model;
import java.util.List;
public class UserModel {
<span class="hljs-keyword">private</span> Long id;
<span class="hljs-keyword">private</span> String name;
<span class="hljs-keyword">private</span> Integer age;
<span class="hljs-keyword">private</span> String sex;
<span class="hljs-keyword">private</span> List<UserModel> userModels;
<span class="hljs-function"><span class="hljs-keyword">public</span> Long <span class="hljs-title">getId</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> id;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setId</span><span class="hljs-params">(Long id)</span> </span>{
<span class="hljs-keyword">this</span>.id = id;
}
<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> Integer <span class="hljs-title">getAge</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> age;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAge</span><span class="hljs-params">(Integer age)</span> </span>{
<span class="hljs-keyword">this</span>.age = age;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getSex</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> sex;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setSex</span><span class="hljs-params">(String sex)</span> </span>{
<span class="hljs-keyword">this</span>.sex = sex;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> List<UserModel> <span class="hljs-title">getUserModels</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> userModels;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUserModels</span><span class="hljs-params">(List<UserModel> userModels)</span> </span>{
<span class="hljs-keyword">this</span>.userModels = userModels;
}
}
UserController控制器
package com.study.controller;
import com.study.mapper.UserMapper;
import com.study.model.UserModel;
import com.study.result.PageParam;
import com.study.result.PageResult;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
<span class="hljs-meta">@Autowired</span>
<span class="hljs-keyword">private</span> UserMapper userMapper;
<span class="hljs-comment">/**
* 分页条件查询
* 参数例:{"pageIndex":1,"pageSize":2,"sort":"u.sex desc","condition":"{'name':'','sex':'男'}"}
*
* <span class="hljs-doctag">@param</span> pageParam
* <span class="hljs-doctag">@return</span>
*/</span>
<span class="hljs-meta">@PostMapping</span>(<span class="hljs-string">"/getPage"</span>)
<span class="hljs-function"><span class="hljs-keyword">public</span> PageResult <span class="hljs-title">getPage</span><span class="hljs-params">(@RequestBody PageParam pageParam)</span> </span>{
<span class="hljs-keyword">int</span> pageStart = pageParam.getPageStart();
<span class="hljs-keyword">int</span> pageIndex = pageParam.getPageIndex();
<span class="hljs-keyword">int</span> pageSize = pageParam.getPageSize();
String sort = pageParam.getSort();
JSONObject jsonObject = <span class="hljs-keyword">new</span> JSONObject(pageParam.getCondition());
String name = <span class="hljs-string">".*"</span> + jsonObject.getString(<span class="hljs-string">"name"</span>) + <span class="hljs-string">".*"</span>; <span class="hljs-comment">//模糊查询</span>
String sex = jsonObject.getString(<span class="hljs-string">"sex"</span>);
List<UserModel> models = userMapper.getPage(pageStart, pageSize, sort, name, sex);
<span class="hljs-keyword">int</span> total = userMapper.getPageTotal(name, sex);
PageResult pageResult = <span class="hljs-keyword">new</span> PageResult(pageIndex, pageSize, total, models);
<span class="hljs-keyword">return</span> pageResult;
}
<span class="hljs-meta">@GetMapping</span>(<span class="hljs-string">"/getAll"</span>)
<span class="hljs-function"><span class="hljs-keyword">public</span> List<UserModel> <span class="hljs-title">getAll</span><span class="hljs-params">()</span> </span>{
List<UserModel> models = userMapper.getAll();
<span class="hljs-keyword">return</span> models;
}
<span class="hljs-comment">/**
* 根据id查询(含节点关系)
*
* <span class="hljs-doctag">@param</span> id
* <span class="hljs-doctag">@return</span>
*/</span>
<span class="hljs-meta">@GetMapping</span>(<span class="hljs-string">"/getById/{id}"</span>)
<span class="hljs-function"><span class="hljs-keyword">public</span> UserModel <span class="hljs-title">getById</span><span class="hljs-params">(@PathVariable Long id)</span> </span>{
UserModel model = userMapper.getById(id);
<span class="hljs-keyword">return</span> model;
}
<span class="hljs-meta">@PostMapping</span>(<span class="hljs-string">"/add"</span>)
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">add</span><span class="hljs-params">(@RequestBody UserModel model)</span> </span>{
<span class="hljs-keyword">int</span> i = userMapper.add(model);
<span class="hljs-keyword">return</span> i;
}
<span class="hljs-meta">@PostMapping</span>(<span class="hljs-string">"/update"</span>)
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">update</span><span class="hljs-params">(@RequestBody UserModel model)</span> </span>{
<span class="hljs-keyword">int</span> i = userMapper.update(model);
<span class="hljs-keyword">return</span> i;
}
<span class="hljs-meta">@DeleteMapping</span>(<span class="hljs-string">"/deleteById/{id}"</span>)
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">deleteById</span><span class="hljs-params">(@PathVariable Long id)</span> </span>{
<span class="hljs-keyword">int</span> i = userMapper.deleteById(id);
<span class="hljs-keyword">return</span> i;
}
}
要用分页条件查询请到git 上复制 PageResult,PageParam类。
最后在入口类记得加上扫描mapper接口@MapperScan(“com.study.mapper”)
原文地址:https://blog.csdn.net/WSM960921/article/details/95076242 </div>