此项目为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.0modelVersion>
<groupId>springboot-neo4j-mybatisgroupId>
<artifactId>springboot-neo4j-mybatisartifactId>
<version>1.0-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>2.1.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<version>2.1.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.jsongroupId>
<artifactId>jsonartifactId>
<version>20180813version>
dependency>
<dependency>
<groupId>org.neo4jgroupId>
<artifactId>neo4j-jdbc-driverartifactId>
<version>3.4.0version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.0.1version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
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"?>
<mapper namespace="com.study.mapper.UserMapper">
<resultMap id="userMap" type="com.study.model.UserModel">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<result property="sex" column="sex"/>
<collection property="userModels" ofType="com.study.model.UserModel">
<id property="id" column="id1"/>
<result property="name" column="name1"/>
<result property="age" column="age1"/>
<result property="sex" column="sex1"/>
collection>
resultMap>
<select id="getPage" resultMap="userMap">
match (u:user)
<where>
<if test="name != null and name != ''">
u.name =~ #{name}
if>
<if test="sex != null and sex != ''">
and u.sex = #{sex}
if>
where>
return
id(u) as id,u.name as name,u.age as age,u.sex as sex
<if test="sort != null and sort != ''">
order by ${sort}
if>
<if test="pageStart >= 0 and pageSize > 0">
skip #{pageStart} limit #{pageSize}
if>
select>
<select id="getPageTotal" resultType="int">
match (u:user)
<where>
<if test="name != null and name != ''">
u.name =~ #{name}
if>
<if test="sex != null and sex != ''">
and u.sex = #{sex}
if>
where>
return count(*)
select>
<select id="getAll" resultMap="userMap">
match
(u:user)
return
id(u) as id,u.name as name,u.age as age,u.sex as sex
select>
<select id="getById" resultMap="userMap">
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
select>
<insert id="add">
create(u:user{name:#{model.name},age:#{model.age},sex:#{model.sex}})
insert>
<update id="update">
match (u:user)
where id(u) = #{model.id}
set u.name = #{model.name},u.age = #{model.age},u.sex = #{model.sex}
update>
<delete id="deleteById">
match (u:user) where id(u) = #{id} delete u
delete>
mapper>
UserMapper接口
package com.study.mapper;
import com.study.model.UserModel;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface UserMapper {
List getPage(@Param("pageStart") int pageStart, @Param("pageSize") int pageSize, @Param("sort") String sort, @Param("name") String name, @Param("sex") String sex);
int getPageTotal(@Param("name") String name, @Param("sex") String sex);
List getAll();
UserModel getById(@Param("id") Long id);
int add(@Param("model") UserModel model);
int update(@Param("model") UserModel model);
int deleteById(@Param("id") Long id);
}
userModel实体类
package com.study.model;
import java.util.List;
public class UserModel {
private Long id;
private String name;
private Integer age;
private String sex;
private List userModels;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public List getUserModels() {
return userModels;
}
public void setUserModels(List userModels) {
this.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 {
@Autowired
private UserMapper userMapper;
/**
* 分页条件查询
* 参数例:{"pageIndex":1,"pageSize":2,"sort":"u.sex desc","condition":"{'name':'','sex':'男'}"}
*
* @param pageParam
* @return
*/
@PostMapping("/getPage")
public PageResult getPage(@RequestBody PageParam pageParam) {
int pageStart = pageParam.getPageStart();
int pageIndex = pageParam.getPageIndex();
int pageSize = pageParam.getPageSize();
String sort = pageParam.getSort();
JSONObject jsonObject = new JSONObject(pageParam.getCondition());
String name = ".*" + jsonObject.getString("name") + ".*"; //模糊查询
String sex = jsonObject.getString("sex");
List models = userMapper.getPage(pageStart, pageSize, sort, name, sex);
int total = userMapper.getPageTotal(name, sex);
PageResult pageResult = new PageResult(pageIndex, pageSize, total, models);
return pageResult;
}
@GetMapping("/getAll")
public List getAll() {
List models = userMapper.getAll();
return models;
}
/**
* 根据id查询(含节点关系)
*
* @param id
* @return
*/
@GetMapping("/getById/{id}")
public UserModel getById(@PathVariable Long id) {
UserModel model = userMapper.getById(id);
return model;
}
@PostMapping("/add")
public int add(@RequestBody UserModel model) {
int i = userMapper.add(model);
return i;
}
@PostMapping("/update")
public int update(@RequestBody UserModel model) {
int i = userMapper.update(model);
return i;
}
@DeleteMapping("/deleteById/{id}")
public int deleteById(@PathVariable Long id) {
int i = userMapper.deleteById(id);
return i;
}
}
要用分页条件查询请到git 上复制 PageResult,PageParam类。
最后在入口类记得加上扫描mapper接口@MapperScan(“com.study.mapper”)
原文地址:https://blog.csdn.net/WSM960921/article/details/95076242