比較簡單的方式就是依據模仿同類產品,依據同類產品的進行模仿,表單就是一個起碼要加的欄位,然後依據專案須要額外新增欄位。

注意:實體類之間的引用關係還須要考慮效能的影響。如:單向或是雙向。

表設計:

設計好後:

寫實體類

建立實體類到資料庫的關聯關係

概述

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>

User.java

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>