图书商城系统
Java 實訓項目–Estore圖書商城
1、 項目介紹
1.1 課程名稱:
Estore圖書商城
1.2 課程持續時間:
三周
1.3 課程實施方式:
項目驅動式教學
1.4 課程實施對象:
計算機相關專業在校學生
1.5 實訓意義與目標:
? 體驗真實項目開發
通過商業化項目的開發和演示,讓學生真正理解軟件開發思想,了解行業主流的開發技術,了解從需求、設計、編碼、測試等軟件開發流程,使用企業中常用的開發工具,增強解決處理問題的能力,積累專業的開發經驗。
? 理解專業技術標準
按照軟件工程的要求,通過實際的行業項目,真正理解學校學習的開發語言,并提高編碼的熟練度,熟悉各種項目開發文檔和表格的撰寫。
? 鍛煉團隊合作
培養學生高度的敬業精神、團隊合作意識、協作能力,迅速有效縮短個人與企業實際需求的差距,從而實現從學校到企業的順利過渡。
1.6 項目功能介紹:
這是一個購物類圖書商城網站,面向所有網民開放,主要提供商品在線銷售服務
? 服務端 使用服務器進行商品的發布
? 客戶端 將服務器端發布的商品進行展示,供用戶購買
2、 實訓項目實施方案
項目任務分解:
實訓任務1:掌握java基礎
? Tomcat的下載、安裝以及使用
? JSP頁面的編寫與使用
? Servlet的使用與練習
? MySql數據庫的安裝、基本操作
? JDBC操作數據庫
效果圖如下:
圖2-1-1 Navicat forMySql查看數據表
實訓任務2: 掌握SSM框架
? SSM框架的使用
? SSM框架整合使用
圖2-1-2 SSM框架整合
實訓任務3: 圖書商城服務端編寫
對客戶端顯示的商品進行管理
圖2-1-3商品發布頁面
實訓任務4:網站客戶端編寫
對服務端發布的商品進行展示
圖2-1-4 圖書商城首頁
3、 實訓項目軟硬件環境配置要求
硬件環境配置
? 電腦一套(CPU主頻不小于1.5GHz,內存空間不小于4GB,硬盤存儲空間不小于60GB)
? 實訓場地(30平米以上)
? 投影儀(如:明基,小帥影院,NEC,奧圖碼)
軟件環境配置
? Eclipse安裝包
? Tomcat安裝包(可以用綠色版或安裝版,版本不小于7.0)
? My SQL數據庫服務安裝包(版本不小于5.0)
? JDK安裝包(版本不小于1.7)
? 瀏覽器(火狐或Google)
首先配置web.xml文件,如果創建的是maven程序不需要這一步
<?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_2_5.xsd" id="WebApp_ID" version="2.5"><display-name>bookshopping</display-name><welcome-file-list><welcome-file>/</welcome-file></welcome-file-list><!-- servletContext:代表整個web應用 --><!-- spring監聽器 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/applicationContext-*.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- springmvc前端控制器 --><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/springmvc.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><!-- springmvc對哪些請求解析*.action 解析所有的以.action結尾的請求/ 解析所有的請求,除了jsp,需要在springmvc配置文件中定義靜態資源的訪問--><url-pattern>/</url-pattern></servlet-mapping><!-- 驗證碼servlet --><servlet><servlet-name>checkImg</servlet-name><servlet-class>com.liuliuliu.utils.CheckImgServlet</servlet-class></servlet><servlet-mapping><servlet-name>checkImg</servlet-name><url-pattern>/checkImg</url-pattern></servlet-mapping><!-- 字符編碼的過濾器 --><filter><filter-name>encoding</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param><init-param><param-name>forceEncoding</param-name><param-value>TRUE</param-value></init-param></filter><filter-mapping><filter-name>encoding</filter-name><!-- 攔截所有的請求 --><url-pattern>/*</url-pattern></filter-mapping><!-- 自動登錄的過濾器 --><filter><filter-name>autoLogin</filter-name><filter-class>com.feicui.filter.AutoLoginFilter</filter-class></filter><filter-mapping><filter-name>autoLogin</filter-name><!-- 配置對哪些請求攔截 --><url-pattern>/*</url-pattern></filter-mapping></web-app>在寫java web程序時jsp頁面都已經給出,這里就不多加敘述了
接下來配置好springmvc文件
<?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: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-4.3.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"><!-- 對于靜態資源的訪問 --><mvc:resources location="/css/" mapping="/css/**"></mvc:resources><mvc:resources location="/images/" mapping="/images/**"></mvc:resources><mvc:resources location="/js/" mapping="/js/**"></mvc:resources><mvc:resources location="/fonts/" mapping="/fonts/**"></mvc:resources><mvc:resources location="/img/" mapping="/img/**"></mvc:resources><mvc:resources location="/plugins/" mapping="/plugins/**"></mvc:resources><!-- 處理器適配器,處理器映射器 --><mvc:annotation-driven></mvc:annotation-driven><context:component-scan base-package="com.liuliuliu.controller"></context:component-scan><!-- 視圖解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/"></property><property name="suffix" value=".jsp"></property></bean><!-- 上傳組件 --><bean name="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!-- 設置上傳文件的大小5M --><property name="maxUploadSize" value="5242880"></property></bean></beans>現在開始寫代碼部分
首先根據數據庫建立好每個對象的模型,也就是model層
這里使用用戶對象做說明
package com.liuliuliu.model;
/**
* 實體類:
* 1.屬性私有化,提供公共的get和set方法
* 2.必須要有無參構造
* 3.屬性類型應該為包裝類類型
* 4.類不能使用final修飾
*
* final,finally,finalize
*
*/
public class Users {
private Integer id;
private String username;
private String password;
private String nickname;
private String email;
private String role;
private Integer state;
private String activecode;
private String updatetime;
}
接下來在dao層書寫方法的接口,還是用user作實例
public interface UsersMapper {/*** 保存方法* @param user 保存用戶對象* @return 如果返回1,保存成功,否則,返回0*/public int saveUsers(Users user);/*** 刪除用戶方法* @param id 要刪除的用戶的id* @return 如果返回1,刪除成功,否則,返回0*/public int delUsers(int id);/*** 修改的方法* @param user 要修改的對象* @return 如果返回1,修改成功,否則,返回0*/public int updateUsers(Users user);/*** 根據id查詢用戶* @param id 要查詢的用戶的id* @return 查詢的用戶對象*/public Users findUserById(int id);/*** 查詢所有的用戶* @return 所用用戶組成的集合*/public List<Users> findUsersList();/*** 用戶登錄方法* @param user 對象中存放用戶輸入的用戶名和密碼* @return 登錄成功,返回users對象,否則,返回null*/public Users login(Users user);public Users findUserByCode(String code);public Users findUserByName(String username);}在service層和serviceimpl層實現
public interface UsersService {/*** 保存方法* @param user 保存用戶對象* @return 如果返回1,保存成功,否則,返回0*/public int saveUsers(Users user);/*** 刪除用戶方法* @param id 要刪除的用戶的id* @return 如果返回1,刪除成功,否則,返回0*/public int delUsers(int id);/*** 修改的方法* @param user 要修改的對象* @return 如果返回1,修改成功,否則,返回0*/public int updateUsers(Users user);/*** 根據id查詢用戶* @param id 要查詢的用戶的id* @return 查詢的用戶對象*/public Users findUserById(int id);/*** 查詢所有的用戶* @return 所用用戶組成的集合*/public List<Users> findUsersList();/*** 用戶登錄方法* @param user 對象中存放用戶輸入的用戶名和密碼* @return 登錄成功,返回users對象,否則,返回null*/public Users login(Users user);/*** 根據激活碼查詢用戶* @param code* @return*/public Users findUserByCode(String code);/*** 根據用戶名查詢對象* @param username 查詢的用戶名* @return 查詢的對象*/public Users findUserByName(String username); } @Service public class UsersServiceImpl implements UsersService {@Autowiredprivate UsersMapper usersMapper;@Overridepublic int saveUsers(Users user) {int num = usersMapper.saveUsers(user);//發送給用戶激活郵件try {MailUtils.sendMail(user.getEmail(), user.getActivecode());} catch (Exception e) {e.printStackTrace();}return num;}@Overridepublic int delUsers(int id) {return usersMapper.delUsers(id);}@Overridepublic int updateUsers(Users user) {return usersMapper.updateUsers(user);}@Overridepublic Users findUserById(int id) {return usersMapper.findUserById(id);}@Overridepublic List<Users> findUsersList() {return usersMapper.findUsersList();}@Overridepublic Users login(Users user) {return usersMapper.login(user);}@Overridepublic Users findUserByCode(String code) {return usersMapper.findUserByCode(code);}@Overridepublic Users findUserByName(String username) {return usersMapper.findUserByName(username);}}用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="com.feicui.dao.UsersMapper"><!-- 插入 --><insert id="saveUsers" parameterType="com.feicui.model.Users">insert into users(username,password,nickname,email,role,state,activecode,updatetime)values(#{username},#{password},#{nickname},#{email},#{role},#{state},#{activecode},#{updatetime})</insert><!-- 刪除 --><delete id="delUsers" parameterType="int">delete from users where id = #{id}</delete><!-- 修改 --><update id="updateUsers" parameterType="com.feicui.model.Users">update users set username = #{username},password = #{password},nickname = #{nickname},email = #{email},role = #{role},state = #{state}where id = #{id}</update><!-- 根據id查詢 --><select id="findUserById" parameterType="int" resultType="com.feicui.model.Users">select * from users where id = #{id}</select><!-- 查詢所有 --><select id="findUsersList" resultType="com.feicui.model.Users">select * from users</select><!-- 登錄 --><select id="login" parameterType="com.feicui.model.Users" resultType="com.feicui.model.Users">select * from users where username = #{username} and password = #{password}</select><!-- 根據激活碼查詢用戶 --><select id="findUserByCode" parameterType="string" resultType="com.feicui.model.Users">select * from users where activecode = #{activecode}</select><!-- 根據用戶名查詢對象 --><select id="findUserByName" parameterType="string" resultType="com.feicui.model.Users">select * from users where username = #{name}</select> </mapper>不要忘了配置數據庫和事物管理器,在service中分別配置
<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"><!-- 引入外部的db.properties文件 --><context:property-placeholder location="classpath:db.properties"/><!-- 數據庫連接池 --><bean name = "dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><!-- 數據庫連接信息 --><property name="driverClass" value="${jdbc.driver}"></property><property name="jdbcUrl" value="${jdbc.url}"></property><property name="user" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean><!-- sqlsessionFactory --><bean name = "sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="mapperLocations" value="classpath:mybatis/*.xml"></property></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.feicui.dao"></property></bean> </beans> <?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:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"><!-- 掃描service中的注解 --><context:component-scan base-package="com.liuliuliu.service"></context:component-scan><!-- 事務管理 --><!-- 事務管理器 --><bean name = "transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!-- 通知 --><tx:advice transaction-manager="transactionManager" id = "txAdvice"><tx:attributes><!-- propagation:事務的傳播行為 --><tx:method name="save*" propagation="REQUIRED" read-only="false"/><tx:method name="update*" propagation="REQUIRED" read-only="false"/><tx:method name="del*" propagation="REQUIRED" read-only="false"/><tx:method name="get*" propagation="SUPPORTS" read-only="true"/><tx:method name="add*" propagation="REQUIRED" read-only="false"/><tx:method name="edit*" propagation="REQUIRED" read-only="false"/><tx:method name="remove*" propagation="REQUIRED" read-only="false"/><tx:method name="find*" propagation="SUPPORTS" read-only="true"/></tx:attributes></tx:advice><aop:config><aop:pointcut expression="execution(* com.liuliuliu.service.impl.*.*(..))" id="pc"/><aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/></aop:config></beans>注:在鏈接數據庫時引入了一個db.properties文件
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/bookshopping?useUnicode=true&characterEncoding=UTF-8 jdbc.username=root jdbc.password=****根據自己的需要做適當修改
接下來開始寫最重要的controller部分,還是用user作實例,其他的不多加贅述
@Controller public class UsersController {@Autowiredprivate UsersService userService;@RequestMapping("showReg")public String showReg(String type,HttpServletRequest request){if(type != null && type.equals("1")){request.setAttribute("msg", "兩次密碼不一致");}else if(type != null && type.equals("2")){request.setAttribute("msg", "驗證碼錯誤");}else if(type != null && type.equals("3")){request.setAttribute("msg", "注冊失敗");}return "reg";}@RequestMapping("reg")public String reg(Users user,String repassword,String checkcode,HttpServletRequest request){user.setRole("user");user.setUpdatetime(DateUtils.format(new Date()));user.setState(0);//0:表示沒有激活,1激活//將密碼與確認密碼進行比較String password = user.getPassword();if(!password.equals(repassword)){return "redirect:showReg.action?type=1";}//使用md5對密碼加密user.setPassword(Md5Utils.md5(password));//生成一個激活碼String uuid = UUIDUtils.getUUID();user.setActivecode(uuid);//驗證驗證碼是否正確//從session中獲取生成驗證碼//String checkcode_session = (String)request.getSession().getAttribute("checkcode_session");//拿用戶輸入的驗證碼與生成的驗證碼比較 // if(!checkcode.equals(checkcode_session)){ // return "redirect:showReg.action?type=2"; // }//調用service進行保存int num = userService.saveUsers(user);if(num == 0){return "redirect:showReg.action?type=3";}return "redirect:showLogin.action?type=1";}@RequestMapping("showLogin")public String showLogin(String type,Model model){if("1".equals(type)){model.addAttribute("msg", "注冊成功,請登錄");}else if("2".equals(type)){model.addAttribute("msg","用戶名或密碼錯誤");}return "login";}//激活賬戶的方法@RequestMapping("activation")public String activation(String code){//根據激活碼查詢用戶Users user = userService.findUserByCode(code);//將賬戶的狀態改為1user.setState(1);;userService.updateUsers(user);return "login";}//異步校驗用戶名@RequestMapping("checkName")@ResponseBodypublic String checkName(String username){System.out.println(username);//根據用戶名查詢是否存在該用戶名Users user = userService.findUserByName(username);//當對象不為null,說明用戶名存在if(user != null){//返回false表示用戶名存在//{"msg":"false"}return "{\"msg\":\"false\"}";}return "{\"msg\":\"true\"}";}//登陸@RequestMapping("login")public String login(Users user,String remember,String autologin,HttpServletResponse resp,HttpServletRequest request){Users users = null; try {user.setPassword(Md5Utils.md5(user.getPassword()));users = userService.login(user);if(users == null){return "redirect:showLogin?type=2";}//記住用戶//判斷用戶是否勾選記住用戶if(remember != null && remember.equals("on")){//將用戶名以cookie的形式發送到客戶端Cookie cookie;cookie = new Cookie("username", URLEncoder.encode(users.getUsername(), "utf-8"));//cookie默認是會話級別的,會隨著瀏覽器的關閉,cookie消失//通過setMaxAge設置cookie的存活時間,單位秒cookie.setMaxAge(60 * 60 * 24 * 7);//存儲7天 resp.addCookie(cookie);Cookie cookie1 = new Cookie("save", "on");cookie1.setMaxAge(60 * 60 * 24 * 7);//存儲7天 resp.addCookie(cookie1);}else{//將用戶名以cookie的形式發送到客戶端Cookie cookie = new Cookie("username", "");//cookie默認是會話級別的,會隨著瀏覽器的關閉,cookie消失//通過setMaxAge設置cookie的存活時間,單位秒cookie.setMaxAge(0);//存儲7天 resp.addCookie(cookie);Cookie cookie1 = new Cookie("save", "");cookie.setMaxAge(0);//存儲7天 resp.addCookie(cookie1);}//自動登錄if(autologin != null && autologin.equals("on")){Cookie cookie = new Cookie("autoLogin", URLEncoder.encode(users.getUsername(), "utf-8") + "-" + users.getPassword());cookie.setMaxAge(60 * 60 * 24 * 7);resp.addCookie(cookie);}//將登陸的用戶存放到session中request.getSession().setAttribute("user", users);} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}//判斷登陸用戶的角色//如果是admin,跳轉到后臺管理主頁面if(users.getRole().equals("admin")){return "redirect:showAdminIndex";}return "redirect:showIndex";}//退出登錄@RequestMapping("logout")public String logout(HttpServletRequest request,HttpServletResponse resp){HttpSession session = request.getSession();//刪除session中存放的登錄對象//session.removeAttribute("user");//銷毀sessionsession.invalidate();//去除自動登陸的功能//將自動登陸cookie中的信息清空Cookie cookie = new Cookie("autoLogin", "");cookie.setMaxAge(0);resp.addCookie(cookie);return "redirect:showIndex";} }這里用到許多工具類,網上都能查到,就不寫上了。
注意:代碼中含有@符號的注解都不能刪掉,具體作用可以上網查。
總結
- 上一篇: win7双系统安装
- 下一篇: 2021新版彩虹易支付系统源码/运营版/