评论肯定是基于文章的,文章肯定有唯一的ID。
这样的话,可以让评论全部关联到这个ID上。
随便写写,代码不能运行,参考即可。
public class Comment {
Long id;
Article article;//文章
User user;//用户
Date date;//时间
String content;//内容
}
public class CommentService {
void addComment(Article article, User user, String content);
void updateComment(Comment comment);
void deleteComment(Comment commnet);
ListfindCommentByArticle(Article article);
ListfindCommentByUser(User user);
}
public class AddCommentAction {
private Long articleId;
private Long userId;
private String content;
public String execute() {
Article article = articleService.getArticle(articleId);
User user = userService.getUser(userId);
commentService.addComment(article, user, comment);
return SUCCESS;
}
}
action要继承一个Sturts2中的actionSuport类,要是Struts1就要继承一个httpServlet,DAO就是访问数据库的操作的一些接口,你可能继承一个HibernateDao这个类,里面增、删、改、查基本上都定好了,直接Super.方法名(),就可以,Service层关系不大,要不要这一层没多大关系,要的话也最多别一些action接收的一些数据,和向页面发送数据时的一些数据的验证。
dao 层主要是操作数据库层面的
sevice 这一层主要是你实现的业务逻辑,逻辑中部分会操作数据库,这样service会用到 dao 调用数据库的方法
action 层理主要控制页面的跳转 不会有太多逻辑,里面会有多个service 的方法组成一个完整的逻辑。
hiberate负责数据库持久化,也就是操作数据库。
struts2负责action等,
spring负责事物托管。
Dao负责操作数据库,最好一个表对应一个dao。
把对应的需求封装成service,当操作数据库时,service调用dao层。
action负责页面跳转等,比如submit a comment,然后action层去调用service层。