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)微信論壇小程序(三)