校园论坛(Java)—— 考研学习模块
文章目录
1、写在前面
- Windows版本:Windows10
- JDK版本:Java8
- MySQL版本:MySQL5.7
- Tomcat版本:Tomcat9.0
- IDE:IntelliJ IDEA Ultimate2020.2.3
2、系统结构设计
2.1 各个页面之间的调用关系
考研学习模块
TouristStudy.jsp
:游客模式下,提示登录使用发表功能touristStudyReply.jsp
:游客模式下,只有查看学习专帖的权限,提示登录使用回复等功能studyList.jsp
:学习专帖的展示页面study.jsp
:发表学习专帖releaseStudyJudge.jsp
:学习专帖标题判空操作studyReply.jsp
:查看回帖信息MdeleteUserStudyServlet
:当前登录用户或管理员删除当前登录的学习专帖UpdeleteStudyReplyByIdServlet
:当前登录用户删除当前登录的学习专帖回帖userlist.jsp
:所有用户包括管理员自身的用户信息列表
2.2. 登录注册模块各层的设计
- Entity层
实体层声明学习专帖的
sid、title、content、cover、createTime
等变量以及对应的set、get方法
public class StudyEntity {
private int sid;
private String title;
private String content;
private String cover;
private Timestamp createTime;
//关联用户,多对一额关联
private UserEntity author = new UserEntity();
//关联回复
private List<StudyReplyEntity> studyReplyList = new ArrayList<>();
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Timestamp getCreateTime() {
return createTime;
}
public void setCreateTime(Timestamp createTime) {
this.createTime = createTime;
}
public String getCover() {
return cover;
}
public void setCover(String cover) {
this.cover = cover;
}
public UserEntity getAuthor() {
return author;
}
public void setAuthor(UserEntity author) {
this.author = author;
}
public List<StudyReplyEntity> getStudyReplyList() {
return studyReplyList;
}
public void setStudyReplyList(List<StudyReplyEntity> studyReplyList) {
this.studyReplyList = studyReplyList;
}
}
- Dao层
Dao层分别实现一个
interface
接口类以及一个接口方法
实现类
接口类:
public interface StudyDaoInf {
// TODO 查询所有考研学习信息
public List<StudyEntity> findStudyList();
// TODO 根据ID查询某个考研帖子
public StudyEntity findStudyById(int id);
// TODO 添加考研帖子
public int addStudy(StudyEntity study);
// TODO 根据考研帖子的ID查询所有回复
public List<StudyReplyEntity> findStudyReplyList(int fid);
// TODO 添加回复
public int addStudyReply(StudyReplyEntity reply);
// TODO 根据用户ID查询用户的所有考研发帖
public List<StudyEntity> personStudy(int uid);
// TODO 用户删除个人某个学习帖子
public void deleteStudy(int fid);
// TODO 管理员根据学习帖子id删除所有回复
public void deleteStudyReply(int sid);
// TODO 用户删除某个考研回帖
public void deleteStudyReplyByRid(int rid);
// TODO 用户管理模块
// TODO 删除用户所有考研发帖
public void dUserAllStudyByUid(int uid);
// TODO 删除该用户所有考研回帖
public void dUserAllStudyReplyByUid(int uid);
// TODO 删除用户帖子下的所有考研回帖
public void dUserOtherStudyRelyBySid(int uid);
}
接口实现方法
public class StudyDaoImpl implements StudyDaoInf {
/**
* TODO 查询考研帖子列表
* @return
*/
public List<StudyEntity> findStudyList() {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<StudyEntity> list = new ArrayList<StudyEntity>();
String sql = "select * from study_info order by create_time desc";
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
StudyEntity study = new StudyEntity();
study.setSid(rs.getInt(1));
study.setTitle(rs.getString(2));
study.setContent(rs.getString(3));
study.setCover(rs.getString(4));
study.setCreateTime(rs.getTimestamp(5));
// TODO 获取对应用户表中的外键
UserEntity author = new UserEntity();
author.setUser_id(rs.getInt(6));
study.setAuthor(author);
list.add(study);
System.out.println("查询所有考研帖子:" + rs.getString(3));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, ps, conn);
}
return list;
}
/**
* TODO 添加考研帖子
* @param study
* @return
*/
public int addStudy(StudyEntity study) {
Connection conn = null;
PreparedStatement ps = null;
String sql = "insert into study_info(title,content,cover,create_time,user_id) values(?,?,?,?,?)";
System.out.println("添加考研帖子");
int i = 0;
System.out.println(study.getCreateTime());
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setString(1, study.getTitle());
ps.setString(2, study.getContent());
ps.setString(3, study.getCover());
ps.setTimestamp(4, study.getCreateTime());
ps.setInt(5, study.getAuthor().getUser_id());
i = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(null, ps, conn);
}
return i;
}
/**
* TODO 根据ID查询某个考研帖子
* @param sid
* @return
*/
public StudyEntity findStudyById(int sid) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
StudyEntity study = null;
String sql = "select * from study_info where sid = ?";
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, sid);
rs = ps.executeQuery();
if(rs.next()){
study = new StudyEntity();
study.setSid(rs.getInt(1));
study.setTitle(rs.getString(2));
study.setContent(rs.getString(3));
study.setCover(rs.getString(4));
study.setCreateTime(rs.getTimestamp(5));
// TODO 获取对应用户表中的外键
UserEntity author = new UserEntity();
author.setUser_id(rs.getInt(6));
study.setAuthor(author);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, ps, conn);
}
return study;
}
/**
* TODO 查询考研帖子对应的所有回复
* @param sid
* @return
*/
public List<StudyReplyEntity> findStudyReplyList(int sid) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<StudyReplyEntity> list = new ArrayList<StudyReplyEntity>();
// TODO 根据外键作为条件查询当前帖子的所有回复
String sql = "select * from study_reply_info where sid = ?";
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, sid);
rs = ps.executeQuery();
while(rs.next()){
StudyReplyEntity reply = new StudyReplyEntity();
reply.setRid(rs.getInt(1));
reply.setContent(rs.getString(2));
reply.setStudyReplyTime(rs.getTimestamp(3));
// TODO 获取对应用户表中的外键
UserEntity replyUser = new UserEntity();
replyUser.setUser_id(rs.getInt(4));
reply.setUser(replyUser);
list.add(reply);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, ps, conn);
}
return list;
}
/**
* TODO 添加考研回复
* @param reply
* @return
*/
public int addStudyReply(StudyReplyEntity reply) {
Connection conn = null;
PreparedStatement ps = null;
int i = 0;
System.out.println("添加回复成功");
String sql = "insert into study_reply_info(reply_content, reply_time, user_id, sid) values(?,?,?,?)";
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setString(1, reply.getContent());
ps.setTimestamp(2, reply.getStudyReplyTime());
// TODO 将相应的两个外键设值
ps.setInt(3, reply.getUser().getUser_id());
ps.setInt(4, reply.getStudy().getSid());
i = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(null, ps, conn);
}
return i;
}
/**
* TODO 根据用户ID查询用户的所有考研发帖
* @param pid
* @return
*/
public List<StudyEntity> personStudy(int pid){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from study_info where user_id=?";
List<StudyEntity> list = new ArrayList<StudyEntity>();
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, pid);
rs = ps.executeQuery();
while (rs.next()) {
StudyEntity study = new StudyEntity();
study.setSid(rs.getInt(1));
study.setTitle(rs.getString(2));
study.setContent(rs.getString(3));
study.setCover(rs.getString(4));
study.setCreateTime(rs.getTimestamp(5));
UserEntity user = new UserEntity();
user.setUser_id(rs.getInt(6));
study.setAuthor(user);
list.add(study);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
/**
* TODO 用户删除个人某个学习帖子
* @param sid
*/
public void deleteStudy(int sid){
Connection conn = null;
PreparedStatement ps = null;
String sql = "delete from study_info where sid =?";
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, sid);
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(null, ps, conn);
}
}
/**
* TODO 管理员根据学习帖子id删除所有回复
* @param sid
*/
public void deleteStudyReply(int sid){
System.out.println("sid: " + sid);
Connection conn = null;
PreparedStatement ps = null;
String sql = "delete from study_reply_info where sid = ?";
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, sid);
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(null, ps, conn);
}
}
/**
* TODO 用户删除某个考研学习回帖
* @param rid
*/
public void deleteStudyReplyByRid(int rid){
Connection conn = null;
PreparedStatement ps = null;
String sql = "delete from study_reply_info where reply_id = ?";
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, rid);
ps.executeUpdate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.close(null, ps, conn);
}
}
// TODO 2. 用户管理模块
/**
* TODO 删除用户所有考研发帖
* @param uid
*/
public void dUserAllStudyByUid(int uid){
Connection conn = null;
PreparedStatement ps = null;
String sql = "delete from study_info where user_id=?";
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, uid);
ps.executeUpdate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.close(null, ps, conn);
}
}
/**
* TODO 删除用户的所有考研回帖
* @param uid
*/
public void dUserAllStudyReplyByUid(int uid){
Connection conn = null;
PreparedStatement ps = null;
String sql = "delete from study_reply_info where user_id=?";
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, uid);
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(null, ps, conn);
}
}
/**
* TODO 删除用户帖子下的所有考研回帖
* @param uid
*/
public void dUserOtherStudyRelyBySid(int uid){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select sid from study_info where user_id=?";
List<StudyEntity> sidlist = new ArrayList<StudyEntity>();
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, uid);
rs = ps.executeQuery();
while (rs.next()) {
StudyEntity forum=new StudyEntity();
forum.setSid(rs.getInt(1));
sidlist.add(forum);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(null, ps, conn);
}
String ssql = "delete from study_reply_info where sid=?";
try {
for (StudyEntity forum : sidlist) {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(ssql);
ps.setInt(1, forum.getSid());
ps.executeUpdate();
DBUtil.close(null, ps, conn);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
- service层
实现servlet层调用的方法,对数据库进行增删改查操作。
- Servlet层
在该层,通过jsp页面传递的参数,调用相应的方法进行操作。
3、考研学习模块设计
考研学习模块分为浏览、查看、发表、删除、回复帖子的功能。
3.1 浏览和查看帖子
考研学习模块的帖子列表如下图所示:
单击帖子标题,即可查看该帖子的详细内容,同时,如果查看的帖子的发表者是当前登录用户或者当前登录用户是管理员,则均具有删除本帖的权限,否则,不会显示「删除本帖」按钮。三种情况分别如下方3张图所示:
与普通帖子一样,学习专帖也可以通过单击用户名即可查看发表者的信息,如图3-22所示。
3.2 发表帖子
发表考研学习帖子:
点击学习专区的「发表学习专帖」按钮进入发表页面,此处不同于普通帖子的是,学习专区的帖子新增了帖子封面壁纸
的选择,完成帖子信息的填写之后,点击“发表”按钮即可发表成功。如下图所示:
发表的学习专帖可以通过考研学习专区帖子列表来查看。
3.3 删除帖子
通过studyReply.jsp页面的“删除本帖”按钮,页面设计和逻辑实现和普通帖子的删除也是大同小异的。
若查看的学习专帖的发表者为当前登录用户或者当前登录用户为管理员身份,均拥有删除此条帖子的功能。如下图所示:
3.4 回复帖子
在studyReply.jsp页面底部设置有回复框,页面设计和逻辑实现和普通帖子的回复也是大同小异的。
与普通帖子的回复如出一辙,学习专帖的回复如下图所示:
回帖前:
回帖后:
3.5 删除回帖
在studyReply.jsp页面,主帖下的每一条回帖都设置有“删除回帖”按钮,页面设计和逻辑实现和普通帖子的删除回帖也是大同小异的。
如下图所示,若查看的学习专帖的发表者为当前登录用户,则当前用户拥有删除此条帖子下所有回帖的权限。
5、项目代码
本文来自博客园,作者:{WHYBIGDATA},转载请注明原文链接:https://www.cnblogs.com/shadowlim/p/17051742.html