⭐基于bootstap-Jquery-JSP-Servlet-mysql⭐博客项目——后台论坛管理demo
Posted stormzhuo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了⭐基于bootstap-Jquery-JSP-Servlet-mysql⭐博客项目——后台论坛管理demo相关的知识,希望对你有一定的参考价值。
文章目录
这一篇主要实现后台论坛管理的实现,代码逻辑跟之前用户管理,资源管理是一样
表的设计
同样的,论坛管理需要两个表,一个是管理用户帖子的表post,另一个是管理帖子评论的表comment
显然,管理用户帖子的表需要一个外键,post_uid对应用户账号user_id
这样当删除用户时,用户所拥有的帖子也会被删除
而管理帖子评论的表,需要两个外键,其中一个外键comment_pid对应用户的帖子post_id,另一个comment_uid对应用户账号user_id
这样当删除用户时,用户所拥有的评论也会被删除,同理,当删除帖子时时,属于这种帖子的评论也会被删除
管理用户帖子表post如下
字段名称 | 类型 | 约束 | 描述 |
---|---|---|---|
post_id | int(10) | NOT NULL AUTO_INCREMENT | 帖子编号 |
post_uid | varchar(20) | NOT NULL FOREIGN KEY (post_uid ) REFERENCES user (user_id ) ON DELETE CASCADE ON UPDATE CASCADE | 外键对应用户账号 |
post_title | varchar(100) | NOT NULL | 帖子标题 |
post_text | longtext | NOT NULL | 帖子内容 |
post_image | varchar(500) | DEFAULT NULL | 图片路径 |
post_time | datetime | NOT NULL | 帖子发布时间 |
帖子评论表comment如下
字段名称 | 类型 | 约束 | 描述 |
---|---|---|---|
comment_id | int(10) | NOT NULL AUTO_INCREMENT | 评论编号 |
comment_pid | int(10) | NOT NULL FOREIGN KEY (comment_pid ) REFERENCES post (post_id ) ON DELETE CASCADE ON UPDATE CASCADE | 外键对应帖子编号 |
comment_uid | varchar(20) | NOT NUL FOREIGN KEY (comment_uid ) REFERENCES user (user_id ) ON DELETE CASCADE ON UPDATE CASCADE | 外键对应用户账号 |
comment_text | varchar(250) | NOT NULL | 评论内容 |
comment_time | datetime | NOT NUL | 评论时间 |
表的创建
post表的创建
在mysql命令行使用如下sql语句创建post表
DROP TABLE IF EXISTS `post`;
CREATE TABLE `post` (
`post_id` int(10) NOT NULL AUTO_INCREMENT,
`post_uid` varchar(20) NOT NULL,
`post_title` varchar(100) NOT NULL,
`post_text` longtext NOT NULL,
`post_image` varchar(500) DEFAULT NULL,
`post_time` datetime NOT NULL,
PRIMARY KEY (`post_id`),
KEY `post_uid` (`post_uid`),
CONSTRAINT `post_ibfk_1` FOREIGN KEY (`post_uid`) REFERENCES `user` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
comment表的创建
在mysql命令行使用如下sql语句创建comment表
DROP TABLE IF EXISTS `comment`;
CREATE TABLE `comment` (
`comment_id` int(10) NOT NULL AUTO_INCREMENT,
`comment_pid` int(10) NOT NULL,
`comment_uid` varchar(20) NOT NULL,
`comment_text` varchar(250) NOT NULL,
`comment_time` datetime NOT NULL,
PRIMARY KEY (`comment_id`),
KEY `comment_uid` (`comment_uid`),
KEY `comment_pid` (`comment_pid`),
CONSTRAINT `comment_ibfk_2` FOREIGN KEY (`comment_uid`) REFERENCES `user` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `comment_ibfk_3` FOREIGN KEY (`comment_pid`) REFERENCES `post` (`post_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
创建实体类
在entity包下,创建帖子实体post,如下
public class Post
private int postId;
private String postUid;
private String postTitle;
private String postText;
private String postImage;
private String postTime;
public Post()
super();
public Post(int postId, String postUid, String postTitle, String postText, String postImage, String postTime)
super();
this.postId = postId;
this.postUid = postUid;
this.postTitle = postTitle;
this.postText = postText;
this.postImage = postImage;
this.postTime = postTime;
public int getPostId()
return postId;
public void setPostId(int postId)
this.postId = postId;
public String getPostUid()
return postUid;
public void setPostUid(String postUid)
this.postUid = postUid;
public String getPostTitle()
return postTitle;
public void setPostTitle(String postTitle)
this.postTitle = postTitle;
public String getPostText()
return postText;
public void setPostText(String postText)
this.postText = postText;
public String getPostImage()
return postImage;
public void setPostImage(String postImage)
this.postImage = postImage;
public String getPostTime()
return postTime;
public void setPostTime(String postTime)
this.postTime = postTime;
@Override
public String toString()
return "Post [postId=" + postId + ", postUid=" + postUid + ", postTitle=" + postTitle + ", postText=" + postText
+ ", postImage=" + postImage + ", postTime=" + postTime + "]";
在entity包下,创建评论实体comment,如下
public class Comment
private int commentId;
private int commentPid;
private String commentUid;
private String commentText;
private String commentTime;
public Comment()
super();
public Comment(int commentId, int commentPid, String commentUid, String commentText, String commentTime)
super();
this.commentId = commentId;
this.commentPid = commentPid;
this.commentUid = commentUid;
this.commentText = commentText;
this.commentTime = commentTime;
public int getCommentId()
return commentId;
public void setCommentId(int commentId)
this.commentId = commentId;
public int getCommentPid()
return commentPid;
public void setCommentPid(int commentPid)
this.commentPid = commentPid;
public String getCommentUid()
return commentUid;
public void setCommentUid(String commentUid)
this.commentUid = commentUid;
public String getCommentText()
return commentText;
public void setCommentText(String commentText)
this.commentText = commentText;
public String getCommentTime()
return commentTime;
public void setCommentTime(String commentTime)
this.commentTime = commentTime;
@Override
public String toString()
return "Comment [commentId=" + commentId + ", commentPid=" + commentPid + ", commentUid=" + commentUid
+ ", commentText=" + commentText + ", commentTime=" + commentTime + "]";
论坛管理实现
用户帖子管理
获取所有用户帖子信息并分页
在controller中创建forum包,并在forum包下创建DoPostSelect.java,代码如下
@WebServlet("/manage/admin_do_post_select")
public class DoPostSelect extends HttpServlet
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
// 当前页
int currentPage = 1;
// 每页显示条数
int count = 5;
// array[0]=帖子总数,array[1]=总页数,后面会被覆盖
int array[] = 0, 0 ;
// 获取用户指定的页面
String cp = request.getParameter("currentPage");
// 接收用户搜索的关键字
String keyword = request.getParameter("keywords");
/* 用户如果有指定当前页,则把字符串转化为int型 */
if (cp != null)
currentPage = Integer.parseInt(cp);
ForumServiceImpl forumServiceImpl = new ForumServiceImpl();// 创建帖子service层接口的实现类
/*
* 调用业务逻辑方法获取帖子记录总数并算出总页数 即(array[0]=帖子总数,array[1]=总页数(帖子总数/每页显示条数)
*/
try
array = forumServiceImpl.getPostPageTotal(count, keyword);
catch (SQLException e)
e.printStackTrace();
ArrayList<Post> listPost = null;
try
listPost = forumServiceImpl.getAllPost(currentPage, count, keyword);
catch (SQLException e)
e.printStackTrace();
/*
* 遍历所有帖子,遍历过程中利用帖子id 调用业务逻辑方法获取帖子用户信息,并添加到list集合中
*/
User user = null;
ArrayList<User> listUser = new ArrayList<User>();
for (int i = 0; i < listPost.size(); i++)
try
user = forumServiceImpl.getPostUser(listPost.get(i).getPostUid());
catch (SQLException e)
e.printStackTrace();
listUser.add(user);
/* 放到请求对象域里 */
request.setAttribute("listPost", listPost);
request.setAttribute("listUser", listUser);
request.setAttribute("totalStudent", array[0]);
request.setAttribute("totalPage", array[1]);
request.setAttribute("currentPage", currentPage);
if (keyword != null)
request.setAttribute("searchParams", "&keywords=" + keyword);
request.getRequestDispatcher("/WEB-INF/manage/admin_post.jsp").forward(request, response);
在service层中创建ForumService接口,并在接口中添加如下业务逻辑抽象方法
// 获取所有帖子记录总数及页数
public int[] getPostPageTotal(int count, String keyword) throws SQLException;
// 获取所有帖子信息
public ArrayList<Post> getAllPost(int currentPage, int count, String keyword) throws SQLException;
// 获取帖子的用户信息
public User getPostUser(String postUid) throws SQLException;
在service层中创建接口的实现类ForumServiceImpl.java,并在实现类中重写接口方法
/* 创建帖子dao层接口的实现类,并赋给接口对象变量
* 实现上转型,也就面向接口编程,而不关心它的实现类是谁 */
ForumDao forumDao = new ForumDaoImpl();
/*
* 创建用户dao层接口的实现类,并赋给接口对象变量。
* 实现上转型,也就面向接口编程,而不关心它的实现类是谁
*/
UserDao userDao = new UserDaoImpl();
@Override
public int[] getPostPageTotal(int count, String keyword) throws SQLException
// 检索所有帖子总数并算出总页数
int[] array = forumDao.selectPostTotal(count, keyword);
return array;
@Override
public ArrayList<Post> getAllPost(int currentPage, int count, String keyword) throws SQLException
// 检索所有帖子信息
ArrayList<Post> listPost = forumDao.selectAllPost(currentPage, count, keyword);
return listPost;
@Override
public User getPostUser(String postUid) throws SQLException
// 检索用户信息
User user = userDao.selectUser(postUid, "");
return user;
这里需要说一下获取帖子用户信息的业务功能,它调用了检索用户信息dao层方法。
在前面的用户管理中已经在dao层的UserDao编写了检索用户的方法,在这里就可以直接用了,而不需要重复在dao层的ForumDao编写检索用户信息的方法了
但是呢,UserDaoImpl是需要一个身份标识的参数的,这里只需传入空值即可
在dao层中创建接口ForumDao.java,并在接口中添加如下抽象方法,执行底层操作
// 检索所有帖子总数并算出总页数
public int[] selectPostTotal(int count, String keyword) throws SQLException;
// 检索所有帖子信息
public ArrayList<Post> selectAllPost(int currentPage, int count, String keyword) throws SQLException;
在dao层中创建接口的实现类ForumDaoImpl.java,并在实现类中重写接口方法
// 存放查询数据库返回的结果集
ResultSet resultSet;
@Override
public int[] selectPostTotal(int count, String keyword) throws SQLException
String sql = "";
// 0 帖子记录数 1 页数
System.out.println("keyword =" + keyword);
int array[] = 0, 1 ;
if (keyword != null)
sql = "select count(*) from post where post_title like ?";
resultSet = JDBCUtil.executeQuery(sql, "%" + keyword + "%");
else
sql = "select count(*) from post";
resultSet = JDBCUtil.executeQuery(sql);
while (resultSet.next())
array[0] = resultSet.getInt(1);
if (array[0] % count == 0)
array[1] = array[0] / count;
else
array[1] = array[0] / count + 1;
return array;
@Override
public ArrayList<Post> selectAllPost(int currentPage, int count, String keyword) throws SQLException
ArrayList<Post> listPost = new ArrayList<Post>();
String sql = "";
if (keyword != null)
sql = "select * from post where post_title like ? order by post_time desc limit ?, ?";
resultSet = JDBCUtil.executeQuery(sql, "%" + keyword + 基于内容与基于协作的过滤?
推荐算法简介:基于用户的协同过滤基于物品的协同过滤基于内容的推荐