您的位置:首页 > 其它

spingBoot myBatis neo4j整合项目案例

2019-07-08 17:36 295 查看
此项目为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>

<groupId>springboot-neo4j-mybatis</groupId>
<artifactId>springboot-neo4j-mybatis</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<!--spring boot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>2.1.2.RELEASE</version>
</dependency>

<!--org.json-->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>

<!--neo4j-jdbc-driver-->
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc-driver</artifactId>
<version>3.4.0</version>
</dependency>

<!--mybatis-spring-boot-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</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"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<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>

<!--这里CQL语句返回的字段名必须与上面定义的结果集的字段名一致,若不一致需要重命名,否则会报空指针异常 -->
<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<UserModel> 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<UserModel> 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<UserModel> 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<UserModel> getUserModels() {
return userModels;
}

public void setUserModels(List<UserModel> 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<UserModel> 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<UserModel> getAll() {
List<UserModel> 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”)

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: