springboot+jpa+redis+quzartz+elasticsearch實(shí)現(xiàn)微信論壇小程序(二)

Springboot+jpa部分

pom

        <!--springboot依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <!--JPA-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

yml文件

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: #數(shù)據(jù)庫用戶名
    password: #數(shù)據(jù)庫密碼
    url: jdbc:mysql://localhost/bbs?characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8
  jpa:
    properties:
      hibernate:
        hbm2ddl:
          auto: update
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect
    show-sql: true #測試時(shí)方便查看錯(cuò)誤,上線可設(shè)為false
  jackson:
    default-property-inclusion: non_null

mysql的新版的驅(qū)動(dòng)類改成了com.mysql.cj.jdbc.Driver
新版驅(qū)動(dòng)連接url也有所改動(dòng)需要指定時(shí)區(qū)拌消,不然插入到數(shù)據(jù)庫時(shí)間會(huì)有8小時(shí)時(shí)差

serverTimezone=GMT%2B8 

啟動(dòng)類

在啟動(dòng)類上加入注解

@SpringBootApplication
@EnableJpaAuditing
public class BbsApplication {

    public static void main(String[] args) {
        SpringApplication.run(BbsApplication.class, args);
    }

}

實(shí)體類

實(shí)體放在entity包中

帖子

@Data
@Entity
@DynamicUpdate
@EntityListeners(AuditingEntityListener.class)
@Document(indexName="bbs",type="article")
public class Article implements Serializable {

    /** 帖子id. */
    @Id
    @org.springframework.data.annotation.Id
    private String articleId;
    /** 帖子所屬版塊id. */
    private Integer articleTopicType;
    /** 用戶id. */
    private String articleUserId;
    /** 帖子標(biāo)題. */
    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String articleTitle;
    /** 帖子內(nèi)容. */
    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String articleContent;
    /** 帖子圖片. */
    private String articleImg;
    /** 帖子關(guān)鍵詞. */
    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String articleKeywords;
    /** 帖子瀏覽次數(shù). */
    private Integer articleViewNum = 0;
    /** 帖子評(píng)論次數(shù). */
    private Integer articleCommentNum = 0;
    /** 帖子熱度指數(shù). */
    private Double articleHotNum = 0.0;
    /** 帖子點(diǎn)贊次數(shù). */
    private Integer articleLikeNum = 0;
    @CreatedDate
    /** 帖子發(fā)表時(shí)間. */
    private Date articleCreateTime;
    /** 帖子是否被刪除(0表示未刪除,1表示已刪除). */
    private Integer articleIsDelete = 0;
}

板塊

@Data
@Entity
@DynamicUpdate
public class Topic {

    /** 版塊id. */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer topicId;
    /** 版塊名稱. */
    private String topicName;
    /** 版塊編號(hào). */
    private Integer topicType;
    /** 創(chuàng)建日期. */
    @CreatedDate
    private Date topicCreateTime;
    /** 更新日期. */
    @LastModifiedDate
    private Date topicUpdateTime;
}

輪播圖

@Data
@Entity
public class Slideshow {

    /** 輪播圖id. */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer slideshowId;
    /** 版塊類型. */
    private Integer topicType;
    /** 圖片url. */
    private String imgUrl;
}

用戶

@Data
@Entity
@DynamicUpdate
@EntityListeners(AuditingEntityListener.class)
public class User implements Serializable {

    /** 用戶id. */
    @Id
    private String userId;
    /** 用戶昵稱. */
    private String userName;
    /** 用戶身份. */
    private Integer userRoleType;
    /** 用戶性別. */
    private Integer userGender;
    /** 用戶所在學(xué)院. */
    private Integer userDepartment = 0;
    /** 用戶經(jīng)驗(yàn). */
    private String userEx = "0";
    /** 用戶情感狀態(tài). */
    private Integer userEmotion = EmotionEnum.UNKNOWN.getCode();
    /** 用戶個(gè)性簽名. */
    private String userShow = "";
    /** 用戶頭像. */
    private String userImg;
    /** 用戶粉絲數(shù). */
    private Integer userFansNum = 0;
    /** 用戶關(guān)注數(shù). */
    private Integer userAttentionNum = 0;
    /** 用戶發(fā)帖數(shù). */
    private Integer userArticleNum = 0;
    /** 用戶所在城市. */
    private String userCity;
    /** 用戶所在省份. */
    private String userProvince;
    /** 用戶所在國家. */
    private String userCountry;
    @CreatedDate
    /** 用戶注冊(cè)時(shí)間. */
    private Date userTime;
}

用戶角色

@Data
@Entity
public class Role implements Serializable {

    /** 身份id. */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer roleId;
    /** 身份類型. */
    private Integer roleType;
    /** 身份名稱. */
    private String roleName;
}

用戶學(xué)院

@Data
@Entity
public class Department {

    /** 院系id. */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer departmentId;
    /** 院系名稱. */
    private String departmentName;
}

關(guān)注

@Data
@Entity
public class Attention {

    /** 關(guān)注id. */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer attentionId;
    /** 被關(guān)注人id. */
    private String attentionUserId;
    /** 關(guān)注人id. */
    private String attentionFollowerId;
    /** 是否關(guān)注. */
    private Integer isAttention;
}

評(píng)論

@Data
@Entity
@DynamicUpdate
@EntityListeners(AuditingEntityListener.class)
public class Comment implements Serializable {

    /** 評(píng)論id. */
    @Id
    private String commentId;
    /** 評(píng)論文章id. */
    private String commentArticleId;
    /** 評(píng)論用戶id. */
    private String commentUserId;
    /** 評(píng)論內(nèi)容. */
    private String commentContent;
    /** 評(píng)論點(diǎn)贊數(shù). */
    private Integer commentLikeNum = 0;
    @CreatedDate
    /** 評(píng)論時(shí)間. */
    private Date commentTime;
}

回復(fù)

@Data
@Entity
@DynamicUpdate
@EntityListeners(AuditingEntityListener.class)
public class Reply implements Serializable {

    /** 回復(fù)id. */
    @Id
    private String replyId;
    /** 被回復(fù)評(píng)論id. */
    private String replyCommentId;
    /** 回復(fù)用戶id. */
    private String replyUserId;
    /** 回復(fù)內(nèi)容. */
    private String replyContent;
    @CreatedDate
    /** 回復(fù)時(shí)間. */
    private Date replyTime;
}

收藏

@Data
@Entity
public class Collect {

    /** 收藏id. */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer collectId;
    /** 收藏文章id. */
    private String collectArticleId;
    /** 收藏用戶id. */
    private String collectUserId;
    /** 是否收藏. */
    private Integer isCollect;
}

帖子點(diǎn)贊

@Data
@Entity
public class LikeArticle {

    /** 點(diǎn)贊id. */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private BigInteger likeId;
    /** 被點(diǎn)贊文章id. */
    private String likeArticleId;
    /** 點(diǎn)贊用戶id. */
    private String likeUserId;
    /** 是否點(diǎn)贊. */
    private Integer isLike;
}

評(píng)論點(diǎn)贊

@Data
@Entity
public class LikeComment {

    /** 點(diǎn)贊id. */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private BigInteger likeId;
    /** 被點(diǎn)贊評(píng)論id. */
    private String likeCommentId;
    /** 點(diǎn)贊用戶id. */
    private String likeUserId;
    /** 是否點(diǎn)贊. */
    private Integer isLike;
}

消息

@Data
@Entity
@DynamicUpdate
@EntityListeners(AuditingEntityListener.class)
public class Message {

    /** 消息id. */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private BigInteger messageId;
    /** 消息類型,0為點(diǎn)贊,1為回復(fù). */
    private Integer messageType;
    /** 消息對(duì)應(yīng)帖子id. */
    private String articleId;
    /** 消息對(duì)應(yīng)評(píng)論id. */
    private String commentId;
    /** 消息接收者id. */
    private String receiverUserId;
    /** 消息發(fā)送者id. */
    private String senderUserId;
    /** 消息回復(fù)的內(nèi)容. */
    private String repliedContent;
    /** 消息內(nèi)容. */
    private String messageContent;
    /** 消息是否已讀,0為未讀,1為已讀. */
    private Integer isRead = 0;
    /** 消息創(chuàng)建時(shí)間. */
    @CreatedDate
    private Date messageTime;
}

Repository類

放在repository包中,繼承JpaRepository使用蟆技,因?yàn)椴幌矚g方法名太長沫换,我比較喜歡通過@Query注解來使用JPA,分頁直接使用Pageable對(duì)象就可以君躺,十分方便了横腿。有些方法還沒有用上,還是先寫上了粒褒。

ArticelRepository

public interface ArticleRepository extends JpaRepository<Article, String> {

    // 分頁查看所有帖子
    @Query("select a from Article a where a.articleIsDelete = 0 order by a.articleHotNum desc")
    Page<Article> findAll(Pageable pageable);

    // 查看用戶所發(fā)帖子
    @Query("select a from Article a where a.articleUserId = ?1 and a.articleIsDelete = 0 " +
            "order by a.articleCreateTime desc")
    Page<Article> findUserArticle(String userId, Pageable pageable);

    // 查看被某位用戶點(diǎn)贊的帖子
    @Query("select a from Article a, com.ccnu.bbs.entity.LikeArticle l where " +
            "a.articleId = l.likeArticleId and l.likeUserId = ?1 order by a.articleCreateTime desc ")
    Page<Article> findUserLike(String userId, Pageable pageable);

    // 查看被某位用戶收藏的帖子
    @Query("select a from Article a, com.ccnu.bbs.entity.Collect c where " +
            "a.articleId = c.collectArticleId and c.collectUserId = ?1 order by a.articleCreateTime desc ")
    Page<Article> findUserCollect(String userId, Pageable pageable);

    @Query("select a from Article a where a.articleId = ?1")
    Article findArticle(String articleId);
}

UserRepository

public interface UserRepository extends JpaRepository<User, String> {

    // 根據(jù)用戶id查找用戶
    User findByUserId(String userId);

    // 查看某用戶的粉絲
    @Query("select u from User u, com.ccnu.bbs.entity.Attention a where " +
            "u.userId = a.attentionFollowerId and a.attentionUserId = ?1")
    Page<User> findFollower(String userId, Pageable pageable);

    // 查看某用戶的關(guān)注
    @Query("select u from User u, com.ccnu.bbs.entity.Attention a where " +
            "u.userId = a.attentionUserId and a.attentionFollowerId = ?1")
    Page<User> findAttention(String userId, Pageable pageable);

    // 查看收藏了某帖子的用戶
    @Query("select u from User u, com.ccnu.bbs.entity.Collect c where " +
            "u.userId = c.collectUserId and c.collectArticleId = ?1")
    Page<User> findCollectUser(String articleId, Pageable pageable);

    // 查看點(diǎn)贊了某帖子的用戶
    @Query("select u from User u, com.ccnu.bbs.entity.LikeArticle l where " +
            "u.userId = l.likeUserId and l.likeArticleId = ?1")
    Page<User> findLikeArticleUser(String articleId, Pageable pageable);

    // 查看點(diǎn)贊了某評(píng)論的用戶
    @Query("select u from User u, com.ccnu.bbs.entity.LikeComment l where " +
            "u.userId = l.likeUserId and l.likeCommentId = ?1")
    Page<User> findLikeCommentUser(String commentId, Pageable pageable);
}

CommentRepository

public interface CommentRepository extends JpaRepository<Comment, String> {

    // 查看某個(gè)帖子的評(píng)論(按照點(diǎn)贊數(shù)降序排列)
    @Query("select c from Comment c where c.commentArticleId = ?1 and c.commentLikeNum > 10 order by c.commentLikeNum desc")
    List<Comment> findArticleCommentByLike(String articleId);

    // 查看某個(gè)帖子的評(píng)論(按評(píng)論時(shí)間升序排列)
    @Query("select c from Comment c where c.commentArticleId = ?1 order by c.commentTime asc")
    Page<Comment> findArticleCommentByTime(String articleId, Pageable pageable);

    // 查看某個(gè)用戶的評(píng)論((按照評(píng)論時(shí)間降序排列)
    @Query("select c from Comment c where c.commentUserId = ?1 order by c.commentTime desc")
    Page<Comment> findUserComment(String userId, Pageable pageable);

    // 查看被某個(gè)用戶點(diǎn)贊的評(píng)論
    @Query("select c from Comment c, com.ccnu.bbs.entity.LikeComment l where " +
            "c.commentId = l.likeCommentId and l.likeUserId = ?1")
    Page<Comment> findUserLike(String userId, Pageable pageable);

    @Query("select c from Comment c where c.commentId = ?1")
    Comment findComment(String commentId);
}

ReplyRepository

public interface ReplyRepository extends JpaRepository<Reply, String> {

    // 查看某個(gè)評(píng)論的回復(fù)(按照回復(fù)時(shí)間升序排列)
    @Query("select r from Reply r where r.replyCommentId = ?1 order by r.replyTime asc")
    Page<Reply> findCommentReply(String commentId, Pageable pageable);

    // 查看某個(gè)用戶的回復(fù)(按照回復(fù)時(shí)間降序排列)
    @Query("select r from Reply r where r.replyUserId = ?1 order by r.replyTime desc")
    Page<Reply> findUserReply(String userId, Pageable pageable);

}

LikeAritcleRepository

public interface LikeArticleRepository extends JpaRepository<LikeArticle, BigInteger> {

    // 查詢被某用戶點(diǎn)贊的帖子的記錄
    @Query("select l from LikeArticle l where l.likeArticleId = ?1 and l.likeUserId = ?2")
    LikeArticle findLikeArticle(String articleId, String userId);
}

LikeCommentRepository

public interface LikeCommentRepository extends JpaRepository<LikeComment, BigInteger> {

    // 查詢被某用戶點(diǎn)贊的評(píng)論的記錄
    @Query("select l from LikeComment l where l.likeCommentId = ?1 and l.likeUserId = ?2")
    LikeComment findLikeComment(String commentId, String userId);
}

CollectRepository

public interface CollectRepository extends JpaRepository<Collect, Integer> {

    // 查詢被某用戶收藏的帖子記錄
    @Query("select c from Collect c where c.collectArticleId = ?1 and c.collectUserId = ?2")
    Collect findCollect(String articleId, String userId);
}

MessageRepository

public interface MessageRepository extends JpaRepository<Message, BigInteger> {

    // 按照消息類型查找全部消息
    @Query("select m from Message m where m.receiverUserId = ?1 and m.messageType = ?2 order by m.messageTime desc")
    Page<Message> findMessage(String receiverUserId, Integer messageType, Pageable pageable);

    // 按照消息類型查找是否有新消息
    @Query("select count(m) from Message m where m.receiverUserId = ?1 and m.messageType = ?2 and m.isRead = 0")
    Integer haveNewMessage(String receiverUserId, Integer messageType);

    @Transactional
    void deleteByMessageId(BigInteger messageId);
}

這些做完之后就可以使用我們創(chuàng)建的方法進(jìn)行相應(yīng)的數(shù)據(jù)庫操作啦识颊!下一章將會(huì)介紹redis以及微信小程序的登錄模塊~
上一篇:springboot+jpa+redis+quzartz+elasticsearch實(shí)現(xiàn)微信論壇小程序(一)
下一篇:springboot+jpa+redis+quzartz+elasticsearch實(shí)現(xiàn)微信論壇小程序(三)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市奕坟,隨后出現(xiàn)的幾起案子祥款,更是在濱河造成了極大的恐慌清笨,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,198評(píng)論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件镰踏,死亡現(xiàn)場離奇詭異函筋,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)奠伪,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門跌帐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人绊率,你說我怎么就攤上這事谨敛。” “怎么了滤否?”我有些...
    開封第一講書人閱讀 167,643評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵脸狸,是天一觀的道長。 經(jīng)常有香客問我藐俺,道長炊甲,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,495評(píng)論 1 296
  • 正文 為了忘掉前任欲芹,我火速辦了婚禮卿啡,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘菱父。我一直安慰自己颈娜,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,502評(píng)論 6 397
  • 文/花漫 我一把揭開白布浙宜。 她就那樣靜靜地躺著官辽,像睡著了一般。 火紅的嫁衣襯著肌膚如雪粟瞬。 梳的紋絲不亂的頭發(fā)上同仆,一...
    開封第一講書人閱讀 52,156評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音裙品,去河邊找鬼俗批。 笑死,一個(gè)胖子當(dāng)著我的面吹牛清酥,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播蕴侣,決...
    沈念sama閱讀 40,743評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼焰轻,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼!你這毒婦竟也來了昆雀?” 一聲冷哼從身側(cè)響起辱志,我...
    開封第一講書人閱讀 39,659評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤蝠筑,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后揩懒,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體什乙,經(jīng)...
    沈念sama閱讀 46,200評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,282評(píng)論 3 340
  • 正文 我和宋清朗相戀三年已球,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了臣镣。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,424評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡智亮,死狀恐怖忆某,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情阔蛉,我是刑警寧澤弃舒,帶...
    沈念sama閱讀 36,107評(píng)論 5 349
  • 正文 年R本政府宣布,位于F島的核電站状原,受9級(jí)特大地震影響聋呢,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜颠区,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,789評(píng)論 3 333
  • 文/蒙蒙 一削锰、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧瓦呼,春花似錦喂窟、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至质和,卻和暖如春稳摄,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背饲宿。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評(píng)論 1 271
  • 我被黑心中介騙來泰國打工厦酬, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人瘫想。 一個(gè)月前我還...
    沈念sama閱讀 48,798評(píng)論 3 376
  • 正文 我出身青樓仗阅,卻偏偏與公主長得像,于是被迫代替她去往敵國和親国夜。 傳聞我的和親對(duì)象是個(gè)殘疾皇子减噪,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,435評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容