新闻发布项目——数据实现类(commentDaoImpl)
发布日期:2021-05-14 13:16:24 浏览次数:31 分类:精选文章

本文共 2761 字,大约阅读时间需要 9 分钟。

package bdqn.newsMange.Dao.Impl;

import java.sql.ResultSet; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import bdqn.newsMange.Dao.BaseDao; import bdqn.newsMange.Dao.commentDao; import bdqn.newsMange.entity.Comment;

/**

  • @class commentDaoImpl

  • @description 新闻评论的数据访问接口实现类

  • @author tech

  • @since 1.0 */ public class CommentDaoImpl extends BaseDao implements CommentDao {

    /**

    • 获取所有评论
    • @return所有评论的列表 */ public List
      getComments() { List
      comments = new ArrayList<>(); String sql = "SELECT * FROM comment"; try { ResultSet rs = executeQuery(sql, null); while (rs.next()) { .Comment comment = new Comment(); comment.setCommentId(rs.getInt("comment_id")); comment.setCmmUser(rs.getString("cmm_user")); comment.setIp(rs.getString("ip")); comment.setCmmContent(rs.getString("cmm_content")); comment.setNewsId(rs.getInt("news_id")); comment.setCmmDate(rs.getDate("cmm_date")); .comments.add(comment); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { closeAll(); } return comments; }

    /**

    • 根据新闻ID获取该新闻的所有评论
    • @param newsId 新闻ID
    • @return该新闻所有评论的列表 */ public List
      getCommentsByNewsId(int newsId) { List
      comments = new ArrayList<>(); String sql = "SELECT * FROM comment WHERE comment_id = ?"; Object[] params = new Object[]{newsId}; try { ResultSet rs = executeQuery(sql, params); while (rs.next()) { Comment comment = new Comment(); comment.setCommentId(rs.getInt("comment_id")); comment.setCmmUser(rs.getString("cmm_user")); comment.setIp(rs.getString("ip")); comment.setCmmContent(rs.getString("cmm_content")); comment.setNewsId(rs.getInt("news_id")); comment.setCmmDate(rs.getDate("cmm_date")); comments.add(comment); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { closeAll(); } return comments; }

    /**

    • 添加评论
    • @param comment 要添加的评论实体
    • @return 1(成功) | 0(失败) */ public int addComment(Comment comment) { int result = 0; String sql = "INSERT INTO comment (cmm_user, ip, cmm_content, news_id, cmm_date) VALUES (?, ?, ?, ?, ?)"; Object[] params = new Object[]{ comment.getCmmUser(), comment.getIp(), comment.getCmmContent(), comment.getNewsId(), new SimpleDateFormat("yyyy-MM-dd").format(new Date()) }; try { result = executeUpdate(sql, params); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { closeAll(); } return result; }

    /**

    • 更新评论信息
    • @param comment 需要更新的评论实体
    • @return 1(成功) | 0(失败) */ public int updateComment(Comment comment) { int result = 0; // 过于简略,实际业务需要根据具体需求实现 return result; }

    /**

    • 删除评论
    • @param id 要删除的评论ID
    • @return 1(成功) | 0(失败) */ public int deleteComment(int id) { int result = 0; return result; } }
上一篇:新闻发布项目——数据实现类(categoryTBDaoImpl)
下一篇:新闻发布项目——数据实现类(newsTbDaoImpl)

发表评论

最新留言

感谢大佬
[***.8.128.20]2025年04月05日 19时29分52秒