【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月前
|
前端开发 Java 微服务
《深入理解Spring》:Spring、Spring MVC与Spring Boot的深度解析
Spring Framework是Java生态的基石,提供IoC、AOP等核心功能;Spring MVC基于其构建,实现Web层MVC架构;Spring Boot则通过自动配置和内嵌服务器,极大简化了开发与部署。三者层层演进,Spring Boot并非替代,而是对前者的高效封装与增强,适用于微服务与快速开发,而深入理解Spring Framework有助于更好驾驭整体技术栈。
|
11月前
|
前端开发 Java 测试技术
微服务——SpringBoot使用归纳——Spring Boot中的MVC支持——@RequestParam
本文介绍了 `@RequestParam` 注解的使用方法及其与 `@PathVariable` 的区别。`@RequestParam` 用于从请求中获取参数值(如 GET 请求的 URL 参数或 POST 请求的表单数据),而 `@PathVariable` 用于从 URL 模板中提取参数。文章通过示例代码详细说明了 `@RequestParam` 的常用属性,如 `required` 和 `defaultValue`,并展示了如何用实体类封装大量表单参数以简化处理流程。最后,结合 Postman 测试工具验证了接口的功能。
641 0
微服务——SpringBoot使用归纳——Spring Boot中的MVC支持——@RequestParam
|
11月前
|
JSON 前端开发 Java
微服务——SpringBoot使用归纳——Spring Boot中的MVC支持——@RequestBody
`@RequestBody` 是 Spring 框架中的注解,用于将 HTTP 请求体中的 JSON 数据自动映射为 Java 对象。例如,前端通过 POST 请求发送包含 `username` 和 `password` 的 JSON 数据,后端可通过带有 `@RequestBody` 注解的方法参数接收并处理。此注解适用于传递复杂对象的场景,简化了数据解析过程。与表单提交不同,它主要用于接收 JSON 格式的实体数据。
1150 0
|
11月前
|
前端开发 Java 微服务
微服务——SpringBoot使用归纳——Spring Boot中的MVC支持——@PathVariable
`@PathVariable` 是 Spring Boot 中用于从 URL 中提取参数的注解,支持 RESTful 风格接口开发。例如,通过 `@GetMapping(&quot;/user/{id}&quot;)` 可以将 URL 中的 `{id}` 参数自动映射到方法参数中。若参数名不一致,可通过 `@PathVariable(&quot;自定义名&quot;)` 指定绑定关系。此外,还支持多参数占位符,如 `/user/{id}/{name}`,分别映射到方法中的多个参数。运行项目后,访问指定 URL 即可验证参数是否正确接收。
711 0
|
11月前
|
JSON 前端开发 Java
微服务——SpringBoot使用归纳——Spring Boot中的MVC支持——@RequestMapping
@RequestMapping 是 Spring MVC 中用于请求地址映射的注解,可作用于类或方法上。类级别定义控制器父路径,方法级别进一步指定处理逻辑。常用属性包括 value(请求地址)、method(请求类型,如 GET/POST 等,默认 GET)和 produces(返回内容类型)。例如:`@RequestMapping(value = &quot;/test&quot;, produces = &quot;application/json; charset=UTF-8&quot;)`。此外,针对不同请求方式还有简化注解,如 @GetMapping、@PostMapping 等。
611 0
|
11月前
|
JSON 前端开发 Java
微服务——SpringBoot使用归纳——Spring Boot中的MVC支持——@RestController
本文主要介绍 Spring Boot 中 MVC 开发常用的几个注解及其使用方式,包括 `@RestController`、`@RequestMapping`、`@PathVariable`、`@RequestParam` 和 `@RequestBody`。其中重点讲解了 `@RestController` 注解的构成与特点:它是 `@Controller` 和 `@ResponseBody` 的结合体,适用于返回 JSON 数据的场景。文章还指出,在需要模板渲染(如 Thymeleaf)而非前后端分离的情况下,应使用 `@Controller` 而非 `@RestController`
454 0
|
7月前
|
前端开发 Java API
Spring Cloud Gateway Server Web MVC报错“Unsupported transfer encoding: chunked”解决
本文解析了Spring Cloud Gateway中出现“Unsupported transfer encoding: chunked”错误的原因,指出该问题源于Feign依赖的HTTP客户端与服务端的`chunked`传输编码不兼容,并提供了具体的解决方案。通过规范Feign客户端接口的返回类型,可有效避免该异常,提升系统兼容性与稳定性。
529 0
|
7月前
|
SQL Java 数据库连接
Spring、SpringMVC 与 MyBatis 核心知识点解析
我梳理的这些内容,涵盖了 Spring、SpringMVC 和 MyBatis 的核心知识点。 在 Spring 中,我了解到 IOC 是控制反转,把对象控制权交容器;DI 是依赖注入,有三种实现方式。Bean 有五种作用域,单例 bean 的线程安全问题及自动装配方式也清晰了。事务基于数据库和 AOP,有失效场景和七种传播行为。AOP 是面向切面编程,动态代理有 JDK 和 CGLIB 两种。 SpringMVC 的 11 步执行流程我烂熟于心,还有那些常用注解的用法。 MyBatis 里,#{} 和 ${} 的区别很关键,获取主键、处理字段与属性名不匹配的方法也掌握了。多表查询、动态
221 0
|
7月前
|
JSON 前端开发 Java
第05课:Spring Boot中的MVC支持
第05课:Spring Boot中的MVC支持
327 0
|
9月前
|
SQL Java 数据安全/隐私保护
发现问题:Mybatis-plus的分页总数为0,分页功能失效,以及多租户插件的使用。
总的来说,使用 Mybatis-plus 确实可以极大地方便我们的开发,但也需要我们理解其工作原理,掌握如何合适地使用各种插件。分页插件和多租户插件是其中典型,它们的运用可以让我们的代码更为简洁、高效,理解和掌握好它们的用法对我们的开发过程有着极其重要的意义。
876 15