电影网站增删改查 spring boots/MVC/neo4j 源码分析-2 DATA 实体建模(noe4j)

扫码关注公众号:Java 技术驿站

发送:vip
将链接复制到本浏览器,永久解锁本站全部文章

【公众号:Java 技术驿站】 【加作者微信交流技术,拉技术群】

模型设计部分:分为节点实体和关系实体

两个节点实体 actor , movie , 一个关系实体 actor in movies (roles)

前提, NEO4J 必须配置

1.neo4j依赖
spring-data-neo4j
jackson-jsog

2.config resources/ogm.properties

actor.java

    package com.test.data.domain;

    import com.fasterxml.jackson.annotation.JsonIdentityInfo;
    import com.voodoodyne.jackson.jsog.JSOGGenerator;
    import org.neo4j.ogm.annotation.GraphId;
    import org.neo4j.ogm.annotation.NodeEntity;
    import org.neo4j.ogm.annotation.typeconversion.DateLong;
    import org.springframework.format.annotation.DateTimeFormat;

    import java.util.Date;

    @JsonIdentityInfo(generator=JSOGGenerator.class)//防止查询数据的时候引发递归效应
    @NodeEntity //节点实体
    public class Actor {
        @GraphId //节点唯一标识 , 系统自动生成
        Long id;
        private String name;
        private int sex;
        @DateLong
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")//neo4j 没有日期格式数据类型,用注解进行转换
        private Date born;

        public Actor() { }

        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 int getSex() {
            return sex;
        }

        public void setSex(int sex) {
            this.sex = sex;
        }

        public Date getBorn() {
            return born;
        }

        public void setBorn(Date born) {
            this.born = born;
        }
    }

movie.java;

    package com.test.data.domain;

    import com.fasterxml.jackson.annotation.JsonIdentityInfo;
    import com.voodoodyne.jackson.jsog.JSOGGenerator;
    import org.neo4j.ogm.annotation.GraphId;
    import org.neo4j.ogm.annotation.NodeEntity;
    import org.neo4j.ogm.annotation.Relationship;
    import org.neo4j.ogm.annotation.typeconversion.DateLong;
    import org.springframework.format.annotation.DateTimeFormat;

    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;

    @JsonIdentityInfo(generator=JSOGGenerator.class)
    @NodeEntity
    public class Movie {
        @GraphId
        Long id;
        private String name;
        private String photo;
        @DateLong
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private Date createDate;

        @Relationship(type="扮演", direction = Relationship.INCOMING)
        List<Role> roles = new ArrayList<>();//关系列表,泛型 指只能存放所有角色的集合

        public Role addRole(Actor actor, String name){ //增加关系的方法
            Role role = new Role(actor,this,name);
            this.roles.add(role);
            return role;
        }

        public Movie() { }

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public List<Role> getRoles() {
            return roles;
        }

        public void setRoles(List<Role> roles) {
            this.roles = roles;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getPhoto() {
            return photo;
        }

        public void setPhoto(String photo) {
            this.photo = photo;
        }

        public Date getCreateDate() {
            return createDate;
        }

        public void setCreateDate(Date createDate) {
            this.createDate = createDate;
        }
    }

Role.java

    package com.test.data.domain;

    import com.fasterxml.jackson.annotation.JsonIdentityInfo;
    import com.voodoodyne.jackson.jsog.JSOGGenerator;
    import org.neo4j.ogm.annotation.EndNode;
    import org.neo4j.ogm.annotation.GraphId;
    import org.neo4j.ogm.annotation.RelationshipEntity;
    import org.neo4j.ogm.annotation.StartNode;

    @JsonIdentityInfo(generator=JSOGGenerator.class)
    @RelationshipEntity(type = "扮演")
    public class Role {
        @GraphId
        Long id;
        String name;
        @StartNode //开始节点
        Actor actor;
        @EndNode //结束节点
        Movie movie;

        public Role() {  //空的构造函数
        }

        public Role(Actor actor, Movie movie, String name) {//构造函数的重载
            this.actor = actor;
            this.movie = movie;
            this.name = name;
        }

        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 Actor getActor() {
            return actor;
        }

        public Movie getMovie() {
            return movie;
        }

    }

分页代码粗略一看不懂, 结合前端再研究:

    package com.test.data.service;

    import org.neo4j.ogm.cypher.Filters;
    import org.neo4j.ogm.cypher.query.Pagination;
    import org.neo4j.ogm.cypher.query.SortOrder;
    import org.neo4j.ogm.session.Session;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.PageImpl;
    import org.springframework.data.domain.Pageable;
    import org.springframework.data.domain.Sort;
    import org.springframework.stereotype.Service;

    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Iterator;
    import java.util.List;

    @Service
    //分页 Pageable pageable, List<T> results 页数设定和排序字段设定
    public class PagesService<T> {
        @Autowired
        private Session session;

        public Page<T> findAll(Class<T> clazz, Pageable pageable, Filters filters){
            Collection data = this.session.loadAll(clazz, filters, convert(pageable.getSort()), new Pagination(pageable.getPageNumber(), pageable.getPageSize()), 1);
            return updatePage(pageable, new ArrayList(data));
        }

        private Page<T> updatePage(Pageable pageable, List<T> results) {
            int pageSize = pageable.getPageSize();
            int pageOffset = pageable.getOffset();
            int total = pageOffset + results.size() + (results.size() == pageSize?pageSize:0);
            return new PageImpl(results, pageable, (long)total);
        }

        private SortOrder convert(Sort sort) {
            SortOrder sortOrder = new SortOrder();
            if(sort != null) {
                Iterator var3 = sort.iterator();

                while(var3.hasNext()) {
                    Sort.Order order = (Sort.Order)var3.next();
                    if(order.isAscending()) {
                        sortOrder.add(new String[]{order.getProperty()});
                    } else {
                        sortOrder.add(SortOrder.Direction.DESC, new String[]{order.getProperty()});
                    }
                }
            }
            return sortOrder;
        }
    }

来源:[]()

赞(0) 打赏
版权归原创作者所有,任何形式的转载请联系博主:daming_90:Java 技术驿站 » 电影网站增删改查 spring boots/MVC/neo4j 源码分析-2 DATA 实体建模(noe4j)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏