码迷,mamicode.com
首页 > 编程语言 > 详细

JavaWeb_(视频网址)_二、用户模块3

时间:2019-12-13 23:26:51      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:标准   网站   this   tco   mysq   sql   private   get   control   

 

 

用户关注

技术图片

 

技术图片

 

  User.java中创建一个Set<>集合的User对象(多对多的关系)

  //该用户关注了哪些用户  在保存的时候,会级联保存所有临时对象
    @ManyToMany(cascade = CascadeType.PERSIST)
    @JoinTable(
            name="user_follow",
            joinColumns = @JoinColumn(name = "user_id"),
            inverseJoinColumns = @JoinColumn(name = "follow_id")
            )
    private Set<User> follows = new HashSet<User>();
    

 

技术图片
package com.Gary.betobe.domain;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;

@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    private String username;
    private String password;
    private String email;
    
    private String firstName;
    private String lastName;
    
    private String displayNane;
    //个人首页
    private String webUrl;
    
    private String phone;
    //个人描述
    @Lob
    //长文本 ->lob对应mysql的数据类型  longtext
    private String description;
    //social Link
    private String qqLink;
    private String weixinLink;
    
    //封面头像
    private String coverImage;
    //头像
    private String headImage;
    //创建时间
    private String createTime;
    
    //该用户关注了哪些用户  在保存的时候,会级联保存所有临时对象
    @ManyToMany(cascade = CascadeType.PERSIST)
    @JoinTable(
            name="user_follow",
            joinColumns = @JoinColumn(name = "user_id"),
            inverseJoinColumns = @JoinColumn(name = "follow_id")
            )
    private Set<User> follows = new HashSet<User>();
    
    //JPA的标准
    protected User()
    {
        
    }
    
    
    
    public User(Long id, String username, String password, String email, String firstName, String lastName,
            String displayNane, String webUrl, String phone, String description, String qqLink, String weixinLink,
            String coverImage, String headImage, String createTime, Set<User> follows) {
        super();
        this.id = id;
        this.username = username;
        this.password = password;
        this.email = email;
        this.firstName = firstName;
        this.lastName = lastName;
        this.displayNane = displayNane;
        this.webUrl = webUrl;
        this.phone = phone;
        this.description = description;
        this.qqLink = qqLink;
        this.weixinLink = weixinLink;
        this.coverImage = coverImage;
        this.headImage = headImage;
        this.createTime = createTime;
        this.follows = follows;
    }



    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getDisplayNane() {
        return displayNane;
    }
    public void setDisplayNane(String displayNane) {
        this.displayNane = displayNane;
    }
    public String getWebUrl() {
        return webUrl;
    }
    public void setWebUrl(String webUrl) {
        this.webUrl = webUrl;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getQqLink() {
        return qqLink;
    }
    public void setQqLink(String qqLink) {
        this.qqLink = qqLink;
    }
    public String getWeixinLink() {
        return weixinLink;
    }
    public void setWeixinLink(String weixinLink) {
        this.weixinLink = weixinLink;
    }
    public String getCoverImage() {
        return coverImage;
    }
    public void setCoverImage(String coverImage) {
        this.coverImage = coverImage;
    }
    public String getHeadImage() {
        return headImage;
    }
    public void setHeadImage(String headImage) {
        this.headImage = headImage;
    }
    public String getCreateTime() {
        return createTime;
    }
    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public Set<User> getFollows() {
        return follows;
    }

    public void setFollows(Set<User> follows) {
        this.follows = follows;
    }

    
    
    
}
User.java

 

  运行项目,数据库中多出一张user_follow表

  技术图片

 

 

  UserController.java中实现addFollows请求,传入两个参数,表示谁(id)关注了谁(followId)

  @RequestMapping("/addFollows")
    private String addFollows(String id,String followId)
    {
        User user = userService.findUserById(id);
        User followUser = userService.findUserById(followId);
        //在id的follow集合中添加关系
        user.getFollows().add(followUser);
        //保存关系
        userService.saveUser(user);
        //System.err.println("123123123");
        return "redirect:/index";
    }

 

  测试:登陆网站,模拟用户发送请求,数据库存在id为1的用户和id为10的用户,通过发送www.pinzhi365.com/addFollows?id=1&followId=10请求,让数据库中id为1的用户关注id为10的用户

技术图片

JavaWeb_(视频网址)_二、用户模块3

标签:标准   网站   this   tco   mysq   sql   private   get   control   

原文地址:https://www.cnblogs.com/1138720556Gary/p/12037409.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!