【问题标题】:Like Button Implementation in Spring BootSpring Boot 中的 Like 按钮实现
【发布时间】:2018-05-24 18:05:46
【问题描述】:

在我的应用程序中,用户可以发布文章。其他用户可以喜欢这些文章。

文章分类:

@Entity
public class Article {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "article_id")
    private int id;

    @Column(name = "article_title")
    private String articleTitle;

    @OneToMany(mappedBy = "event")
    private List<PeopleWhoLiked> peopleWhoLiked;
}

@Entity
public class PeopleWhoLiked {
    @EmbeddedId
    private PeopleWhoLiked id;
    @ManyToOne @MapsId("articleId")
    private Article article;
    @ManyToOne @MapsId("userId")
    private User user;
}

还有分类实体。每篇文章都有一个分类。

public class Category {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "category_id")
    private int id;

    @NotEmpty
    @Column(name = "categoryName")
    private String categoryName;

    @OneToMany(mappedBy = "article")
    private List<Article> articleList;
}

我喜欢的表

Article_id User_id
x          x

这些都是相关表的外键。

category.getArticleList(); 功能我可以向用户显示文章。他们可以喜欢文章。但问题是系统不知道该文章是否已经被用户喜欢。所以总是喜欢按钮处于活动状态。

查询(Like 表上每篇文章的选择语句)看起来具有巨大的时间复杂性和系统过载。)即使我这样做,我如何才能将其发布到只有 Article 对象的 thymeleaf th:each 语句中。

我认为每次使用一个 select 语句查询 10 篇文章的点赞听起来不错。但是我又如何使用 Article 对象将它传递给 thymeleaf。

【问题讨论】:

    标签: spring hibernate spring-boot jpa spring-data-jpa


    【解决方案1】:

    您的性能问题是由对每一行的额外请求引起的。

    对于返回 100 行的 1 个选择,您需要向数据库添加 100 个选择。

    如果您需要显示复杂的结果构建视图并将视图结果映射到您的 @Entity 类,这将仅用于演示目的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-17
      • 1970-01-01
      • 1970-01-01
      • 2015-11-05
      相关资源
      最近更新 更多