标签:table 创建 显示 数据 .post any 唯一性 context 查询
实体有几个,有什么关系,属性:主键、关联关系属性、一般属性、特殊属性(解决某个问题而设计【排序】),最后在看一遍设计的属性能满足这个页面和那个页面吗
主题数和文章数在Forum实体中新增topicCount和articleCount两个属性发帖删帖对应加1减一,把它当做普通的属性显示就没有效率性能的问题了,因为查询的操作远远多于发删帖,查询的数据很多
最后发表的主题也在Forum中设计lastTopic属性,只要发新主题了把这个属性改一下
主题的标题、内容时间、作者。发表时间、主题类型、从父类继承来了
回复数量、不用关联对象的集合去查,存一个普通的字段replyCount int型,没发表一次回复加1
最后回复lastReply的人和时间
主题列表的顺序?新的在前面
FROM
Topic t
ORDER BY
if(t.lastReply == null){
t.postTime
}else{
t.lastReply.postTime
}
DESC
===========================================
在Topic中新增字段lastUpdateTime Date类型
FROM
Topic t
ORDER BY
t.lastUpdateTime DESC
回复标题、回复内容时间、作者、楼层
回复楼层?
因为在发帖时经常会有“对某楼说”的情况,所以回帖的楼层不能改变,否则就会出现一些问题。
方式一:
删除策略:直接删除回复,显示时不显示已删除的回复。
楼层显示策略:需要记住楼层,可以在Reply实体中增加一个floor属性。
方式二:
删除策略:不直接删除回复,而是做一个已删除的标记,显示时还有此楼,不过把内容显示为“此帖已被管理员删除”。
楼层显示策略:在显示回复列表时实时的计算出楼层。方式为:
不分页:${status.count}楼
分页 :${(currentPage - 1) * pageSize + status.count} 楼
===========================================
映射时注意:
1,content应是text类型。
2,有一对一的关联关系。(基于主键【id引用id】、基于外键【唯一性约束】)
3,单向关联。
4,继承。
Article.java
/** * 文章 * @author yejin */ public class Article { private Long id; private String title;// 标题 private String content;// 内容 private Date postTime;// 发表时间 private User author;// 作者 private String ipAddr;// 发表文章时所用的IP地址 public Long getId() { return id; } public void setId(Long id) { this.id = id; } 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 Date getPostTime() { return postTime; } public void setPostTime(Date postTime) { this.postTime = postTime; } public User getAuthor() { return author; } public void setAuthor(User author) { this.author = author; } public String getIpAddr() { return ipAddr; } public void setIpAddr(String ipAddr) { this.ipAddr = ipAddr; } }
Forum.java
/** * 版块 * @author yejin */ public class Forum { private Long id; private String name; private String Description; private int position; //排序用的位置号 private Set<Topic> topics = new HashSet<Topic>(); private int topicCount; //主题数量 private int articleCount; //文章数量(主题+回复) private Topic lastTopic; //最后发表的主题 public Set<Topic> getTopics() { return topics; } public void setTopics(Set<Topic> topics) { this.topics = topics; } public int getTopicCount() { return topicCount; } public void setTopicCount(int topicCount) { this.topicCount = topicCount; } public int getArticleCount() { return articleCount; } public void setArticleCount(int articleCount) { this.articleCount = articleCount; } public Topic getLastTopic() { return lastTopic; } public void setLastTopic(Topic lastTopic) { this.lastTopic = lastTopic; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return Description; } public void setDescription(String description) { Description = description; } public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } }
Reply.java
/** * 回复 * @author yejin */ public class Reply extends Article{ private Topic topic;// 所属的主题 public Topic getTopic() { return topic; } public void setTopic(Topic topic) { this.topic = topic; } }
Topic.java
/** * 主题 * @author yejin */ public class Topic extends Article{ private Forum forum;// 所属版块 private Set<Reply> replies = new HashSet<Reply>(); private int type;// 类型 private int replyCount;// 回复数量 private Reply lastReply;// 最后回复 private Date lastUpdateTime;// 最后更新时间(主题发表时间或最后回复的时间) public Forum getForum() { return forum; } public void setForum(Forum forum) { this.forum = forum; } public Set<Reply> getReplies() { return replies; } public void setReplies(Set<Reply> replies) { this.replies = replies; } public int getType() { return type; } public void setType(int type) { this.type = type; } public int getReplyCount() { return replyCount; } public void setReplyCount(int replyCount) { this.replyCount = replyCount; } public Reply getLastReply() { return lastReply; } public void setLastReply(Reply lastReply) { this.lastReply = lastReply; } public Date getLastUpdateTime() { return lastUpdateTime; } public void setLastUpdateTime(Date lastUpdateTime) { this.lastUpdateTime = lastUpdateTime; } }
映射实体
Forum.hbm.xml
<hibernate-mapping package="cn.itcast.oa.domain"> <class name="Forum" table="itcast_forum"> <id name="id"> <generator class="native" /> </id> <property name="name"></property> <property name="description"></property> <property name="position"></property> <property name="topicCount"></property> <property name="articleCount"></property> <!-- topics属性,本类与Topic的 一对多--> <set name="topics" > <key column="forumId"></key> <one-to-many class="Topic"/> </set> <!-- lastTopic属性,本类与Topic的一对一,采用基于外键的一对一映射方式,本方有外键 --> <many-to-one name="lastTopic" class="Topic" column="lastTopicId" unique="true"></many-to-one> </class> </hibernate-mapping>
Topic.hbm.xml
<hibernate-mapping package="cn.itcast.oa.domain"> <class name="Topic" table="itcast_topic"> <id name="id"> <generator class="native" /> </id> <property name="title"></property> <property name="content" type="text" length="65536"></property> <property name="postTime" type="timestamp"></property> <property name="ipAddr"></property> <property name="type"></property> <property name="replyCount"></property> <property name="lastUpdateTime" type="timestamp"></property> <!-- author属性,本类与User的多对一 --> <many-to-one name="author" class="User" column="authorId"></many-to-one> <!-- forum属性 ,本类与Forum的多对一--> <many-to-one name="forum" class="Forum" column="forumId"></many-to-one> <!-- replies属性,本类与Reply一对多 --> <set name="replies" > <key column="topicId"></key> <one-to-many class="Reply"/> </set> <!-- lastReply属性,本类与Reply一对一 采用基于外键的一对一映射,本方有外键 --> <many-to-one name="lastReply" class="Reply" column="lastReplyId" unique="true"></many-to-one> </class> </hibernate-mapping>
Reply.hbm.xml
<hibernate-mapping package="cn.itcast.oa.domain"> <class name="Reply" table="itcast_reply"> <id name="id"> <generator class="native" /> </id> <property name="title"></property> <property name="content" type="text" length="65536"></property> <property name="postTime" type="timestamp"></property> <property name="ipAddr"></property> <!-- author属性,本类与User的多对一 --> <many-to-one name="author" class="User" column="authorId"></many-to-one> <!-- topic属性,本类与Topic的多对一 --> <many-to-one name="topic" class="Topic" column="topicId"></many-to-one> </class> </hibernate-mapping>
把映射文件加到hibernate.cfg.xml
运行测试类testSessionFactory()方法,创建表,查看表结构外键是否正确
public class SpringTest { //得到容器对象,创建容器对象 private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); @Test public void testBean() throws Exception { TestAction testAction = (TestAction) ac.getBean("testAction"); System.out.println(testAction); } //测试SessionFactory @Test public void testSessionFactory() throws Exception { SessionFactory sessionFactory = (SessionFactory)ac.getBean("sessionFactory"); System.out.println(sessionFactory); } //测试事务 @Test public void testTransaction() throws Exception { TestService testService = (TestService) ac.getBean("testService"); testService.saveTwoUser(); } }
标签:table 创建 显示 数据 .post any 唯一性 context 查询
原文地址:http://www.cnblogs.com/justdoitba/p/7821025.html