赞
踩
描述:
顾客:登录,新增订单,查询所有商品
管理员:查询所有商品,修改商品信息,新增商品,删除商品。
效果展示:
登录界面:
用户登录界面:
购买界面
管理员登录:
新增商品
以及修改商品
一、目录结构:
二、配置文件
applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 指定需要扫描的包,注解生效 --> <context:component-scan base-package="com.dao" /> <context:component-scan base-package="com.controller" /> <context:component-scan base-package="com.service" /> <!-- 扫描数据池 --> <context:property-placeholder location="classpath:jdbc_mysql.properties" ignore-resource-not-found="false" local-override="false" /> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> <!-- Mysql数据库驱动 --> <property name="driverClassName" value="${jdbc.driver}" /> <!-- URL --> <property name="url" value="${jdbc.url}" /> <!-- Username --> <property name="username" value="${jdbc.username}" /> <!-- password --> <property name="password" value="${jdbc.password}" /> <!-- 最大链接数 --> <property name="maxTotal" value="30" /> <!-- 最大空闲链接数 --> <property name="maxIdle" value="10" /> <!-- 初始化连接数 --> <property name="initialSize" value="5" /> </bean> <!-- 为数据源添加事物管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 开启事物注解 --> <tx:annotation-driven transaction-manager="txManager"/> <!-- 配置Mybatis工厂,同时指定数据源,并与Mybatis完美整合 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!--configLocation 的属性值为 MyBatis 的核心配置文件 --> <property name="configLocation" value="classpath:com/mybatis/mybatis-config.xml" /> </bean> <!-- Mapper 代理开发,使用 Spring 自动扫描 MyBatis的接口并装配 (Spring 将指定包中所有被@Mapper 注解标注的接口自动装配为 MyBatis 的映射接口 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- mybatis-spring组件的扫描器 --> <property name="basePackage" value="com.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <context:component-scan base-package="com.service"></context:component-scan> </beans>
数据库连接文件
jdbc_mysql.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localHost:3306/test?characterEncoding=utf8&serverTimezone=UTC
jdbc.username=root
jdbc.password=129869
日志文件
log4j.properties
### Global logging configuration
log4j.rootLogger=ERROR, stdout
### Uncomment for MyBatis logging
log4j.logger.org.apache.ibatis=ERROR
### Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
springmvc-servlet.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 使用扫描机制扫描控制器类,控制器类都在 controller 包及其子包下 --> <context:component-scan base-package="com.controller" /> <mvc:annotation-driven /> <!-- annotation-driven 用于简化开发的配置, 注解 DefaultAnnotationHandlerMapping AnnotationMethodHandlerAdapter --> <!-- 允许css目录下的所有文件可见 --> <mvc:resources location="/css/" mapping="/css/**" /> <!-- html --> <mvc:resources location="/html/" mapping="/html/**" /> <mvc:resources location="/jsp/" mapping="/jsp/**" /> <mvc:resources location="/js/" mapping="/js/**" /> <mvc:resources location="/images/" mapping="/images/**" /> <mvc:resources location="/fonts/" mapping="/fonts/**" /> <!-- 配置视图解析器ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/WEB-INF/jsp/" /> <!-- 后缀 --> <property name="suffix" value=".jsp"></property> </bean> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <display-name>ChooseEachother</display-name> <welcome-file-list> <welcome-file>Login.html</welcome-file> <welcome-file>Login.htm</welcome-file> <welcome-file>Login.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 实例化容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 部署DispatcherServlet --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 表示容器在启动时立即加载servlet --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- 处理所有url --> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 避免乱码 --> <filter> <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
三、控制类
对于这个系统有两个表,也有两个控制类
GoodsController.java
package com.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.po.Goods; import com.service.GoodsService; @Controller("goodsController") @RequestMapping("/goods") public class GoodsController { @Autowired private GoodsService goodsService; //管理员登录 @RequestMapping("/adminlogin") public String AdminLogin(Model model) { return "manageAllGoods"; } // 顾客登录 @RequestMapping("/login") public String Login(String account, String password, Model model) { if ("jyq".equals(account) && "123456".equals(password)||"rezheaiba".equals(account) && "123456".equals(password)) { model.addAttribute("account", account); model.addAttribute("password", password); return "redirect:/goods/selectAllGoods"; }else { model.addAttribute("massage", "用户名或密码错误!您可以先浏览以下商品!"); return "selectAllGoods"; } } // 显示所有商品,顾客用 @RequestMapping("/selectAllGoods") public String selectAllGoods(Model model) { List<Goods> allGoods = goodsService.selectAllGoods(); model.addAttribute("allGoods", allGoods); return "selectAllGoods"; } //管理所有商品,管理员用 @RequestMapping("/manageAllGoods") public String manageAllGoods(Model model) { List<Goods> allGoods = goodsService.selectAllGoods(); model.addAttribute("allGoods", allGoods); return "manageAllGoods"; } //管理员删除商品 @RequestMapping("/deleteGoods") public String deleteGoods(int goodsId) { goodsService.deleteGoods(goodsId); return "redirect:/goods/manageAllGoods"; } //管理员添加商品信息 @RequestMapping("/insertGoods") public String insertGoods(String goodsname,String goodsprice) { Goods goods = new Goods(goodsname,Double.parseDouble(goodsprice)); goodsService.insertGoods(goods); return "redirect:/goods/manageAllGoods"; } //跳转到修改商品信息页面 @RequestMapping("/infoGoods") public String infoGoods(int goodsId,Model model) { model.addAttribute("gid", goodsId); return "updateGoods"; } //管理员修改商品 @RequestMapping("/updateGoods") public String updateGoods(String gid,String gsname,String price ) { Goods goods = new Goods(gsname,Double.parseDouble(price)); goods.setGoodsId(Integer.parseInt(gid)); goodsService.updateGoods(goods); return "redirect:/goods/manageAllGoods"; } }
OrdersController.java
package com.controller; import java.util.Date; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.po.Orders; import com.service.OrdersService; @Controller("ordersController") @RequestMapping("/orders") public class OrdersController { @Autowired private OrdersService ordersService; //跳转至购买页面,去填写个人信息 @RequestMapping("/buyGoods") public String buyGoods(String gname, Model model) { model.addAttribute("gname", gname); return "BuyGoods"; } @RequestMapping("/selectAllOrders") public String selectAllOrders(Model model) { List<Orders> orders = ordersService.selectAllOrders(); model.addAttribute("myorders", orders); return "myOrders"; } //insert订单 @RequestMapping("/myOrders") public String myOrders(String gname,String inputName,String inputPhoneNumber,String inputAddress) { int max=10000,min=1; int ordernumber = (int) (Math.random()*(max-min)+min); Orders orders = new Orders(inputName,Integer.parseInt(inputPhoneNumber),gname,inputAddress,ordernumber,new Date()); ordersService.insertOrders(orders); return "redirect:/orders/selectAllOrders"; } }
其他文件懒得粘贴了,有需要的可自行获取
https://download.csdn.net/download/weixin_45095712/12598411
https://github.com/rezheaiba/rzab
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。