比较简单的方式就是根据模仿同类产品,根据同类产品的进行模仿,表单就是一个起码要加的字段,然后根据项目需要额外增加字段。
注意:实体类之间的引用关系还需要考虑性能的影响。如:单向或是双向。
表设计:
设计好后:
写实体类
建立实体类到数据库的关联关系
概述
5.实体关系分析
1.类结构:带箭头是单线关联,不带箭头是双向关联
----------------------------------------
class User
(1)<------(*) class Survey (1)-------(*) class Page (1)-------(*) class Question
{ { {
{
Integer id ;
Integer id ; Integer id ; Integer id ;
... ... ... ...
User user ; Survey survey ; Page page ;
Set<Page> pages ;
Set<Question> questions ;
} } }
}
2.表结构
------------------------------------------------------------------
[users]
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| email | varchar(50) | YES | | NULL | |
| password | varchar(50) | YES | | NULL | |
| nickname | varchar(50) | YES | | NULL | |
| regdate | datetime | YES | | NULL | |
+----------+-------------+------+-----+---------+----------------+
[surveys]
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(200) | YES | | NULL | |
| pretext | varchar(50) | YES | | NULL | |
| nexttext | varchar(50) | YES | | NULL | |
| exittext | varchar(50) | YES | | NULL | |
| donetext | varchar(50) | YES | | NULL | |
| createtime | datetime | YES | | NULL | |
| userid | int(11) | YES | MUL | NULL | |
+---------------+--------------+------+-----+---------+----------------+
[pages]
+-------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(200) | YES | | NULL | |
| description | varchar(200) | YES | | NULL | |
| surveyid | int(11) | YES | MUL | NULL | |
+-------------+---------------+------+-----+---------+----------------+
[questions]
+---------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| questiontype | int(11) | YES | | NULL | |
| title | varchar(200) | YES | | NULL | |
| options | varchar(200) | YES | | NULL | |
| other | bit(1) | YES | | NULL | |
| otherstyle | int(11) | YES | | NULL | |
| otherselectoptions | varchar(200) | YES | | NULL | |
| matrixrowtitles | varchar(200) | YES | | NULL | |
| matrixcoltitles | varchar(200) | YES | | NULL | |
| matrixselectoptions | varchar(200) | YES | | NULL | |
| pageid | int(11) | YES | MUL | NULL | |
+---------------------+--------------+------+-----+---------+----------------+
3.映射文件
------------------------------------------
[User.hbm.xml]
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.atguigu.surveypark.model.User" table="users">
<id name="id" column="id" type="integer">
<generator class="identity" />
</id>
<property name="email" column="email" type="string" length="50" />
<property name="password" column="password" type="string" length="50" />
<property name="nickName" column="nickname" type="string" length="50" />
<property name="regDate" column="regdate" type="timestamp" update="false"/>
</class>
</hibernate-mapping>
[Survey.hbm.xml]
<hibernate-mapping package="com.atguigu.surveypark.model">
<class name="Survey" table="surveys">
<id name="id" column="id" type="integer">
<generator class="identity" />
</id>
<property name="title" column="title" type="string" length="200" />
<property name="preText" column="pretext" type="string" length="50" />
<property name="nextText" column="nexttext" type="string" length="50" />
<property name="doneText" column="donetext" type="string" length="50" />
<property name="exitText" column="exittext" type="string" length="50" />
<property name="createTime" column="createtime" type="string" length="200" />
<!-- 映射从Survey到User之间多对一关联关系 -->
<many-to-one name="user" class="User" column="userid" />
<!-- 映射从Survey到Page之间一对多关联关系 -->
<set name="pages" inverse="true">
<key column="surveyid" />
<one-to-many class="Page"/>
</set>
</class>
</hibernate-mapping>
[Page.hbm.xml]
<hibernate-mapping package="com.atguigu.surveypark.model">
<class name="Page" table="pages">
<id name="id" column="id" type="integer">
<generator class="identity" />
</id>
<property name="title" column="title" type="string" length="100" />
<property name="description" column="description" type="string" length="200" />
<!-- 映射从Page到Survey之间多对一关联关系 -->
<many-to-one name="survey" class="Survey" column="surveyid" />
<!-- 映射从Page到Question之间一对多关联关系 -->
<set name="questions" inverse="true">
<key column="pageid" />
<one-to-many class="Question"/>
</set>
</class>
</hibernate-mapping>
[Question.hbm.xml]
<hibernate-mapping package="com.atguigu.surveypark.model">
<class name="Question" table="questions">
<id name="id" column="id" type="integer">
<generator class="identity" />
</id>
<property name="questionType" column="questiontype" type="integer" />
<property name="title" column="title" type="string" length="100" />
<property name="options" column="options" type="string" length="200" />
<property name="other" column="other" type="boolean"/>
<property name="otherStyle" column="otherstyle" type="integer" />
<property name="otherSelectOptions" column="otherselectoptions" type="string" length="200" />
<property name="matrixRowTitles" column="maxtrixrowtitles" type="string" length="200" />
<property name="matrixColTitles" column="matrixcoltitles" type="string" length="200" />
<property name="matrixSelectOptions" column="matrixselectoptions" type="string" length="200" />
<!-- 映射从Question到Page之间多对一关联关系 -->
<many-to-one name="page" class="Page" column="pageid" />
</class>
</hibernate-mapping>
详细代码如下:
Page.java
package com.atguigu.surveypark.model;
import java.util.HashSet;
import java.util.Set;
/**
* 页面类
*/
public class Page {
private Integer id;
private String title = "未命名";
private String description;
//简历从Page到Survey之间多对一关联关系
private Survey survey;
//简历从Page到Question之间一对多关联关系
private Set<Question> questions = new HashSet<>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Survey getSurvey() {
return survey;
}
public void setSurvey(Survey survey) {
this.survey = survey;
}
public Set<Question> getQuestions() {
return questions;
}
public void setQuestions(Set<Question> questions) {
this.questions = questions;
}
}
Page.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.atguigu.surveypark.model">
<class name="Page" table="pages">
<id name="id" column="id" type="integer">
<generator class="identity" />
</id>
<property name="title" column="title" type="string" length="100" />
<property name="description" column="description" type="string" length="200" />
<!-- 映射从Page到Survey之间多对一关联关系 -->
<many-to-one name="survey" class="Survey" column="surveyid" />
<!-- 映射从Page到Question之间一对多关联关系 -->
<set name="questions" inverse="true">
<key column="surveyid" />
<one-to-many class="Question"/>
</set>
</class>
</hibernate-mapping>
Survey.java
package com.atguigu.surveypark.model;
import java.util.Date;
/**
* 调查类
*/
public class Survey {
private Integer id;
private String title = "未命名";
private String preText = "上一步";
private String nextText = "下一步";
private String exitText = "退出";
private String doneText = "完成";
private Date createTime = new Date();
//建立从Survey到User之间多对一关联关系
private User user ;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPreText() {
return preText;
}
public void setPreText(String preText) {
this.preText = preText;
}
public String getNextText() {
return nextText;
}
public void setNextText(String nextText) {
this.nextText = nextText;
}
public String getExitText() {
return exitText;
}
public void setExitText(String exitText) {
this.exitText = exitText;
}
public String getDoneText() {
return doneText;
}
public void setDoneText(String doneText) {
this.doneText = doneText;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
Survey.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.atguigu.surveypark.model">
<class name="Survey" table="surveys">
<id name="id" column="id" type="integer">
<generator class="identity" />
</id>
<property name="title" column="title" type="string" length="200" />
<property name="preText" column="pretext" type="string" length="50" />
<property name="nextText" column="nexttext" type="string" length="50" />
<property name="doneText" column="donetext" type="string" length="50" />
<property name="exitText" column="exittext" type="string" length="50" />
<property name="createTime" column="createtime" type="string" length="200" />
<!-- 映射从Survey到User之间多对一关联关系 -->
<many-to-one name="user" class="User" column="userid" />
<!-- 映射从Survey到Page之间一对多关联关系 -->
<set name="pages" inverse="true">
<key column="surveyid" />
<one-to-many class="Page"/>
</set>
</class>
</hibernate-mapping>
Question.java:
package com.atguigu.surveypark.model;
/**
* 问题类
*/
public class Question {
//
private Integer id;
// 题型0-8
private int questionType;
//
private String title;
// 选项
private String options;
// 其他项
private boolean other;
// 其他项样式:0-无 1-文本框 2-下拉列表
private int otherStyle;
// 其他项下拉选项
private String otherSelectOptions;
// 矩阵式行标题集
private String matrixRowTitles;
// 矩阵式列标题集
private String matrixColTitles;
// 矩阵是下拉选项集
private String matrixSelectOptions;
//建立从Question到Page之间多对一关联关系
private Page page;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public int getQuestionType() {
return questionType;
}
public void setQuestionType(int questionType) {
this.questionType = questionType;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getOptions() {
return options;
}
public void setOptions(String options) {
this.options = options;
}
public boolean isOther() {
return other;
}
public void setOther(boolean other) {
this.other = other;
}
public int getOtherStyle() {
return otherStyle;
}
public void setOtherStyle(int otherStyle) {
this.otherStyle = otherStyle;
}
public String getOtherSelectOptions() {
return otherSelectOptions;
}
public void setOtherSelectOptions(String otherSelectOptions) {
this.otherSelectOptions = otherSelectOptions;
}
public String getMatrixRowTitles() {
return matrixRowTitles;
}
public void setMatrixRowTitles(String matrixRowTitles) {
this.matrixRowTitles = matrixRowTitles;
}
public String getMatrixColTitles() {
return matrixColTitles;
}
public void setMatrixColTitles(String matrixColTitles) {
this.matrixColTitles = matrixColTitles;
}
public String getMatrixSelectOptions() {
return matrixSelectOptions;
}
public void setMatrixSelectOptions(String matrixSelectOptions) {
this.matrixSelectOptions = matrixSelectOptions;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
}
Question.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.atguigu.surveypark.model">
<class name="Question" table="questions">
<id name="id" column="id" type="integer">
<generator class="identity" />
</id>
<property name="questionType" column="questiontype" type="integer" />
<property name="title" column="title" type="string" length="100" />
<property name="options" column="options" type="string" length="200" />
<property name="other" column="other" type="boolean"/>
<property name="otherStyle" column="otherstyle" type="integer" />
<property name="otherSelectOptions" column="otherselectoptions" type="string" length="200" />
<property name="matrixRowTitles" column="maxtrixrowtitles" type="string" length="200" />
<property name="matrixColTitles" column="matrixcoltitles" type="string" length="200" />
<property name="matrixSelectOptions" column="matrixselectoptions" type="string" length="200" />
<!-- 映射从Question到Page之间多对一关联关系 -->
<many-to-one name="page" class="Page" column="pageid" />
</class>
</hibernate-mapping>
package com.atguigu.surveypark.model;
import java.util.Date;
/**
* 用户类
*/
public class User {
private Integer id;
private String email;
private String name;
private String password;
private String nickName;
//注册时间
private Date regDate = new Date();
public Integer getId() {
return id;
}
public Date getRegDate() {
return regDate;
}
public void setRegDate(Date regDate) {
this.regDate = regDate;
}
public void setId(Integer id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
}
User.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.atguigu.surveypark.model.User" table="users">
<id name="id" column="id" type="integer">
<generator class="identity" />
</id>
<property name="email" column="email" type="string" length="50" />
<property name="password" column="password" type="string" length="50" />
<property name="nickName" column="nickname" type="string" length="50" />
<property name="regDate" column="regdate" type="timestamp" update="false"/>
</class>
</hibernate-mapping>