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>

    <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”)

原文地址:https://blog.csdn.net/WSM960921/article/details/95076242

转载于:https://www.cnblogs.com/jpfss/p/11249690.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值