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

五、數據庫與實體設計

在開始開發一個系統之前,首先需要做的就是根據需求分析設計系統的實體對象以及對應的數據庫表結構,這是開發的基礎。

根據前面的需求分析設計的功能模塊,實體對象可以分爲5個模塊,分別是系統模塊(system)、基礎模塊(base)、採購模塊(purchase)、銷售模塊(sale)、庫存模塊(stock)。下面是實體清單:

有了清單之後,利用PowerDesigner進行數據庫物理模型設計。由於擁有對數據庫的完全控制權,所以不對錶設置約束,所有的約束在程序代碼中進行控制。下面列出各個實體的屬性即對應的表,具體可參考《數據庫物理模型.pdm》。物理模型設計完成後,創建名爲gpss的數據庫,然後創建各個表。

數據庫模型:

六、系統功能實現

1. 創建工程

需求分析做完了,技術沒問題,底層架構也設計好了,數據庫設計好了,前面的所有準備工作做完了,下面就要進行燃氣管進銷存系統的編碼實現了。首先要做的工作就是創建工程,項目名擬爲gpss,即燃氣管進銷存(Gas Purchase Sale Stock)的縮寫,工程名則爲lyyzoo-gpss。

在IDEA中創建lyyzoo-gpss的maven工程,繼承lyyzoo。同時,在lyyzoo-gpss下創建兩個子模塊,分別爲lyyzoo-gpss-base和lyyzoo-gpss-web,base模塊包含了系統的dao、entity、service等層次,主要爲java文件;web則包含了controller層、jsp、js等web相關資源文件。

lyyzoo-gpss目錄結構:

lyyzoo-gpss > pom.xml:

1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3   <modelVersion>4.0.0</modelVersion>
 4   <parent>
 5     <groupId>com.lyyzoo</groupId>
 6     <artifactId>lyyzoo</artifactId>
 7     <version>1.0-SNAPSHOT</version>
 8   </parent>
 9   <groupId>com.lyyzoo.gpss</groupId>
10   <artifactId>lyyzoo-gpss</artifactId>
11   <name>lyyzoo :: gpss</name>
12   <packaging>pom</packaging>
13 
14   <modules>
15     <module>lyyzoo-gpss-base</module>
16     <module>lyyzoo-gpss-web</module>
17   </modules>
18 
19   <profiles>
20     <!-- 開發環境 -->
21     <profile>
22       <id>dev</id>
23       <activation>
24         <property>
25           <name>env</name>
26           <value>dev</value>
27         </property>
28       </activation>
29     </profile>
30     <!-- 線上環境 -->
31     <profile>
32       <id>online</id>
33       <activation>
34         <property>
35           <name>env</name>
36           <value>online</value>
37         </property>
38       </activation>
39     </profile>
40   </profiles>
41   <build>
42     <plugins>
43       <plugin>
44         <groupId>com.juvenxu.portable-config-maven-plugin</groupId>
45         <artifactId>portable-config-maven-plugin</artifactId>
46         <version>1.1.5</version>
47         <executions>
48           <execution>
49             <goals>
50               <goal>replace-package</goal>
51             </goals>
52           </execution>
53         </executions>
54         <configuration>
55           <portableConfig>src/main/resources/portable/config-${env}.xml</portableConfig>
56         </configuration>
57       </plugin>
58     </plugins>
59   </build>
60 
61 </project>

lyyzoo-gpss-base主要是dao層、service層、model層的集成,所以需要依賴於底層lyyzoo-starter-jpa、lyyzoo-starter-web。

lyyzoo-gpss-base > pom.xml:

1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3     <modelVersion>4.0.0</modelVersion>
 4     <parent>
 5         <groupId>com.lyyzoo.gpss</groupId>
 6         <artifactId>lyyzoo-gpss</artifactId>
 7         <version>1.0-SNAPSHOT</version>
 8     </parent>
 9     <artifactId>lyyzoo-gpss-base</artifactId>
10     <name>lyyzoo :: gpss :: base</name>
11     <packaging>jar</packaging>
12 
13     <dependencies>
14         <dependency>
15             <groupId>com.lyyzoo</groupId>
16             <artifactId>lyyzoo-starter-jpa</artifactId>
17         </dependency>
18         <dependency>
19             <groupId>com.lyyzoo</groupId>
20             <artifactId>lyyzoo-starter-web</artifactId>
21         </dependency>
22     </dependencies>
23 
24 </project>

lyyzoo-gpss-web是web相關,是controller層所在。依賴於lyyzoo-gpss-base、lyyzoo-starter-base等。

lyyzoo-gpss-web > pom.xml:

1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 3     <modelVersion>4.0.0</modelVersion>
 4     <parent>
 5         <groupId>com.lyyzoo.gpss</groupId>
 6         <artifactId>lyyzoo-gpss</artifactId>
 7         <version>1.0-SNAPSHOT</version>
 8     </parent>
 9     <artifactId>lyyzoo-gpss-web</artifactId>
10     <name>lyyzoo :: gpss :: web</name>
11     <packaging>war</packaging>
12 
13     <dependencies>
14         <dependency>
15             <groupId>com.lyyzoo.gpss</groupId>
16             <artifactId>lyyzoo-gpss-base</artifactId>
17             <version>${project.version}</version>
18         </dependency>
19         <dependency>
20             <groupId>com.lyyzoo</groupId>
21             <artifactId>lyyzoo-starter-base</artifactId>
22         </dependency>
23         <dependency>
24             <groupId>com.lyyzoo</groupId>
25             <artifactId>lyyzoo-starter-jpa</artifactId>
26         </dependency>
27         <dependency>
28             <groupId>com.lyyzoo</groupId>
29             <artifactId>lyyzoo-starter-web</artifactId>
30         </dependency>
31         <dependency>
32             <groupId>com.lyyzoo</groupId>
33             <artifactId>lyyzoo-starter-test</artifactId>
34         </dependency>
35 
36 
37     </dependencies>
38 
39 
40     <build>
41         <finalName>gpss</finalName>
42         <outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>
43     </build>
44 
45 </project>

2. 系統配置

工程建好後,首要要做的就是系統的配置工作了,如web.xml,這應該算是web項目的起點了。進入lyyzoo-gpss-web/src/main/webapp/WEB-INF/web.xml,進行web的配置,主要的一些配置有加載系統的配置文件、Spring、字符過濾器、SpringMVC等配置。

一些基礎的配置如下:

1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns="http://java.sun.com/xml/ns/javaee"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 5     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 6          version="3.0">
 7 
 8   <display-name>lyyzoo :: gpss</display-name>
 9 
10   <!-- 加載配置文件 -->
11   <context-param>
12     <param-name>contextConfigLocation</param-name>
13     <param-value>classpath*:/spring/spring-base*.xml</param-value>
14   </context-param>
15 
16   <!-- Spring -->
17   <listener>
18     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
19   </listener>
20 
21   <!-- 編碼過濾器 -->
22   <filter>
23     <filter-name>encodingFilter</filter-name>
24     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
25     <init-param>
26       <param-name>encoding</param-name>
27       <param-value>UTF-8</param-value>
28     </init-param>
29     <init-param>
30       <param-name>forceEncoding</param-name>
31       <param-value>true</param-value>
32     </init-param>
33   </filter>
34   <filter-mapping>
35     <filter-name>encodingFilter</filter-name>
36     <url-pattern>/*</url-pattern>
37   </filter-mapping>
38 
39   <!-- 訪問控制 -->
40   <filter>
41     <filter-name>VisitFilter</filter-name>
42     <filter-class>com.lyyzoo.gpss.filter.VisitFilter</filter-class>
43   </filter>
44   <filter-mapping>
45     <filter-name>VisitFilter</filter-name>
46     <url-pattern>/admin/*</url-pattern>
47   </filter-mapping>
48 
49   <!-- Spring MVC -->
50   <servlet>
51     <servlet-name>SpringMVC</servlet-name>
52     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
53     <init-param>
54       <param-name>contextConfigLocation</param-name>
55       <param-value>classpath*:/spring/spring-web*.xml</param-value>
56     </init-param>
57     <load-on-startup>1</load-on-startup>
58   </servlet>
59   <servlet-mapping>
60     <servlet-name>SpringMVC</servlet-name>
61     <url-pattern>/</url-pattern>
62   </servlet-mapping>
63 
64   <!-- 錯誤頁面 -->
65   <error-page>
66     <error-code>404</error-code>
67     <location>/error/404</location>
68   </error-page>
69   <error-page>
70     <error-code>500</error-code>
71     <location>/error/500</location>
72   </error-page>
73   <error-page>
74     <exception-type>java.lang.Throwable</exception-type>
75     <location>/error/500</location>
76   </error-page>
77 
78   <!-- 首頁 -->
79   <welcome-file-list>
80     <welcome-file>/</welcome-file>
81   </welcome-file-list>
82 
83 </web-app>

接着,配置系統將會用到的一些屬性,如JDBC驅動、數據庫用戶名和密碼等。在lyyzoo-gpss-web/src/main/resources/config下,創建config.properties配置文件,這個配置文件中的屬性將會被底層spring配置的文件所引用。

比如JDBC的屬性:

1 ####################################
 2 #            DATABASE
 3 ####################################
 4 # local_db
 5 jdbc.driver=net.sf.log4jdbc.DriverSpy
 6 jdbc.url=jdbc:log4jdbc:mysql://localhost:3306/gpss?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false
 7 jdbc.username=root
 8 jdbc.password=root
 9 
10 # 初始化連接
11 c3p0.initialPoolSize=5
12 # 連接池保留最小連接數
13 c3p0.minPoolSize=5
14 # 連接池保留最大連接數
15 c3p0.maxPoolSize=15
16 # 最大空閒時間
17 c3p0.maxIdleTime=600
18 
19 #hibernate
20 hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
21 hibernate.show.sql=false
22 hibernate.hbm2ddl.auto=update

但是上面的配置只是本機的一個開發環境,如果我將項目發佈到生產環境,那就至少需要重新修改數據庫地址、用戶名和密碼。這樣是比較麻煩的,所以,在lyyzoo-gpss-web/src/main/portable下創建兩個xml文件,分別爲開發環境和生產環境的:config-dev.xml和config-online.xml。

config-online.xml:

1 <?xml version="1.0" encoding="utf-8" ?>
 2 
 3 <portable-config>
 4     <config-file path="WEB-INF/classes/config/config.properties">
 5 
 6         <!-- JDBC -->
 7         <replace key="jdbc.driver"><![CDATA[com.mysql.jdbc.Driver]]></replace>
 8         <replace key="jdbc.url"><![CDATA[jdbc:mysql://192.168.1.91:30112/fff2f025c2b04?useUnicode=true&characterEncoding=utf-8]]></replace>
 9         <replace key="jdbc.username">a6564a1169d94</replace>
10         <replace key="jdbc.password">d3e6d1aea5e04</replace>
11 
12         <!-- hibernate -->
13         <replace key="hibernate.show.sql">false</replace>
14         <replace key="hibernate.hbm2ddl.auto">none</replace>
15 
16         <!-- debug -->
17         <replace key="app.debug">false</replace>
18         <replace key="app.env">online</replace>
19         <replace key="logback.level">INFO</replace>
20         <replace key="jdbc.resultsettable">ERROR</replace>
21 
22 
23     </config-file>
24 </portable-config>

然後配置一個“不同環境打包”的maven插件——“portable-config-maven-plugin”,通過該插件,就可以在打包的時候加上需要打包的環境,例如指定online環境,在打包時就會將config-online.xml中的屬性替換到config.properties裏,這樣一來就不必我們手動去替換了。配置好之後,我們在打包時可以使用命令[mvn clean package –Denv=online]打包線上環境的war包。

maven環境配置,在lyyzoo-gpss > pom.xml中配置兩個環境:

1 <profiles>
 2   <!-- 開發環境 -->
 3   <profile>
 4     <id>dev</id>
 5     <activation>
 6       <property>
 7         <name>env</name>
 8         <value>dev</value>
 9       </property>
10     </activation>
11   </profile>
12   <!-- 線上環境 -->
13   <profile>
14     <id>online</id>
15     <activation>
16       <property>
17         <name>env</name>
18         <value>online</value>
19       </property>
20     </activation>
21   </profile>
22 </profiles>

不同環境打包插件(portable-config-maven-plugin)的配置:

1 <build>
 2   <plugins>
 3     <plugin>
 4       <groupId>com.juvenxu.portable-config-maven-plugin</groupId>
 5       <artifactId>portable-config-maven-plugin</artifactId>
 6       <version>1.1.5</version>
 7       <executions>
 8         <execution>
 9           <goals>
10             <goal>replace-package</goal>
11           </goals>
12         </execution>
13       </executions>
14       <configuration>
15         <portableConfig>src/main/resources/portable/config-${env}.xml</portableConfig>
16       </configuration>
17     </plugin>
18   </plugins>
19 </build>

3. 模塊分層

3.1 lyyzoo-gpss-base

工程建好後,創建包的結構。按照一般的分層方式,分爲dao層、entity層、service層,同時,每層下按模塊劃分爲system、base、purchase、sale、stock。然後在entity下根據表設計創建實體類。

lyyzoo-gpss-base 目錄結構:

3.2 lyyzoo-gpss-web

然後是lyyzoo-gpss-web模塊,該模塊主要是controller層,以及靜態資源文件、jsp文件等。com.lyyzoo.gpss.web作爲controller層的包,同樣,在web下按系統模塊劃分。

lyyzoo-gpss-web 目錄結構:

3.3 靜態資源文件

lyyzoo-gpss-web/src/main/webapp/static作爲靜態文件的根目錄,static/lib目錄作爲三方類庫的根目錄,如ExtJs、jQuery、其它的插件等。static/css是系統css文件的根目錄;static/img是圖片的根目錄;static/js是系統js文件根目錄,/js下同樣按模塊劃分。

靜態資源文件目錄結構:

3.4 JSP文件

jsp文件不能直接讓用戶訪問,需要放到/WEB-INF下,與配置的spring視圖解析器相對應,所有的jsp文件放到/WEB-INF/view目錄下,view目錄下按模塊劃分,index.jsp是系統的登錄頁面。/WEB-INF/layout/目錄下,將jsp中需要引入的一些資源等做了整合,如ExtJs的文件、meta描述信息、taglib等,整合後,jsp中如果需要引入整合jsp即可,可減少很多重複的工作。

taglib.jsp中引入了標籤和設置了資源的路徑:

1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 
 3 <%-- 引入標籤 --%>
 4 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 5 <%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
 6 <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
 7 <%@taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
 8 
 9 <%-- 資源路徑 --%>
10 <c:set var="CTX" value="${pageContext.request.contextPath}" />
11 <c:set var="STATIC_CTX_URL" value="${CTX}/static" />
12 <c:set var="LIB" value="${STATIC_CTX_URL}/lib" />
13 <c:set var="JS" value="${STATIC_CTX_URL}/js"/>
14 <c:set var="CSS" value="${STATIC_CTX_URL}/css"/>
15 <c:set var="IMG" value="${STATIC_CTX_URL}/img"/>

meta.jsp中主要設置了一些meta描述信息和ICO圖標:

1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <%@ include file="/WEB-INF/layout/taglib.jsp" %>
 3 
 4 <meta charset="utf-8">
 5 <meta http-equiv="X-UA-Compatible" content="IE=9,chrome=1">
 6 
 7 <meta name="description" content="">
 8 <meta name="author" content="">
 9 <meta name="renderer" content="webkit">
10 <%-- 圖標 --%>
11 <link type="image/x-icon" href="${IMG}/favicon.ico" rel="icon">

extjs-neptune.jsp則引入了ExtJs相關的css和js文件,以及jQuery等: 

1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <%@ include file="/WEB-INF/layout/taglib.jsp" %>
 3 
 4 <link rel="stylesheet" type="text/css" href="${LIB}/ext/4.2.2/theme/ext-theme-neptune/ext-theme-neptune-all.css"/>
 5 <link rel="stylesheet" type="text/css" href="${LIB}/ext/4.2.2/icons/icon.css"/>
 6 
 7 <script type="text/javascript" src="${LIB}/ext/4.2.2/ext-all.js"></script>
 8 <script type="text/javascript" src="${LIB}/ext/4.2.2/locale/ext-lang-zh_CN.js"></script>
 9 <script type="text/javascript" src="${LIB}/jquery/2.1.1/jquery.js"></script>
10 <script type="text/javascript" src="${JS}/Global.js"></script>
11 
12 <script type="text/javascript">
13     window.CTX             = "${CTX}";
14     window.STATIC_CTX_URL  = "${STATIC_CTX_URL}";
15     window.LIB             = "${LIB}";
16     window.JS              = "${JS}";
17     window.CSS             = "${CSS}";
18     window.IMG             = "${IMG}";
19 </script>

WEB-INF目錄結構:

3.4 登錄實現舉例

首先創建一個BaseController,BaseController繼承底層的BaseController,增加了HttpSession,以及獲取當前登錄用戶的方法,這樣其它的controller繼承該BaseController後,就可以非常方便的獲得當前session和登錄用戶了。

BaseController代碼如下:

1 package com.lyyzoo.gpss.web;
 2 
 3 import com.lyyzoo.gpss.entity.system.User;
 4 import org.springframework.beans.factory.annotation.Autowired;
 5 
 6 import javax.servlet.http.HttpSession;
 7 
 8 /**
 9  *
10  * <p>
11  *
12  * @author bojiangzhou
13  * @date 2017-04-02
14  */
15 public class BaseController extends com.lyyzoo.web.BaseController {
16 
17     @Autowired
18     protected HttpSession session;
19 
20     public User getCurrentUser(){
21         return (User) session.getAttribute("currentUser");
22     }
23 
24     public Long getCurrentUserId(){
25         return getCurrentUser().getId();
26     }
27 
28     public String getCurrentUserAccount(){
29         return getCurrentUser().getAccount();
30     }
31 
32 }

創建HomeController作爲登錄和主頁的處理器。主要包含的方法有訪問登錄頁面,訪問登錄後的主頁,以及登錄處理等。Controller需要加上@Controller標註該類爲controller,使用@RequestMapping()支持訪問rest形式的地址訪問。HomeController中注入UserService,用於處理用戶登錄相關的業務。

用戶進入登錄界面,jsp頁面以<img src=” ${CTX}/vcode”>的形式請求驗證碼,驗證碼使用工具類生成,以流的形式輸出,生成的驗證碼保存到session中。用戶輸入登錄賬號、密碼和驗證碼登錄系統。首先前臺會檢測輸入是否爲空等,傳到後臺,從session中取出驗證碼判斷驗證碼是否正確,不正確則刷新驗證碼並且需要用戶重新輸入驗證碼。驗證碼通過後,使用登錄賬號和密碼查找數據庫,如果有,則將該用戶保存到session中,跳轉到管理頁面,登錄成功。否則提示用戶名或密碼錯誤。

HomeController代碼如下:

1 package com.lyyzoo.gpss.web;
  2 
  3 import com.lyyzoo.gpss.entity.system.User;
  4 import com.lyyzoo.gpss.service.system.UserService;
  5 import com.lyyzoo.gpss.util.VCodeGenerator;
  6 import com.lyyzoo.util.Cryptos;
  7 import com.lyyzoo.util.Strings;
  8 import org.springframework.beans.BeanUtils;
  9 import org.springframework.beans.factory.annotation.Autowired;
 10 import org.springframework.stereotype.Controller;
 11 import org.springframework.web.bind.annotation.PathVariable;
 12 import org.springframework.web.bind.annotation.RequestMapping;
 13 import org.springframework.web.bind.annotation.RequestMethod;
 14 
 15 import javax.imageio.ImageIO;
 16 import javax.servlet.http.HttpServletResponse;
 17 import javax.servlet.http.HttpSession;
 18 import java.awt.image.BufferedImage;
 19 import java.io.IOException;
 20 
 21 /**
 22  * <p>
 23  *
 24  * @author bojiangzhou
 25  * @date 2017-03-29
 26  */
 27 @Controller
 28 @RequestMapping("")
 29 public class HomeController extends BaseController {
 30 
 31     @Autowired
 32     private UserService userService;
 33 
 34     /**
 35      * 到首頁/登錄頁面
 36      */
 37     @RequestMapping(value = {"", "/", "/index", "/login"})
 38     public String index(){
 39         return "/index";
 40     }
 41 
 42     /**
 43      * 管理員主頁
 44      */
 45     @RequestMapping("/admin/home")
 46     public String toHome(){
 47         return "/home/home";
 48     }
 49 
 50     /**
 51      * 登錄
 52      */
 53     @RequestMapping(value = "/login", method = RequestMethod.POST)
 54     public String login(String account, String password, String vcode, HttpSession session){
 55         String redirect = REDIRECT("/login");
 56         //基本驗證
 57         if(Strings.isNullOrEmpty(account)){
 58             session.setAttribute("errorMsg", "賬號必須填寫!");
 59             return redirect;
 60         }
 61         if(Strings.isNullOrEmpty(password)){
 62             session.setAttribute("errorMsg", "密碼必須填寫!");
 63             return redirect;
 64         }
 65         if(Strings.isNullOrEmpty(vcode)){
 66             session.setAttribute("errorMsg", "驗證碼必須填寫!");
 67             return redirect;
 68         }
 69         //驗證碼
 70         String sessionVcode = (String) session.getAttribute("vcode");
 71         if(!vcode.equalsIgnoreCase(sessionVcode)){
 72             session.setAttribute("errorMsg", "驗證碼錯誤!");
 73             return redirect;
 74         }
 75         //驗證用戶名和密碼
 76         password = Cryptos.encryptMd5(password);
 77         User loginUser = userService.login(account, password);
 78         if(loginUser == null){
 79             session.setAttribute("errorMsg", "賬號或密碼錯誤!");
 80             return redirect;
 81         }
 82         if(loginUser.getIsLocked() == User.IsLocked.YES){
 83             session.setAttribute("errorMsg", "賬號已鎖定,不能登錄!");
 84             return redirect;
 85         }
 86 
 87         //保存到session的時候清除密碼
 88         User currentUser = new User();
 89         BeanUtils.copyProperties(loginUser, currentUser);
 90         currentUser.setPassword(null);
 91 
 92         //登錄成功
 93         session.setAttribute("currentUser", currentUser);
 94 
 95         return REDIRECT("/admin/home");
 96     }
 97 
 98     /**
 99      * 獲取驗證碼
100      */
101     @RequestMapping("/vcode")
102     public void getVCode(HttpSession session, HttpServletResponse response) throws IOException {
103         //創建驗證碼生成器對象
104         VCodeGenerator vcGenerator = new VCodeGenerator();
105         //生成驗證碼
106         String vcode = vcGenerator.generatorVCode();
107         //將驗證碼保存在session域中,以便判斷驗證碼是否正確
108         session.setAttribute("vcode", vcode);
109         //生成驗證碼圖片
110         BufferedImage vImg = vcGenerator.generatorRotateVCodeImage(vcode, true);
111         //輸出圖像
112         ImageIO.write(vImg, "gif", response.getOutputStream());
113     }
114 
115     /**
116      * 退出系統
117      */
118     @RequestMapping("/logoff")
119     public String logoff(HttpSession session){
120         session.invalidate();
121         return REDIRECT("/");
122     }
123 
124     @RequestMapping("/function")
125     public String function(){
126         return "/home/function";
127     }
128 
129     @RequestMapping("/welcome")
130     public String welcome(){
131         return "/home/welcome";
132     }
133 
134     /**
135      * 錯誤頁面
136      * @param code
137      * @return
138      */
139     @RequestMapping("/error/{code}")
140     public String error(@PathVariable String code) {
141         return "/error/" + code;
142     }
143 
144 
145 
146 }

UserService中根據用戶名和密碼獲取用戶:

1 package com.lyyzoo.gpss.service.system;
 2 
 3 @Service
 4 public class UserService extends BaseService<User> {
 5     @Autowired
 6     private UserDao userDao;
 7     /**
 8      * 獲取登錄用戶
 9      */
10     public User login(String account, String password){
11         Map<String, Object> filter = new HashMap<>();
12         filter.put("account", account);
13         filter.put("password", password);
14         filter.put("state", Applications.Flag.YES);
15 
16         User loginUser = get(filter);
17         return loginUser;
18     }
19 }

七、系統的調試與部署

1. 測試

系統開發完成後,首先需要在本地整體測試,從登錄開始,每個模塊,每個功能,每個流程具體的去測試。

首先測試如果未登錄,用戶是不能訪問管理頁面的,直接在地址欄輸入訪問地址看是否跳轉到登錄頁面。然後至少測試各個角色相關的賬號登錄是否正常,登錄後,每個角色擁有的菜單是否顯示正常。

其它模塊的測試,使用各個角色對應的賬號,登錄系統,進行相應功能的測試。如管理員進入系統錄入基礎數據,商品信息、倉庫信息等。採購管理員錄入供應商信息,錄入採購訂單,提交審覈。銷售管理員錄入客戶信息,錄入銷售訂單,提交審覈。庫存管理員審覈採購訂單,審覈通過,則庫存增加;審覈銷售訂單,審覈通過,則庫存減少。查看庫存信息,相應的操作之後,庫存量是否正確。測試結果可查看系統測試截圖。

系統容錯性測試,主要是測試輸入一些錯誤的數據類型以及超出範圍的數值測試系統在異常條件下的行爲。系統在這方面做得比較好,如果用戶輸入了一些非法的數據,會立即提醒用戶輸入正確的數據。首先會在前臺判斷用戶輸入的數據的合法性、是否必須輸入等,數據傳到後臺後,還會在代碼裏判斷一次數據是否正確,纔會保存到數據庫。而系統使用的Jdbc也能在一定程度上防止SQL注入等問題。如果系統發生一些無法預測的異常,也會以友好的界面提示用戶,以便技術員及時維護系統。

總體來說,整個的測試過程比較順利,也存在一些小問題,就立即修復了。功能全部實現了,該系統能滿足一個基本的進銷存流程,以後也還可以擴展。

2. 部署

我這裏使用磨泊雲來部署項目,磨泊雲使用簡單且在一定限度內免費,部署測試比較合適。首先在磨泊雲上創建名爲gpss的Java應用,接着創建mysql服務,並將其綁定到該java應用,複製數據庫連接到配置文件中。導出本地的gpss數據庫,導入到創建的mysql應用裏。然後在IDEA中使用mvn clean package –Denv=online命令打包線上環境的war包,將war包發佈到磨泊雲上。啓動項目,然後訪問,測試,一些都正常。可訪問域名http://gpss.butterfly.mopaasapp.com/查看,由於沒處理好ext的兼容性問題,建議使用谷歌瀏覽器查看。後面再學學如何部署到像阿里雲等雲上。

八、github地址

https://github.com/bojiangzhou/lyyzoo-gpss

(完)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章