【SpringMVC整合MyBatis】商品修改功能分析

结合之前我们搭建好的环境,我们下面来编写商品修改的功能。

商品修改功能开发
1.需求
操作流程:
(1)进入商品查询列表页面
(2)点击修改,进入商品修改页面,页面中显示了要修改的商品(从数据库查询)
要修改的商品从数据库查询,根据商品id(主键)查询商品信息

(3)在商品修改页面,修改商品信息,修改后,点击提交

2.开发mapper
mapper:
根据id查询商品信息
根据id更新Items表的数据
不用开发了,使用逆向工程生成的代码。

3.开发service
接口功能:
根据id查询商品信息
修改商品信息

接口ItemsService
package cn.edu.hpu.ssm.service;

import java.util.List;

import cn.edu.hpu.ssm.po.ItemsCustom;
import cn.edu.hpu.ssm.po.ItemsQueryVo;

//商品管理service
public interface ItemsService {

	//商品查询列表
	public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception;

	//根据id查询商品信息
	public ItemsCustom findItemsById(Integer id)throws Exception;
	
	//修改商品信息
	public void updateItems(Integer id,ItemsCustom itemsCustom)throws Exception;

}

接口的实现:
package cn.edu.hpu.ssm.service.impl;


import java.util.List;


import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;


import cn.edu.hpu.ssm.mapper.ItemsMapper;
import cn.edu.hpu.ssm.mapper.ItemsMapperCustom;
import cn.edu.hpu.ssm.po.Items;
import cn.edu.hpu.ssm.po.ItemsCustom;
import cn.edu.hpu.ssm.po.ItemsQueryVo;
import cn.edu.hpu.ssm.service.ItemsService;


//商品管理
public class ItemsServiceImpl implements ItemsService{


	@Autowired
	private ItemsMapperCustom itemsMapperCustom;
	
	@Autowired
	private ItemsMapper itemsMapper; 
	
	@Override
	public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)
			throws Exception {
		//通过ItemsMapperCustom查询数据库
		return itemsMapperCustom.findItemsList(itemsQueryVo);
	}


	@Override
	public ItemsCustom findItemsById(Integer id) throws Exception {
		
		Items items=itemsMapper.selectByPrimaryKey(id);
		//中间对商品信息进行业务处理
		//...
		//最终返回ItemsCustom
		ItemsCustom itemsCustom=new ItemsCustom();
		//将item的内容拷贝到itemsCustom
		BeanUtils.copyProperties(items, itemsCustom);
		
		return itemsCustom;
	}


	@Override
	public void updateItems(Integer id, ItemsCustom itemsCustom) throws Exception {
		//添加业务校验,通常在Service接口对关键参数进行校验
		//校验id是否为空,如果为空抛出异常
		
		
	}
	
}

4.开发controller
方法:
商品信息修改页面显示
商品信息修改提交

package cn.edu.hpu.ssm.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.hpu.ssm.po.ItemsCustom;
import cn.edu.hpu.ssm.service.ItemsService;

//商品的Controller
@Controller
public class ItemsController {


	@Autowired
	private ItemsService itemsService;
	
	//商品查询列表
	//@RequestMapping实现 对queryItems方法和url进行映射,一个方法对应一个url
	//一般建议将url和方法写成一样
	@RequestMapping("/queryItems")
	public ModelAndView queryItems()throws Exception{
		
		//调用Service查找数据库,查询商品列表,这里使用静态数据模拟
		List<ItemsCustom> itemsList=itemsService.findItemsList(null);
		
		//返回ModelAndView
		ModelAndView modelAndView=new ModelAndView();
		//相当于request的setAttribut,在jsp页面中通过这个来取数据
		modelAndView.addObject("itemsList",itemsList);
		
		//指定视图
		//下边的路径,如果在视图解析器中配置jsp的路径前缀和后缀,修改为items/itemsList
		//modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp")
		//下边的路径配置就可以不在程序中指定jsp路径的前缀和后缀
		modelAndView.setViewName("items/itemsList");


		return modelAndView;
	}
	
	//商品信息修改页面显示
	@RequestMapping("/editItems")
	public ModelAndView editItems()throws Exception{
		
		//调用service根据商品id查询商品信息
		ItemsCustom itemsCustom=itemsService.findItemsById(1);
		
		//返回ModelAndView
		ModelAndView modelAndView=new ModelAndView();
		
		//将商品信息放到model
		modelAndView.addObject("itemsCustom",itemsCustom);
		
		//返回商品修改页面
		modelAndView.setViewName("items/editItems");
		
		return modelAndView;
		
	}
	
	//商品信息修改提交
	@RequestMapping("/editItemsSubmit")
	public ModelAndView editItemsSubmit()throws Exception{
		
		//调用service更新商品信息,页面需要将商品信息传到此方法
		//......没有讲参数绑定,暂时先放在这
		
		//返回ModelAndView
		ModelAndView modelAndView=new ModelAndView();
		
		//返回一个成功页面
		modelAndView.setViewName("success");
		
		return modelAndView;
	}
	
}

jsp文件夹下创建一个success.jsp界面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'success.jsp' starting page</title>


  </head>
  
  <body>
    操作成功! <br>
  </body>
</html>

jsp/items文件夹下创建editItems.jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>修改商品信息</title>


</head>
<body> 


<form id="itemForm" action="${pageContext.request.contextPath }/editItemsSubmit.action" method="post" >
<input type="hidden" name="id" value="${itemsCustom.id }"/>
修改商品信息:
<table width="100%" border=1>
<tr>
	<td>商品名称</td>
	<td><input type="text" name="name" value="${itemsCustom.name }"/></td>
</tr>
<tr>
	<td>商品价格</td>
	<td><input type="text" name="price" value="${itemsCustom.price }"/></td>
</tr>
<tr>
	<td>商品生产日期</td>
	<td><input type="text" name="createtime" value="<fmt:formatDate value="${itemsCustom.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td>
</tr>
<%-- <tr>
	<td>商品图片</td>
	<td>
		<c:if test="${item.pic !=null}">
			<img src="/pic/${item.pic}" width=100 height=100/>
			<br/>
		</c:if>
		<input type="file"  name="pictureFile"/> 
	</td>
</tr> --%>
<tr>
	<td>商品简介</td>
	<td>
	<textarea rows="3" cols="30" name="detail">${itemsCustom.detail }</textarea>
	</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="提交"/>
</td>
</tr>
</table>


</form>
</body>


</html>

回顾一下商品浏览界面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查询商品列表</title>
</head>
<body> 
<form action="${pageContext.request.contextPath }/queryItem.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td><input type="submit" value="查询"/></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
	<td>商品名称</td>
	<td>商品价格</td>
	<td>生产日期</td>
	<td>商品描述</td>
	<td>操作</td>
</tr>
<c:forEach items="${itemsList }" var="item">
<tr>
	<td>${item.name }</td>
	<td>${item.price }</td>
	<td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
	<td>${item.detail }</td>
	
	<td><a href="${pageContext.request.contextPath }/editItems.action?id=${item.id}">修改</a></td>


</tr>
</c:forEach>


</table>
</form>
</body>


</html>

调试:

点击修改,如图


之后弹出界面如图点击修改之后页面


点击提交需要绑定数据,这个我们以后的总结中会讲。现在我们基本实现了Controller得到数据并进行页面的跳转。

转载请注明出处:http://blog.csdn.net/acmman/article/details/47000815

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

光仔December

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值