【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

相关文章
|
1月前
|
缓存 前端开发 Java
Spring MVC 面试题及答案整理,最新面试题
Spring MVC 面试题及答案整理,最新面试题
90 0
|
1月前
ssm(Spring+Spring mvc+mybatis)——updateDept.jsp
ssm(Spring+Spring mvc+mybatis)——updateDept.jsp
10 0
|
1月前
ssm(Spring+Spring mvc+mybatis)——showDept.jsp
ssm(Spring+Spring mvc+mybatis)——showDept.jsp
9 0
|
1月前
|
SQL JavaScript Java
springboot+springm vc+mybatis实现增删改查案例!
springboot+springm vc+mybatis实现增删改查案例!
25 0
|
1月前
|
SQL Java 数据库连接
挺详细的spring+springmvc+mybatis配置整合|含源代码
挺详细的spring+springmvc+mybatis配置整合|含源代码
41 1
|
12天前
|
数据采集 前端开发 Java
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
23 3
|
12天前
|
存储 前端开发 Java
会话锦囊:揭示Spring MVC如何巧妙使用@SessionAttributes
会话锦囊:揭示Spring MVC如何巧妙使用@SessionAttributes
14 1
|
12天前
|
前端开发 Java Spring
数据之桥:深入Spring MVC中传递数据给视图的实用指南
数据之桥:深入Spring MVC中传递数据给视图的实用指南
29 3
|
21天前
|
前端开发 安全 Java
使用Java Web框架:Spring MVC的全面指南
【4月更文挑战第3天】Spring MVC是Spring框架的一部分,用于构建高效、模块化的Web应用。它基于MVC模式,支持多种视图技术。核心概念包括DispatcherServlet(前端控制器)、HandlerMapping(请求映射)、Controller(处理请求)、ViewResolver(视图解析)和ModelAndView(模型和视图容器)。开发流程涉及配置DispatcherServlet、定义Controller、创建View、处理数据、绑定模型和异常处理。
使用Java Web框架:Spring MVC的全面指南
|
27天前
|
SQL Java 数据库连接
Mybatis是如何实现分页功能的
Mybatis是如何实现分页功能的
10 0