Springboot+jpa部分
pom
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-configuration-processor
true
org.springframework.boot
spring-boot-starter-data-jpa
yml文件
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: #数据库用户名
password: #数据库密码
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 #测试时方便查看错误,上线可设为false
jackson:
default-property-inclusion: non_null
mysql的新版的驱动类改成了com.mysql.cj.jdbc.Driver
新版驱动连接url也有所改动需要指定时区,不然插入到数据库时间会有8小时时差
serverTimezone=GMT%2B8
启动类
在启动类上加入注解
@SpringBootApplication
@EnableJpaAuditing
public class BbsApplication {
public static void main(String[] args) {
SpringApplication.run(BbsApplication.class, args);
}
}
实体类
实体放在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;
/** 帖子标题. */
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String articleTitle;
/** 帖子内容. */
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String articleContent;
/** 帖子图片. */
private String articleImg;
/** 帖子关键词. */
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String articleKeywords;
/** 帖子浏览次数. */
private Integer articleViewNum = 0;
/** 帖子评论次数. */
private Integer articleCommentNum = 0;
/** 帖子热度指数. */
private Double articleHotNum = 0.0;
/** 帖子点赞次数. */
private Integer articleLikeNum = 0;
@CreatedDate
/** 帖子发表时间. */
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;
/** 版块编号. */
private Integer topicType;
/** 创建日期. */
@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;
/** 用户所在学院. */
private Integer userDepartment = 0;
/** 用户经验. */
private String userEx = "0";
/** 用户情感状态. */
private Integer userEmotion = EmotionEnum.UNKNOWN.getCode();
/** 用户个性签名. */
private String userShow = "";
/** 用户头像. */
private String userImg;
/** 用户粉丝数. */
private Integer userFansNum = 0;
/** 用户关注数. */
private Integer userAttentionNum = 0;
/** 用户发帖数. */
private Integer userArticleNum = 0;
/** 用户所在城市. */
private String userCity;
/** 用户所在省份. */
private String userProvince;
/** 用户所在国家. */
private String userCountry;
@CreatedDate
/** 用户注册时间. */
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;
}
用户学院
@Data
@Entity
public class Department {
/** 院系id. */
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer departmentId;
/** 院系名称. */
private String departmentName;
}
关注
@Data
@Entity
public class Attention {
/** 关注id. */
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer attentionId;
/** 被关注人id. */
private String attentionUserId;
/** 关注人id. */
private String attentionFollowerId;
/** 是否关注. */
private Integer isAttention;
}
评论
@Data
@Entity
@DynamicUpdate
@EntityListeners(AuditingEntityListener.class)
public class Comment implements Serializable {
/** 评论id. */
@Id
private String commentId;
/** 评论文章id. */
private String commentArticleId;
/** 评论用户id. */
private String commentUserId;
/** 评论内容. */
private String commentContent;
/** 评论点赞数. */
private Integer commentLikeNum = 0;
@CreatedDate
/** 评论时间. */
private Date commentTime;
}
回复
@Data
@Entity
@DynamicUpdate
@EntityListeners(AuditingEntityListener.class)
public class Reply implements Serializable {
/** 回复id. */
@Id
private String replyId;
/** 被回复评论id. */
private String replyCommentId;
/** 回复用户id. */
private String replyUserId;
/** 回复内容. */
private String replyContent;
@CreatedDate
/** 回复时间. */
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;
}
帖子点赞
@Data
@Entity
public class LikeArticle {
/** 点赞id. */
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private BigInteger likeId;
/** 被点赞文章id. */
private String likeArticleId;
/** 点赞用户id. */
private String likeUserId;
/** 是否点赞. */
private Integer isLike;
}
评论点赞
@Data
@Entity
public class LikeComment {
/** 点赞id. */
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private BigInteger likeId;
/** 被点赞评论id. */
private String likeCommentId;
/** 点赞用户id. */
private String likeUserId;
/** 是否点赞. */
private Integer isLike;
}
消息
@Data
@Entity
@DynamicUpdate
@EntityListeners(AuditingEntityListener.class)
public class Message {
/** 消息id. */
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private BigInteger messageId;
/** 消息类型,0为点赞,1为回复. */
private Integer messageType;
/** 消息对应帖子id. */
private String articleId;
/** 消息对应评论id. */
private String commentId;
/** 消息接收者id. */
private String receiverUserId;
/** 消息发送者id. */
private String senderUserId;
/** 消息回复的内容. */
private String repliedContent;
/** 消息内容. */
private String messageContent;
/** 消息是否已读,0为未读,1为已读. */
private Integer isRead = 0;
/** 消息创建时间. */
@CreatedDate
private Date messageTime;
}
Repository类
放在repository包中,继承JpaRepository使用,因为不喜欢方法名太长,我比较喜欢通过@Query注解来使用JPA,分页直接使用Pageable对象就可以,十分方便了。有些方法还没有用上,还是先写上了。
ArticelRepository
public interface ArticleRepository extends JpaRepository {
// 分页查看所有帖子
@Query("select a from Article a where a.articleIsDelete = 0 order by a.articleHotNum desc")
Page findAll(Pageable pageable);
// 查看用户所发帖子
@Query("select a from Article a where a.articleUserId = ?1 and a.articleIsDelete = 0 " +
"order by a.articleCreateTime desc")
Page findUserArticle(String userId, Pageable pageable);
// 查看被某位用户点赞的帖子
@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 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 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 {
// 根据用户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 findFollower(String userId, Pageable pageable);
// 查看某用户的关注
@Query("select u from User u, com.ccnu.bbs.entity.Attention a where " +
"u.userId = a.attentionUserId and a.attentionFollowerId = ?1")
Page 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 findCollectUser(String articleId, Pageable pageable);
// 查看点赞了某帖子的用户
@Query("select u from User u, com.ccnu.bbs.entity.LikeArticle l where " +
"u.userId = l.likeUserId and l.likeArticleId = ?1")
Page findLikeArticleUser(String articleId, Pageable pageable);
// 查看点赞了某评论的用户
@Query("select u from User u, com.ccnu.bbs.entity.LikeComment l where " +
"u.userId = l.likeUserId and l.likeCommentId = ?1")
Page findLikeCommentUser(String commentId, Pageable pageable);
}
CommentRepository
public interface CommentRepository extends JpaRepository {
// 查看某个帖子的评论(按照点赞数降序排列)
@Query("select c from Comment c where c.commentArticleId = ?1 and c.commentLikeNum > 10 order by c.commentLikeNum desc")
List findArticleCommentByLike(String articleId);
// 查看某个帖子的评论(按评论时间升序排列)
@Query("select c from Comment c where c.commentArticleId = ?1 order by c.commentTime asc")
Page findArticleCommentByTime(String articleId, Pageable pageable);
// 查看某个用户的评论((按照评论时间降序排列)
@Query("select c from Comment c where c.commentUserId = ?1 order by c.commentTime desc")
Page findUserComment(String userId, Pageable pageable);
// 查看被某个用户点赞的评论
@Query("select c from Comment c, com.ccnu.bbs.entity.LikeComment l where " +
"c.commentId = l.likeCommentId and l.likeUserId = ?1")
Page 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 {
// 查看某个评论的回复(按照回复时间升序排列)
@Query("select r from Reply r where r.replyCommentId = ?1 order by r.replyTime asc")
Page findCommentReply(String commentId, Pageable pageable);
// 查看某个用户的回复(按照回复时间降序排列)
@Query("select r from Reply r where r.replyUserId = ?1 order by r.replyTime desc")
Page findUserReply(String userId, Pageable pageable);
}
LikeAritcleRepository
public interface LikeArticleRepository extends JpaRepository {
// 查询被某用户点赞的帖子的记录
@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 {
// 查询被某用户点赞的评论的记录
@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 {
// 查询被某用户收藏的帖子记录
@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 {
// 按照消息类型查找全部消息
@Query("select m from Message m where m.receiverUserId = ?1 and m.messageType = ?2 order by m.messageTime desc")
Page 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);
}
这些做完之后就可以使用我们创建的方法进行相应的数据库操作啦!下一章将会介绍redis以及微信小程序的登录模块~
上一篇:springboot+jpa+redis+quzartz+elasticsearch实现微信论坛小程序(一)
下一篇:springboot+jpa+redis+quzartz+elasticsearch实现微信论坛小程序(三)