作者:bojiangzhou;
來源:https://www.cnblogs.com/chiangchou/p/project-gpss.html


Java開發進銷存管理系統(一)


四、底層架構設計

在進行進銷存系統的設計和編碼之前,首先設計一個自己的底層框架,這個底層框架在之後可以作爲其它具體項目開發的一個基礎,從而不必每次開發項目時,都去做很多重複的工作。這個底層框架主要包括一個開發的規範,以及一些通用的工具類等,更重要的是分類別引入各個框架,如Spring、Hibernate、各個配置文件等。同時,如果以後在開發中,增加的一些新功能,還可以往這個底層中添加,不斷的去完善。

1. 規範

在進行框架設計之前,爲了使軟件開發過程順暢、提高代碼的可靠性,可讀性和可維護性等,首先需要確定的就是開發規範了,俗話說,沒有規矩不成方圓,軟件開發亦是如此。下面列出一些簡單的需要遵守的規範。

1.1 基礎規範

首先需要遵守的是一些基礎規範。一般來說,公司會將域名作爲所有命名的一個基礎,比如文件名、包名等等。因此我申請了一個域名[www.lyyzoo.com]作爲個人域名。然後將D:/lyyzoo-repo作爲開發的根目錄,即個人代碼倉庫,以後所有的項目都會建到這個目錄下。所有的項目開發使用maven來管理項目,因此目錄結構是標準的maven規範目錄。

maven約定的目錄結構:

Java開發進銷存管理系統(二)


1.2 代碼規範

① 命名

  • 所有的命名需要見名之意,儘量保證通過變量名得知變量的含義,需要註釋的地方儘量添加註釋。
  • 包命名全小寫,通過域名倒寫+模塊的形式,如:com.lyyzoo.service
  • 類命名採用Pascal名法,大寫字母開頭,每個單詞首字母大寫。
  • 方法名採用Camel命名法,小寫字母開頭,每個單詞首字母小寫;getter和setter使用Lombok自動生成,只需添加@Data註解即可。
  • 變量名採用Camel命名法,小寫字母開頭,每個單詞首字母大寫。變量名不宜過長,可採用首字母縮寫的形式,但要見名之意。
  • 常量名全大寫,每個單詞之間使用”_”分隔。


② 分層

項目以功能模塊劃分,不同項目建立不同的工程,使用maven的依賴進行管理。包的基本分層有controller(控制層)、service(業務層)、dao(數據訪問層)、entity(模型層)。

2. 架構設計

2.1 模塊結構

整個項目的底層着重是一些通用的、基礎的東西,整合到一起,以便於以後重用。首先,創建一個名爲lyyzoo的maven工程,lyyzoo將作爲底層的根目錄。lyyzoo下有兩個主要的子模塊,分別爲lyyzoo-base和lyyzoo-starter,lyyzoo-base是基礎模塊,用於一些簡單的Java及JavaEE程序;lyyzoo-starter則是JavaEE相關,會依賴於lyyzoo-base,同時引入了Spring、Hibernate等第三方框架。然後在各個模塊中添加具體的子模塊。以後開發中需要用到哪個模塊,在依賴中添加那個模塊即可。

底層模塊結構圖:

Java開發進銷存管理系統(二)


以下是各個POM之間的關係:

① lyyzoo > pom.xml

Java開發進銷存管理系統(二)


② lyyzoo-base > pom.xml

Java開發進銷存管理系統(二)


③ lyyzoo-starter > pom.xml

Java開發進銷存管理系統(二)


2.2 依賴管理

結構建好後,就需要進行一些詳細的依賴配置工作了,lyyzoo是所有模塊的父類,所以在lyyzoo中需要添加公用的屬性、依賴管理、maven插件等。

首先將所有的版本號提出來,放到裏,這樣一旦需要切換到另一個版本時,就可以只改個版本號就達到目的了。其中列出了一些屬性如下:包括底層的版本、Java版本、Spring、Hibernate的版本等等。

1 
2
3 1.0-SNAPSHOT
4 1.8
5 ${java.version}
6 ${java.version}
7 UTF-8
8
9 1.16.14
10
11 1.7.7
12 1.1.3
13
14 5.1.38
15
16 0.9.5.2
17
18 4.12
19
20 3.1.0
21
22 4.2.6.RELEASE
23 1.7.3
24 1.7.3
25
26 5.0.1.Final
27 1.0.0.Final
28


接着,引入依賴管理,在lyyzoo中引入其它項目將會用到的所有三方jar包的依賴,所有的依賴都添加到中,這樣就可以方便的管理所有的jar包了。下面列出引入的一部分jar包,其它的可參考源碼。

① 首先需要引入lyyzoo下的其它模塊,如lyyzoo-base-core、lyyzoo-starter-base等模塊。

1 
2
3 com.lyyzoo
4 lyyzoo-base-core
5 ${lyyzoo.version}
6

7
8 com.lyyzoo
9 lyyzoo-base-data
10 ${lyyzoo.version}
11

12
13 com.lyyzoo
14 lyyzoo-starter-base
15 ${lyyzoo.version}
16

17
18 com.lyyzoo
19 lyyzoo-starter-jpa
20 ${lyyzoo.version}
21


② JDBC相關,相關jar包有c3p0,用於作數據庫連接池;mysql驅動包;dbutils,對JDBC進行了簡單的封裝,使用起來簡單方便。

1 
2
3 com.mchange
4 c3p0
5 ${c3p0.version}
6

7
8 mysql
9 mysql-connector-java
10 ${mysql.version}
11

12
13 commons-dbutils
14 commons-dbutils
15 1.5
16


③ 日誌相關:

1 
2
3 org.slf4j
4 slf4j-api
5 ${slf4j.version}
6

7
8 ch.qos.logback
9 logback-classic
10 ${logback.version}
11

12
13
14 org.slf4j
15 log4j-over-slf4j
16 ${slf4j.version}
17


④ spring相關,包括了spring aop、spring mvc等。

1 
2
3 org.springframework
4 spring-aop
5 ${spring.version}
6

7
8 org.springframework
9 spring-aspects
10 ${spring.version}
11

12
13 org.springframework
14 spring-beans
15 ${spring.version}
16

17
18 org.springframework
19 spring-context
20 ${spring.version}
21

22
23 org.springframework
24 spring-context-support
25 ${spring.version}
26

27
28 org.springframework
29 spring-core
30 ${spring.version}
31

32
33 org.springframework
34 spring-jdbc
35 ${spring.version}
36

37
38 org.springframework
39 spring-jms
40 ${spring.version}
41

42
43 org.springframework
44 spring-orm
45 ${spring.version}
46

47
48 org.springframework
49 spring-tx
50 ${spring.version}
51

52
53 org.springframework
54 spring-web
55 ${spring.version}
56

57
58 org.springframework
59 spring-webmvc
60 ${spring.version}
61


⑤ hibernate相關:

1 
2 org.hibernate
3 hibernate-c3p0
4 ${hibernate.version}
5

6
7 org.hibernate.common
8 hibernate-commons-annotations
9 ${hibernate.version}
10

11
12 org.hibernate
13 hibernate-core
14 ${hibernate.version}
15

16
17 org.hibernate
18 hibernate-entitymanager
19 ${hibernate.version}
20


所有的依賴添加好後,就需要爲各個子模塊添加具體的依賴了,根據每個子模塊的功能,添加相關的依賴,而不是將所有的依賴一次性加入。這樣我們就可以根據自己開發的項目的需要,添加模塊依賴,而不是一次性加入所有jar包,避免冗餘,增大項目的體積。下面以lyyzoo-base-data和lyyzoo-starter-jpa爲例說明。

lyyzoo-base-data模塊是基礎數據相關,主要與數據庫打交道,那麼就需要引入mysql驅動、數據庫連接池c3p0等,pom.xml如下:

1 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3 4.0.0
4
5 com.lyyzoo
6 lyyzoo-base
7 1.0-SNAPSHOT
8

9 lyyzoo-base-data
10 ${project.artifactId}
11 jar
12
13
14
15
16 commons-dbutils
17 commons-dbutils
18

19
20
21 mysql
22 mysql-connector-java
23

24
25 com.mchange
26 c3p0
27

28
29
30 com.lyyzoo
31 lyyzoo-base-test
32 test
33

34

35


lyyzoo-starter-jpa是Java持久化相關,所以主要引入hibernate相關的依賴,同時,starter-jpa會依賴base-data及starter-base,pom.xml如下:

1 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3 4.0.0
4
5 com.lyyzoo
6 lyyzoo-starter
7 1.0-SNAPSHOT
8

9 lyyzoo-starter-jpa
10 ${project.artifactId}
11 jar
12
13
14
15 com.lyyzoo
16 lyyzoo-base-data
17

18
19 com.lyyzoo
20 lyyzoo-starter-base
21

22
23 org.hibernate
24 hibernate-c3p0
25

26
27 org.hibernate.common
28 hibernate-commons-annotations
29

30
31 org.hibernate
32 hibernate-core
33

34
35 org.hibernate
36 hibernate-ehcache
37

38
39 org.hibernate.javax.persistence
40 hibernate-jpa-2.1-api
41

42
43 org.hibernate
44 hibernate-validator
45

46
47 com.lyyzoo
48 lyyzoo-starter-test
49 test
50

51

52


2.3 類結構

有了前面的基礎之後,接下來進行重點的類結構設計。底層需要做的最重要的工作就是將一些通用的類抽象出來,以便於以後重用,從而提高開發效率。例如返回結果和分頁類的封裝、通用的工具類、JDBC相關的操作等。

說明一下,整個底層很大一部分是從之前實習的公司(這個就不說了)直接拿過來的,有些則更改了。相信我拿取的這部分代碼並不會涉及機密問題,不用於商業用途,僅僅只是學習,應該沒多大問題。

(1) lyyzoo-base-core

lyyzoo-base-core 目錄結構:

Java開發進銷存管理系統(二)


lyyzoo-base-core是基礎核心,封裝了返回結果和加入了一些常用的工具類。

com.lyyzoo.bean包下封裝了BaseBean和Result,BaseBean是Bean類的一個基礎類,實現了序列化,重寫了toString()方法,如下:

1 package com.lyyzoo.bean;
2
3 import org.apache.commons.lang3.builder.ToStringBuilder;
4 import org.apache.commons.lang3.builder.ToStringStyle;
5
6 import java.io.Serializable;
7
8 /**
9 * BaseBean 實現序列化


10 *
11 * @author bojiangzhou
12 * @date 2017-03-27
13 */
14 public abstract class BaseBean implements Serializable {
15
16 /**
17 * ToStringBuilder – 用於輔助實現Object.toString()方法


18 */
19 public String toString() {
20 return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
21 }
22
23 }


Result則封裝了返回的結果,這裏主要與ExtJs相對應,ExtJs進行ajax請求時,返回success=true,會進入success()方法;返回success=false,會進入failure()方法。同時,封裝了返回消息、標識、錯誤信息等,便於前端拿到相應數據。

1 package com.lyyzoo.bean;
2
3 import lombok.Data;
4
5 import java.util.Map;
6
7 /**
8 * 封裝返回結果


9 *
10 * @author bojiangzhou
11 * @date 2017-03-27
12 */
13 @Data
14 public class Result extends BaseBean {
15 private static final long serialVersionUID = 1L;
16
17 /**
18 * 成功標誌
19 */
20 public boolean success;
21 /**
22 * 返回標示
23 */
24 public Integer code;
25 /**
26 * 相關消息
27 */
28 public String msg;
29 /**
30 * 相關數據
31 */
32 public Object data;
33 /**
34 * 錯誤詳細
35 */
36 public Map errors;
37
38 public Result() {
39
40 }
41
42 public Result(boolean success) {
43 this.success = success;
44 }
45
46 public Result(boolean success, Integer code, Object data, String msg) {
47 this(success);
48 this.code = code;
49 this.data = data;
50 this.msg = msg;
51 }
52
53 public Result(Integer code, Map errors, String msg) {
54 this.success = false;
55 this.code = code;
56 this.errors = errors;
57 this.msg = msg;
58 }
59
60 public boolean isSuccess() {
61 return this.success;
62 }
63
64 public boolean hasErrors() {
65 if (this.errors != null && this.errors.size() > 0) {
66 return true;
67 }
68 return false;
69 }
70 }


其它的主要是一些工具類,例如Arrays繼承了org.apache.commons.lang3.ArrayUtils,使得Arrays具有了豐富的操作集合的工具。再如Dates,Dates裏封裝了大量的對日期操作的方法,比如格式化日期、獲取某個日期當天的開始時間和結束時間等。

(2) lyyzoo-starter-base

lyyzoo-starter-base 目錄結構:

Java開發進銷存管理系統(二)


lyyzoo-starter-base是web應用程序的一個基礎,主要封裝了基礎實體類以及spring-base和日誌的配置。實體的頂層是AbstractEntity,AbstractEntity繼承BaseBean。AbstractEntity重寫了equals和hashCode方法,主要做的處理是,如果兩個實體的id相同也算這兩個對象爲同一個對象。代碼如下:

1 package com.lyyzoo.data.entity;
2
3 import com.lyyzoo.bean.BaseBean;
4
5 import java.io.Serializable;
6
7 /**
8 * 抽象實體類 重寫equals和hashCode方法
9 *
10 * @author bojiangzhou
11 * @date 2017-03-28
12 */
13 @SuppressWarnings("serial")
14 public abstract class AbstractEntity extends BaseBean implements Serializable {
15
16 public abstract ID getId();
17
18 @Override
19 public boolean equals(Object obj) {
20
21 if (null == obj) {
22 return false;
23 }
24 if (this == obj) {
25 return true;
26 }
27 if (!getClass().equals(obj.getClass())) {
28 return false;
29 }
30
31 AbstractEntity> that = (AbstractEntity>) obj;
32
33 return null == this.getId() ? false : this.getId().equals(that.getId());
34 }
35
36 @Override
37 public int hashCode() {
38 int hashCode = 17;
39
40 hashCode += null == getId() ? 0 : getId().hashCode() * 31;
41
42 return hashCode;
43 }
44
45 }


BaseEntity則繼承AbstractEntity,BaseEntity作爲其它實體的父類存在,BaseEntity定義了主鍵ID的生成策略。代碼如下:

1 package com.lyyzoo.data.entity;
2
3 import lombok.Data;
4
5 import javax.persistence.GeneratedValue;
6 import javax.persistence.GenerationType;
7 import javax.persistence.Id;
8 import javax.persistence.MappedSuperclass;
9
10 /**
11 * 統一定義id的entity基類.
12 *


13 * 抽象實體基類,提供統一的ID,和相關的基本功能
14 *


15 */
16 @Data
17 @MappedSuperclass
18 @SuppressWarnings("serial")
19 public abstract class BaseEntity extends AbstractEntity {
20
21 /**
22 * id 字段
23 */
24 @Id
25 protected Long id;
26
27 @Id
28 @GeneratedValue(strategy = GenerationType.IDENTITY)
29 public Long getId() {
30 return this.id;
31 }
32
33 public void setId(Long id) {
34 this.id = id;
35 }
36 }


spring-base.xml是基礎配置,主要配置了掃描系統的配置文件、註解等。

1 
2 3 xmlns="http://www.springframework.org/schema/beans"
4 xmlns:beans="http://www.springframework.org/schema/beans"
5 xmlns:context="http://www.springframework.org/schema/context"
6 xmlns:aop="http://www.springframework.org/schema/aop"
7 xmlns:tx="http://www.springframework.org/schema/tx"
8 xmlns:util="http://www.springframework.org/schema/util"
9 xmlns:mvc="http://www.springframework.org/schema/mvc"
10 xmlns:jee="http://www.springframework.org/schema/jee"
11 xmlns:jdbc="http://www.springframework.org/schema/jdbc"
12 xmlns:tool="http://www.springframework.org/schema/tool"
13 xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
14 xsi:schemaLocation="
15 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
16 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
17 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
18 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
19 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
20 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
21 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
22 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
23 http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool.xsd
24 http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd"
25 >
26
27 Spring Base Config
28
29
30
31
32
33
34
35
36

37
38
39
40
41


(3) lyyzoo-starter-jpa

lyyzoo-starter-jpa 目錄結構:

Java開發進銷存管理系統(二)


lyyzoo-starter-jpa集成了持久化相關的操作,配置等。

首先需要做持久化相關的配置(spring-base-jpa.xml),如數據源、SessionFactory、事務管理等。數據源使用c3p0,數據源相關配置如數據庫驅動、地址等寫到到配置文件中。配置Hibernate SessionFactory的同時,增加了JdbcTemplate。事務管理器使用Hibernate的事務管理。總的配置如下:

1 
2 3 xmlns="http://www.springframework.org/schema/beans"
4 xmlns:beans="http://www.springframework.org/schema/beans"
5 xmlns:tx="http://www.springframework.org/schema/tx"
6 xmlns:aop="http://www.springframework.org/schema/aop"
7 xmlns:context="http://www.springframework.org/schema/context"
8 xmlns:mvc="http://www.springframework.org/schema/mvc"
9 xmlns:util="http://www.springframework.org/schema/util"
10 xmlns:jee="http://www.springframework.org/schema/jee"
11 xmlns:jdbc="http://www.springframework.org/schema/jdbc"
12 xmlns:tool="http://www.springframework.org/schema/tool"
13 xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
14 xsi:schemaLocation="
15 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
16 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
17 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
18 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
19 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
20 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
21 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
22 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
23 http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool.xsd
24 http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd"
25 >
26
27 Spring Jpa
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

52
53
54
55
56
57
58 ${hibernate.dialect}
59 ${hibernate.show.sql}
60 ${hibernate.hbm2ddl.auto}
61

62

63
64
65
66 com.lyyzoo.*.entity
67

68

69

70
71
72
73
74

75
76
77

78
79
80
81

82
83
84
85
86

87
88
89
90
91
92
93
94
95
96
97
98

99

100
101
102
103
104
105

106
107
108
109
110


然後在com.lyyzoo.data.domain包下,Page封裝了分頁相關的屬性,如當前頁數據、頁碼、每頁大小、總頁數等。com.lyyzoo.data.util包下,Sqls封裝了操作sql語句的工具,比如根據傳入的查詢語句,返回查詢總數的sql語句。在com.lyyzoo.data.jdbc包下,JdbcTemplateSupport封裝了比較複雜的查詢,比如根據傳入的參數、分頁條件等,自動查詢數據、總數並封裝到Page裏返回。com.lyyzoo.data.dao包下,SimpleDao封裝了基於Hibernate SessionFactory的簡單的增刪改查操作,如save(),get()等方法。dao包下,BaseDao繼承SimpleDao,並代理了JdbcTemplateSupport,使得BaseDao具有SimpleDao和JdbcTemplateSupport的功能,BaseDao則作爲其它dao的一個父類存在。最後,在com.lyyzoo.service包下,BaseService代理了BaseDao,進行了進一步的封裝,BaseService是其它service類的父類,其它service就可以直接調用save、get、page等內置方法。

(4) lyyzoo-starter-web

lyyzoo-starter-web自然跟web相關,在com.lyyzoo.web包下,BaseController作爲controller類的父類,主要封裝了返回結果集等信息。還有web相關配置,如視圖解析器等內容:

1 
2 3 xmlns="http://www.springframework.org/schema/beans"
4 xmlns:beans="http://www.springframework.org/schema/beans"
5 xmlns:tx="http://www.springframework.org/schema/tx"
6 xmlns:aop="http://www.springframework.org/schema/aop"
7 xmlns:context="http://www.springframework.org/schema/context"
8 xmlns:mvc="http://www.springframework.org/schema/mvc"
9 xmlns:task="http://www.springframework.org/schema/task"
10 xmlns:util="http://www.springframework.org/schema/util"
11 xmlns:jee="http://www.springframework.org/schema/jee"
12 xmlns:jdbc="http://www.springframework.org/schema/jdbc"
13 xmlns:tool="http://www.springframework.org/schema/tool"
14 xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
15 xsi:schemaLocation="
16 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
17 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
18 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
19 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
20 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
21 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
22 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
23 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
24 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
25 http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool.xsd
26 http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd"
27 >
28
29 Spring Web
30
31
32
33
34
35
36
37
38

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

54
55


(5) 類結構圖

類結構圖參考《底層類結構.vsd》

Java開發進銷存管理系統(二)


--未完,待續--

相關文章