SSM架構實作 汽車的租賃管理系統
在這個暑假假期中,我們團隊做了一個汽車的租賃管理的網站,我們團隊采用分工的開發的方式,其中史秀源負責這個系統的統籌管理,羅竣元負責汽車管理的子產品,譚枝敬負責前台職工管理,盧偉斌負責前台管理,闫龍飛負責背景的管理。我們的分工的系統是放在碼
雲上(https://git.oschina.net/nn839155963)和用smartgit進行項目的管理的,現在我們的car 項目截圖如下,每個成員将自己修改的内容pull到碼雲上,其他成員實時更新網站上的東西,整個團隊就可以知道整個項目的開發進度,進而實作了對項目開發過程的精确的管理。
管理
雲上
我們這個系統采用ssm架構,就是Spring ,SpringMVC ,mybstis 的簡稱,mybatis的作用作為一個連接配接資料庫的架構,可以很好配置連接配接好資料庫, 有mybatis,我們對資料庫增删改查的操作更為簡便了。SSM架構是目前網站開發中比較常用和比較流行的架構,其中
ssm架構有六個層次controller ,dao層,model層,service層,mapping層,serviceImp層,我們的工程相關的截圖如下;
相關的六個層次的截圖如下其中,mybatis 用來連接配接資料庫,有mybatis,我們對資料庫增删改查的操作更為簡便了。SSM架構,我們用MAVEN工具對他們進行管理,maven 工具的主要作用就是為我們的工程自動配置好我們所需的架包,是通過POM.xml 檔案進行依懶管理的。pom.xml的代碼如下,配置了相關的依賴的管理;
其中pom.xml 配置檔案的代碼如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zzty</groupId>
<artifactId>car</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>car Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<!-- spring版本号 -->
<spring.version>4.0.2.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.1.1</mybatis.version>
<!-- log4j日志檔案管理包版本 -->
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<!-- 表示開發的時候引入,釋出的時候不會加載此包 -->
<scope>test</scope>
</dependency>
<!-- spring核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- mybatis/spring包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.1.0</version>
</dependency>
<!-- 導入java ee jar 包 -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<!-- 導入Mysql資料庫連結jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<!-- 導入dbcp的jar包,用來在applicationContext.xml中配置資料庫 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<!-- JSTL标簽類 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 日志檔案管理包 -->
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- 格式化對象,友善輸出日志 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log end -->
<!-- 映入JSON -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- 上傳元件包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
<!-- QRcode -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.2.1</version>
</dependency>
</dependencies>
<build>
<finalName>car</finalName>
</build>
</project>
2 現在就是配置resources 下面的四個檔案
jdbc.properties 連接配接你的資料庫的配置,還有一些作用自己了解。
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.31.242:3306/car_db?useUnicode=true&characterEncoding=utf-8&useSSL=false
username=car
password=root
#定義初始連接配接數
initialSize=0
#定義最大連接配接數
maxActive=20
#定義最大空閑
maxIdle=20
#定義最小空閑
minIdle=1
#定義最長等待時間
maxWait=60000
imagePath=D:/sts/workspace/car/src/main/webapp/resources/image/
log4.properties 用來記錄工程運作調試的記錄什麼的。
#定義LOG輸出級别
log4j.rootLogger=INFO,Console,File
#定義日志輸出目的地為控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以靈活地指定日志輸出格式,下面一行是指定具體的格式
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
#檔案大小到達指定尺寸的時候産生一個新的檔案
log4j.appender.File = org.apache.log4j.RollingFileAppender
#指定輸出目錄
#定義檔案最大大小
log4j.appender.File.MaxFileSize = 10MB
# 輸出是以日志,如果換成DEBUG表示輸出DEBUG以上級别日志
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
3 spring mvc .xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自動掃描該包,使SpringMVC認為包下用了@controller注解的類是控制器 -->
<context:component-scan base-package="car.*" />
<!-- 引入配置檔案 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<!--避免IE執行AJAX時,傳回JSON出現下載下傳檔案 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 啟動SpringMVC的注解功能,完成請求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON轉換器 -->
</list>
</property>
</bean>
<!-- 定義跳轉的檔案的前字尾 ,視圖模式配置-這裡的配置我的了解是自動給後面action的方法return的字元串加上字首和字尾,變成一個 可用的url位址-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置檔案上傳,如果沒有使用檔案上傳可以不用配置,當然如果不配,那麼配置檔案中也不必引入上傳元件包 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 預設編碼 -->
<property name="defaultEncoding" value="utf-8" />
<!-- 檔案大小最大值 -->
<property name="maxUploadSize" value="10485760000" />
<!-- 記憶體中的最大值 -->
<property name="maxInMemorySize" value="40960" />
</bean>
</beans>
4 spring mybatis.xml 他的作用就是就Spring 和Mybatis 整合起來。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自動掃描 -->
<context:component-scan base-package="car.*" />
<!-- 引入配置檔案 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 初始化連接配接大小 -->
<property name="initialSize" value="${initialSize}"></property>
<!-- 連接配接池最大數量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 連接配接池最大空閑 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 連接配接池最小空閑 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 擷取連接配接最大等待時間 -->
<property name="maxWait" value="${maxWait}"></property>
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射檔案 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自動掃描mapping.xml檔案 -->
<property name="mapperLocations" value="classpath:car/mapping/*.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring會自動查找其下的類 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="car.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- (事務管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
5.還有webapp 下面的WEB-INF下面的web.xml 的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Archetype Created Web Application</display-name>
<!-- Spring和mybatis的配置檔案 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
<!-- 編碼過濾器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring監聽器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止Spring記憶體溢出監聽器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
<!-- Spring MVC servlet -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!-- 此處可以可以配置成*.do,對應struts的字尾習慣 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置SESSION逾時,機關是分鐘 -->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
現在就是将ssm架構搭好起來了,接下來我們團隊的工作就是進行建資料庫的工作,我們在我們其中一個成員的電腦建立一個資料庫作為伺服器,我們其他的成員通過使用一個區域網路來通路他的資料庫,進而形成了大家随時修改資料的任何内容都可以及時讓團隊的其他的成員看到資料的變化。我們前期主要是建了六個表,後續還會添加新的表根據我們系統的功能進行相應的擴充。
上面是建表相關的需求文檔,現在隻是截取一小部分
三:現在将整個架構基本建立起來了,資料庫也搭建起來了,現在就是該是團隊奮鬥敲代碼的時間了,其中相關的代碼就是以資料為中心,以jsp頁面為顯示,對資料進行增删改查的操作。整個系統無論做的多麼炫酷(使用了前端的有關的架構和模闆),對于後端而言,都是對資料庫進行相關的增删改查。現在就汽車租賃管理系統使用者的登陸和注冊的操作來證明我們團隊的心得感受。我們直接以相關的代碼來一一講解和說明,廢話不多說,直接上代碼!
1 首先是model 層,Mapping層,Dao層,service層,serviceImp層,controller層來說明。首先是model 層,這個Customer層定義了使用者相關的屬性。
package car.model;
public class Customer {
private Integer id;
private String username;
private String pwd;
private String idcard;
private Integer keyid;
private String name;
private String phone;
private String keyname;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username == null ? null : username.trim();
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd == null ? null : pwd.trim();
}
public String getIdcard() {
return idcard;
}
public void setIdcard(String idcard) {
this.idcard = idcard == null ? null : idcard.trim();
}
public Integer getKeyid() {
return keyid;
}
public void setKeyid(Integer keyid) {
this.keyid = keyid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone == null ? null : phone.trim();
}
public String getKeyname() {
return keyname;
}
public void setKeyname(String keyname) {
this.keyname = keyname == null ? null : keyname.trim();
}
}
2.再次就是dao 層,他定義對Customer這個對象相關的方法,這些可以看出也就是相應的增删改查的操作。這就輔證剛才所陳述的語句。
package car.dao;
import car.model.Customer;
public interface CustomerMapper {
int deleteByPrimaryKey(Integer id);
int insert(Customer record);
int insertSelective(Customer record);
Customer selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(Customer record);
int updateByPrimaryKey(Customer record);
int updateByUserName(Customer record);
Customer selectByName(String username);
}
3就是Mapping層,這個層主要連接配接資料的操作,就是一條條對資料庫的資料進行相關的增删改查的操作的SQL語句,這些SQL語句都是基于Mybatis架構進行相應的封裝而寫出來。 在我們資料庫的知識中,如下代碼我們也可以看得懂是對資料進行增删改查的操作。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="car.dao.CustomerMapper" >
<resultMap id="BaseResultMap" type="car.model.Customer" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="username" property="username" jdbcType="VARCHAR" />
<result column="pwd" property="pwd" jdbcType="VARCHAR" />
<result column="idcard" property="idcard" jdbcType="VARCHAR" />
<result column="keyid" property="keyid" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="phone" property="phone" jdbcType="VARCHAR" />
<result column="keyname" property="keyname" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
id, username, pwd, idcard, keyid, name, phone, keyname
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from customer
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectByName" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
<include refid="Base_Column_List" />
from customer
where username = #{username,jdbcType=VARCHAR}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from customer
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="car.model.Customer" >
insert into customer (id, username, pwd,
idcard, keyid, name,
phone, keyname)
values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{pwd,jdbcType=VARCHAR},
#{idcard,jdbcType=VARCHAR}, #{keyid,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR},
#{phone,jdbcType=VARCHAR}, #{keyname,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="car.model.Customer" >
insert into customer
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="username != null" >
username,
</if>
<if test="pwd != null" >
pwd,
</if>
<if test="idcard != null" >
idcard,
</if>
<if test="keyid != null" >
keyid,
</if>
<if test="name != null" >
name,
</if>
<if test="phone != null" >
phone,
</if>
<if test="keyname != null" >
keyname,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="username != null" >
#{username,jdbcType=VARCHAR},
</if>
<if test="pwd != null" >
#{pwd,jdbcType=VARCHAR},
</if>
<if test="idcard != null" >
#{idcard,jdbcType=VARCHAR},
</if>
<if test="keyid != null" >
#{keyid,jdbcType=INTEGER},
</if>
<if test="name != null" >
#{name,jdbcType=VARCHAR},
</if>
<if test="phone != null" >
#{phone,jdbcType=VARCHAR},
</if>
<if test="keyname != null" >
#{keyname,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="car.model.Customer" >
update customer
<set >
<if test="username != null" >
username = #{username,jdbcType=VARCHAR},
</if>
<if test="pwd != null" >
pwd = #{pwd,jdbcType=VARCHAR},
</if>
<if test="idcard != null" >
idcard = #{idcard,jdbcType=VARCHAR},
</if>
<if test="keyid != null" >
keyid = #{keyid,jdbcType=INTEGER},
</if>
<if test="name != null" >
name = #{name,jdbcType=VARCHAR},
</if>
<if test="phone != null" >
phone = #{phone,jdbcType=VARCHAR},
</if>
<if test="keyname != null" >
keyname = #{keyname,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="car.model.Customer" >
update customer
set username = #{username,jdbcType=VARCHAR},
pwd = #{pwd,jdbcType=VARCHAR},
idcard = #{idcard,jdbcType=VARCHAR},
keyid = #{keyid,jdbcType=INTEGER},
name = #{name,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR},
keyname = #{keyname,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByUserName" parameterType="car.model.Customer" >
update customer
set username = #{username,jdbcType=VARCHAR},
idcard = #{idcard,jdbcType=VARCHAR},
name = #{name,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR}
where username = #{username,jdbcType=VARCHAR}
</update>
</mapper>
4 就是Service 層,service層的主要的作用,就是使用借口interface來定義相關的方法。
package car.service;
import java.util.List;
import car.model.Car;
public interface CarService {
int deleteByPrimaryKey(Integer id);
int insert(Car record);
int insertSelective(Car record);
Car selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(Car record);
int updateByPrimaryKey(Car record);
Car selectByName(String username);
List<Car> getAllCar();
}
5 serviceImp層,主要是對上面service 層的接口方法進行實作,也就是我們常說的接口的實作類,現在通過架構将他封裝了起來罷了。實作service 層,主要使用Springmvc 架構的注解@service h和
@Resource 相關架構的操作。
package car.serviceImp;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import car.dao.CarMapper;
import car.model.Car;
import car.service.CarService;
@Service("carService")
public class CarServiceImp implements CarService {
@Resource
private CarMapper carMapper;
public int deleteByPrimaryKey(Integer id) {
// TODO Auto-generated method stub
return this.carMapper.deleteByPrimaryKey(id);
}
public int insert(Car record) {
// TODO Auto-generated method stub
return this.carMapper.insert(record);
}
public int insertSelective(Car record) {
// TODO Auto-generated method stub
return this.carMapper.insertSelective(record);
}
public Car selectByPrimaryKey(Integer id) {
// TODO Auto-generated method stub
return this.carMapper.selectByPrimaryKey(id);
}
public int updateByPrimaryKeySelective(Car record) {
// TODO Auto-generated method stub
return this.carMapper.updateByPrimaryKeySelective(record);
}
public int updateByPrimaryKey(Car record) {
// TODO Auto-generated method stub
return this.carMapper.updateByPrimaryKey(record);
}
public Car selectByName(String username) {
// TODO Auto-generated method stub
return this.selectByName(username);
}
public List<Car> getAllCar() {
// TODO Auto-generated method stub
return this.carMapper.getAllCar();
}
}
6 接下來就是核心Controller 了,他實作整個系統的業務的邏輯,可以看出最後相關的登陸驗證(相當于對資料進行查詢)和注冊(相當于對資料庫進行添加)以及各種頁面的跳轉都是在Controller進行控制和處理的,Controller相當整個系統的大腦。
package car.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.sun.mail.handlers.message_rfc822;
import car.model.Car;
import car.model.Customer;
import car.model.ReservationOrder;
import car.model.Worker;
import car.service.CarService;
import car.service.CustomerService;
import car.service.ReservationOrderService;
import car.service.WorkerService;
@Controller
@RequestMapping("/front")
public class FrontPlatForm {
@Resource
private CustomerService customerService;
@Resource
private ReservationOrderService reservationOrderService;
@Resource
private WorkerService workerService;
@Resource
private CarService carService;
@RequestMapping("/toindex")
public ModelAndView toindex(){
ModelAndView view = new ModelAndView("frontPlatForm/index");
return view;
}
@RequestMapping("/tologin")
public ModelAndView toLogin(){
ModelAndView view = new ModelAndView("frontPlatForm/login");
return view;
}
@RequestMapping("/login")
public ModelAndView login(HttpServletRequest request){
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
if(username!=null&&pwd!=null){
Customer customer = this.customerService.selectByName(username);
if(customer!=null&&pwd.equals(customer.getPwd())){
HttpSession session = request.getSession();
session.setAttribute("customer", customer);
ModelAndView view = new ModelAndView("frontPlatForm/customerInfo");
return view;
}
}
return new ModelAndView("frontPlatForm/login");
}
@RequestMapping("/doregister")
public String doregister(HttpServletRequest request, Model model) {
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
String pwd1 = request.getParameter("pwd1");
if (username!=""&&pwd!=""&&pwd1!="")
{
if(pwd.equals(pwd1)){
Customer customer = new Customer();
customer.setUsername(username);
customer.setPwd(pwd);
int c = this.customerService.insertSelective(customer);
if (c > 0) {
HttpSession session = request.getSession();
session.setAttribute("customer", customer);
}
}
}
return "frontPlatForm/login";
}
//跳轉到賬号資訊頁面
@RequestMapping("/toCustomerInfo")
public ModelAndView CustomerInfo(){
ModelAndView view = new ModelAndView("frontPlatForm/customerInfo");
return view;
}
//修改使用者資訊
@RequestMapping("/changeInfo")
public ModelAndView changeInfo(HttpServletRequest request){
String username = getCustomer(request).getUsername();
String idcard = request.getParameter("idcard");
String name = request.getParameter("name");
String phone = request.getParameter("phone");
Customer customer = new Customer();
customer.setUsername(username);
customer.setIdcard(idcard);
customer.setName(name);
customer.setPhone(phone);
int update = this.customerService.updateByUserName(customer);
System.out.println(update);
if (update > 0) {
String message = "修改成功";
request.getSession().setAttribute("mes", message);
}
HttpSession session = request.getSession();
session.setAttribute("customer", customer);
ModelAndView view = new ModelAndView("frontPlatForm/customerInfo");
return view;
}
// 密碼修改界面
@RequestMapping("/changePwd")
public ModelAndView changePwd(HttpServletResponse response, HttpServletRequest request, Model model)
throws IOException {
String userName = getCustomer(request).getUsername();
String password = request.getParameter("password");
String oldpass = request.getParameter("oldpass");
Customer customer = new Customer();
customer = this.customerService.selectByName(userName);
if (customer.getPwd().equals(oldpass) && !(oldpass.equals(password))) {
customer.setName(userName);
customer.setPwd(password);
this.customerService.updateByUserName(customer);
ModelAndView view = new ModelAndView("frontPlatForm/login");
return view;
} else if (userName != null && password == null && oldpass == null) {
String message = "";
request.getSession().setAttribute("mes", message);
} else if (password.equals(oldpass)) {
String message = "新密碼不能與原密碼相同";
request.getSession().setAttribute("mes", message);
} else {
String message = "原密碼錯誤";
request.getSession().setAttribute("mes", message);
}
return new ModelAndView("frontPlatForm/password");
}
//跳轉到預訂頁面
@RequestMapping("/reservation")
public ModelAndView reservation(HttpServletRequest request){
String downPayment = request.getParameter("downPayment");
String rentDate = request.getParameter("rentDate");
String shReturnDate = request.getParameter("shReturnDate");
String carId = request.getParameter("carId");
if(downPayment!=null&&rentDate!=null&&shReturnDate!=null&&carId!=null){
int customerId = getCustomer(request).getId();
ReservationOrder reOrder = new ReservationOrder();
reOrder.setDownPayment(Float.valueOf(downPayment));
reOrder.setRentDate(rentDate);
reOrder.setShreturnDate(shReturnDate);
reOrder.setCarId(Integer.valueOf(carId));
reOrder.setCustomerId(customerId);
int insert = this.reservationOrderService.insertSelective(reOrder);
}
ModelAndView view = new ModelAndView("frontPlatForm/reservation");
return view;
}
//顯示訂單資訊頁面
@RequestMapping("/reservationInfo")
public ModelAndView reservationInfo(HttpServletRequest request,Model model){
List<ReservationOrder> reOrder = this.reservationOrderService.selectByCustomerId(getCustomer(request).getId());
ModelAndView view = new ModelAndView("frontPlatForm/reservationInfo");
model.addAttribute("reOrder", reOrder);
return view;
}
// 查找客戶資訊
public Customer getCustomer(HttpServletRequest request) {
HttpSession session = request.getSession();
String name = ((Customer) session.getAttribute("customer")).getUsername();
Customer customer = customerService.selectByName(name);
return customer;
}
//職工資訊管理頁面
@RequestMapping("/workerManage")
public ModelAndView getWorker(HttpServletRequest request,Model model){
List<Worker> worker = workerService.selectAll();
model.addAttribute("worker", worker);
ModelAndView view = new ModelAndView("administrator/workerInfo");
return view;
}
//添加職工資訊
@RequestMapping("/addWorker")
public void addWorker(HttpServletRequest request,Model model,HttpServletResponse response) throws IOException{
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
String idcard = request.getParameter("idcard");
String keyname = request.getParameter("keyname");
String keyid = request.getParameter("keyid");
if(name!=null&&idcard!=null&&keyname!=null&&keyid!=null){
Worker worker1 = new Worker();
worker1.setName(name);
worker1.setPwd(pwd);
worker1.setIdcard(idcard);
worker1.setKeyname(keyname);
worker1.setKeyid(Integer.valueOf(keyid));
int insert = workerService.insert(worker1);
if(insert>0){
List<Worker> worker = workerService.selectAll();
model.addAttribute("worker", worker);
response.sendRedirect("workerManage");
}
}
}
//删除職工資訊
@RequestMapping("/deleteWorker")
public ModelAndView deleteWorker(HttpServletRequest request, Model model) {
String items = request.getParameter("delitems");
List<String> delList = new ArrayList<String>();
String[] strs = items.split(",");
for (String str : strs) {
delList.add(str);
}
this.workerService.batchDeletes(delList);
List<Worker> worker = this.workerService.selectAll();
model.addAttribute("worker", worker);
ModelAndView view = new ModelAndView("administrator/workerInfo");
return view;
}
//根據編号查找職工
@RequestMapping(value="/selectWorker", method = RequestMethod.POST)
@ResponseBody
public Worker selectWorker(HttpServletRequest request,Model model){
String id = request.getParameter("id");
Worker worker = new Worker();
if(id!=null){
worker = this.workerService.selectByPrimaryKey(Integer.valueOf(id));
}
HttpSession session = request.getSession();
session.setAttribute("worker1",worker);
model.addAttribute("worker1", worker);
return worker;
}
//修改職工資訊
@RequestMapping("/updateWorker")
public void updateWorker(HttpServletResponse response,HttpServletRequest request,Model model) throws IOException{
String id = request.getParameter("id");
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
String idcard = request.getParameter("idcard");
String keyname = request.getParameter("keyname");
String keyid = request.getParameter("keyid");
int update = 0;
if(id!=null&&name!=null&&idcard!=null&&keyname!=null&&keyid!=null){
Worker worker1 = new Worker();
worker1.setId(Integer.valueOf(id));
worker1.setName(name);
worker1.setPwd(pwd);
worker1.setIdcard(idcard);
worker1.setKeyname(keyname);
worker1.setKeyid(Integer.valueOf(keyid));
update = workerService.updateByPrimaryKey(worker1);
}
if(update>0){
List<Worker> worker = this.workerService.selectAll();
model.addAttribute("worker", worker);
response.sendRedirect("workerManage");
}
}
//管理者顯示訂單資訊頁面
@RequestMapping("/adminReInfo")
public ModelAndView adminReInfo(HttpServletRequest request,Model model){
List<ReservationOrder> reOrder = this.reservationOrderService.selectAll();
ModelAndView view = new ModelAndView("administrator/reservationInfo");
model.addAttribute("reOrder", reOrder);
return view;
}
//管理者顯示車輛資訊
@RequestMapping("/adminCarInfo")
public ModelAndView adminCarInfo(HttpServletRequest request,Model model){
List<Car> car = this.carService.getAllCar();
ModelAndView view = new ModelAndView("administrator/carInfo");
model.addAttribute("car", car);
return view;
}
}
綜上所述,相信老師對于我們所用的這個SSM 架構有了大緻的了解吧,我現在是拿登陸注冊來進行說明,那麼其他後續汽車的管理也是運用上面的六個Controller層進行相關的增删改查。我們現在給出我們這個項目大緻代碼目錄的截圖(d當然這些代碼也會逐漸增加,現在功能還沒有完善到最後)
四:代碼上了一大堆,那麼現在來看看我們實作登陸注冊以及背景汽車管理的真實網頁上截圖。
1 現在customer 表有如下的資料
2 我們現在來看看我們的登陸頁面 ,我們現在使用[email protected]的賬号進行登陸
3 登陸進去的頁面如下,進入到顧客的賬号就可以對自己的資訊進行相應的添加修改,浏覽檢視車輛的資訊以及進行預訂車輛的相關操作,都可以進行。
4 比如我們對自己的資訊進行修改和添加
6 可以看見資料庫的資料已經更新了
7我們在看看修改密碼的功能 将密碼修改為123456
8 資料庫密碼已經更新了
9 檢視汽車的資訊
10 點選檢視詳情連接配接可以進入汽車的詳情頁
11 相關的汽車詳情頁 現在還沒有完全做好,到時候這個頁面将提供汽車的詳細的資訊和相關的圖檔展示 以及汽車的預定頁都将在這個頁面實作
12 再說說另外一個在本系統權限的 最大的管理者的頁面
13 管理并沒有注冊的功能,他隻有注冊的功能,管理者的資料是直接操作資料庫背景直接插進去,系統隻是提供一個登陸的功能給他而已。意思是并不是每個人都可以注冊成為系統的管理者
管理者可以對汽車管理和相關的職工的資訊進行修改添加的增删改查的操作
最後的吧,相關的頁面展示到答辯再一一進行回答,相關的工作拖得有點晚,才導緻這篇部落格拖得這麼晚,不過我想既然決定寫好,就要将它寫的比較好,起碼對的起老師花費寶貴的時間來為我們進行審閱
五:團隊相關的心得體會
通過了這個汽車項目的鍛煉,我們大家都一緻的認為在軟體的開發的過程中,團隊協作以及精誠合作真的很重要,一個人的力量是非常有限的,但是團隊團結起來的力量是強大的。團隊合作是我們開發一個項目的不二之選。
其次,開發的過程我們遇到很多的困難,我們通過互相學習以及利用身邊的書籍網上豐富的資源,解決掉項目不少的問題,通過我們同學之間的交流,思路的不同可以讓我們收獲頗多。善于學習别人的長處和利用身邊的網上的資源,對于我們提高我們的學習能力以及我們自身的技術真的有很大的幫助。其次,開發項目要選擇合适架構以及好的良好的碼雲等相關輔助相關的工具。對于我們開發的速度有很好的促進的作用,總而言之,我們團隊需要通過這個汽車的租賃的項目,來自學吃透SSM架構的相關的内容,這應該就是我們做這個項目的最大的收獲,最後的,加強我們團隊整體的前端的頁面的美化,雖然說自己做的頁面自己都不想看,更何況是使用者尼,是以學習的道路永遠都是在路上,并沒有所謂的終點。
謝謝各位花費寶貴的時間欣賞完本文,謝謝!