SpringMVC学习笔记文本版(二)(springmvc实例教程)
wxin55 2024-10-29 17:26 10 浏览 0 评论
接上期《SpringMVC学习笔记文本版(一)》
8.springmvc 和 mybatis 整合
8.1 需求
使用springmvc和mybatis整合 完成商品列表查询。
8.2 整合思路:
mybatis和springmvc系统架构
spring 将各层整合
1.通过 spring 管理持久层的 mapper(相当于dao接口)
2.通过 spring 管理业务层的service,service 中可以调用mapper接口
进行事务控制
3.通过 spring 管理表现层 Handler, Handler 中可以调用service接口
mapper、service、Handler 都是 javabean
1表现层 springmvc Handler
2业务层 service接口
3持久层 mybatis
4数据库 mysql
调用顺序:
1表现层---》2业务层---》3持久层--》4数据库
第一步:整合持久层 mybatis、hibernate等 mapper 接口。
1.mybatis和springmvc整合,通过 spring 管理 mapper接口
2.使用mapper的扫描自动扫描mapper接口在spring中进行注册
第二步:整合业务层 service 接口
1.通过 spring 管理 service 接口
2.使用配置方式将service接口配置在spring配置文件中 或者 注解方式进行扫描service接口在spring中进行注册
3.实现事务控制。
第三步:整合表现层 springmvc Handler 在配置文件中理由组件自动扫描
由于springmvc是spring的模块,不需要整合。
8.3 整合工程(maven 创建)
所需jar包
由下向上
数据库驱动jar包:mysql
持久层的jar包: mybatis
持久层和spring整合jar包:
日志分析jar包:log4j
连接池jar包:durid
spring核心jar包。
jstl标签jar包
8.4 整合dao
配置文件
8.4.1 sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 全局setting配置,根据需要添加 -->
<!-- 配置别名 -->
<typeAliases>
<!-- 批量扫描别名 -->
<package name="org.hxweb.web.ssm.pojo"/>
</typeAliases>
<!-- 配置mapper
由于使用spring和mybatis的整合包进行mapper扫描,这里不需要配置了。
必须遵循:mapper.xml和mapper.java文件同名且在一个目录
-->
<!-- <mappers>
</mappers> -->
</configuration>
8.4.2 applicationContext-dao.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" 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.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<!-- 加载db.properties文件中的内容,db.properties文件中key命名要有一定的特殊规则 -->
<context:property-placeholder location="classpath:config/db.properties" />
<!-- 配置数据源 ,dbcp
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="30" />
<property name="maxIdle" value="5" />
</bean>
-->
<!-- 配置数据源 使用的是Druid数据源 -->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="0" />
<!-- 连接池最大使用连接数量 -->
<property name="maxActive" value="20" />
<!-- 连接池最小空闲 -->
<property name="minIdle" value="0" />
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="60000" />
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="33" />
<!-- 用来检测有效sql -->
<property name="validationQuery" value="${validationQuery}" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="25200000" />
<!-- 打开removeAbandoned功能 -->
<property name="removeAbandoned" value="true" />
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="1800" />
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="true" />
<!-- 监控数据库 -->
<property name="filters" value="mergeStat" />
</bean>
<!-- sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:config/mybatis/sqlMapConfig.xml" />
</bean>
<!-- mapper扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描包路径,如果需要扫描多个包,中间使用半角逗号隔开 -->
<property name="basePackage" value="org.hxweb.web.ssm.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>
8.4.3 逆向工程生成pojo和mapper(单个表的增删改查询)
1.逆向生成的包和工程中的一致。
2.将生产的文件复制到工程中。
8.4.4 手动定义商品查询mapper
针对综合的查询mapper,一般情况会有管理查询,建议自定义mapper。
8.4.4.1 ItemsMapperCustom.xml
1.sql语句非常重要:
如:SELECT * FROM items where items.name LIKE '%笔记本%'
2.必须在sql客户端是执行正确无误的(1.语法,2.业务逻辑)
3.mybatis与hibernate的最大区别在于这里:只要能写出sql语句,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="org.hxweb.web.ssm.mapper.ItemsMapperCustom">
<!-- 定义商品查询的sql片段 ,就是商品的查询条件 -->
<sql id="query_items_where">
<!-- 使用动态sql,通过if判断,满足条件进行sql拼接 -->
<!-- 查询条件通过 ItemsQueryVo包装对象中itemsCustom属性传递 -->
<if test="itemsCustom != null">
<if test="itemsCustom.name !=null and itemsCustom.name != '' ">
items.name LIKE '%${itemsCustom.name}%'
</if>
</if>
</sql>
<!-- 商品列表查询 -->
<!-- 建议 1.parameterType 传入包装对象(包装了查询条件). 2.resultMap使用扩展类 -->
<select id="findItemsList" parameterType="org.hxweb.web.ssm.pojo.ItemsQueryVo"
resultMap="org.hxweb.web.ssm.pojo.ItemsCustom">
SELECT items.* FROM items
<where>
<include refid="query_items_where"></include>
</where>
</select>
</mapper>
a.包装对象 ItemsQueryVo.java
package org.hxweb.web.ssm.pojo;
/**
* @see:商品的查询的包装对象
* @author Administrator
*
*/
public class ItemsQueryVo {
//商品信息
private Items items;
//为了系统可扩展性,对原始生成的pojo进行扩展
private ItemsCustom itemsCustom;
public Items getItems() {
return items;
}
public void setItems(Items items) {
this.items = items;
}
public ItemsCustom getItemsCustom() {
return itemsCustom;
}
public void setItemsCustom(ItemsCustom itemsCustom) {
this.itemsCustom = itemsCustom;
}
}
b.扩展类 ItemsCustom.java
package org.hxweb.web.ssm.pojo;
/**
* @see 商品信息的扩展类
* @author Administrator
*
*/
public class ItemsCustom extends Items {
//可以添加商品信息的扩展属性
}
8.4.4.2 ItemsMapperCustom.java
package org.hxweb.web.ssm.mapper;
import java.util.List;
import org.hxweb.web.ssm.pojo.ItemsCustom;
import org.hxweb.web.ssm.pojo.ItemsQueryVo;
public interface ItemsMapperCustom {
//商品列表查询
List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
}
8.5 整合 service
让 spring 管理 service 接口
8.5.1 先定义 service 接口
package org.hxweb.web.ssm.service;
import java.util.List;
import org.hxweb.web.ssm.pojo.ItemsCustom;
import org.hxweb.web.ssm.pojo.ItemsQueryVo;
/**
* @see 商品管理 service
* @author Administrator
*
*/
public interface ItemsService {
//商品查询列表
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
}
8.5.2 实现 service 接口
package org.hxweb.web.ssm.service.impl;
import java.util.List;
import org.hxweb.web.ssm.mapper.ItemsMapperCustom;
import org.hxweb.web.ssm.pojo.ItemsCustom;
import org.hxweb.web.ssm.pojo.ItemsQueryVo;
import org.hxweb.web.ssm.service.ItemsService;
import org.springframework.beans.factory.annotation.Autowired;
public class ItemsServiceImpl implements ItemsService{
@Autowired
private ItemsMapperCustom itemsMapperCustom;
@Override
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception {
//利用ItemsMapperCustom查询数据库
return itemsMapperCustom.findItemsList(itemsQueryVo);
}
}
8.5.3 在 spring 容器中配置 service
创建applicationContext-service.xml配置文件中配置service
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" 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.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<!-- 商品管理的service -->
<bean id="itemsService" class="org.hxweb.web.ssm.service.impl.ItemsServiceImpl" />
</beans>
8.5.4 事务控制(创建applicationContext-transaction.xml)
创建applicationContext-transaction.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" 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.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<!-- 事务管理器
对mybatis操作数据库事务控制,spring使用jdbc的事务控制类
-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源
dataSource在applicationContext-dao.xml中配置了
-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 传播行为 -->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- aop -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* org.hxweb.web.ssm.service.impl.*.*(..))"/>
</aop:config>
</beans>
8.6 整合 springmvc
创建springmvc.xml 文件 配置处理器映射器、处理器适配器、视图解析器。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" 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.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<!-- 可以扫描controller、service、... 这里让扫描controller,指定controller的包 -->
<context:component-scan base-package="org.hxweb.web.ssm.controller"></context:component-scan>
<!-- 使用 mvc:annotation-driven代替上边注解映射器和注解适配器配置 mvc:annotation-driven默认加载很多的参数绑定方法,
比如json转换解析器就默认加载了,如果使用mvc:annotation-driven不用配置上边的RequestMappingHandlerMapping和RequestMappingHandlerAdapter
实际开发时使用mvc:annotation-driven -->
<mvc:annotation-driven/>
<!-- 视图解析器 解析jsp解析,默认使用jstl标签,classpath下的要有jstl的包 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置jsp路径的前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 配置jsp路径的后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
8.6.2 配置前端控制器 在 web.xml 中配置
<?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_3_0.xsd"
id="WebApp_ssmhxweb" version="3.0">
<display-name>ssmhxweb</display-name>
<!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring/applicationContext-*.xml</param-value>
</context-param>
<!-- 配置字符集过滤器 -->
<filter>
<description>字符集过滤器</description>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<description>字符集编码</description>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<description>强制转码</description>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- 配置 HiddenHttpMethodFilter: 把 POST 请求转为 DELETE、PUT 请求 -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation
1.配置springmvc需要加载的配置文件(配置处理器映射器、处理器适配器、处理器、视图解析器等)
2.如果不配置contextConfigLocation,就会默认加载/WEB-INF/servlet名称-servlet.xml (这里是springmvc-servlet.xml)
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 多种配置方式:
第一种:*.action,*.do 访问以.action、.do为结尾的由DispatcherServlet来解析。
第二种:/ 所有访问地址都由DispatcherServlet来解析,对应静态文件需要配置不由DispatcherServlet进行解析。
使用此种方式可以实现RESTFul风格的url
第三种:/* 这种配置方式存在问题(不要用这种方式):最终转发到一个jsp页面时,
仍然会由DispatcherServlet解析jsp页面,不能根据jsp页面找到对应的Handler,会报错。也不符合我们设计思想。
-->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- Session超时时间,单位分钟 -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
8.6.3 开始 controller 开发
package org.hxweb.web.ssm.controller;
import java.util.List;
import org.hxweb.web.ssm.pojo.ItemsCustom;
import org.hxweb.web.ssm.service.ItemsService;
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;
/**
* @see 商品的controller
* @author Administrator
*
*/
@Controller
public class ItemsController {
@Autowired
private ItemsService itemsService;
/**
* @see 商品查询
* @return
* @throws Exception
*/
@RequestMapping("/queryItems")
public ModelAndView queryItems() throws Exception{
/*
* 调用service来查找 数据库,查询商品列表
*/
List<ItemsCustom> itemsList = itemsService.findItemsList(null);
//为返回ModelAndView填充数据和指定视图
ModelAndView modelAndView = new ModelAndView();
//相当于request.setAtttibute,在jsp页面可以通过itemsList获取商品列表数据
modelAndView.addObject("itemsList", itemsList);
//指定返回视图
//modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
modelAndView.setViewName("items/itemsList");
//返回ModelAndView
return modelAndView;
}
//商品添加
//商品修改
//商品的删除
//商品的上架
//商品的审核
//商品的下架
}
8.6.4 编写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 action="${pageContext.request.contextPath }/item/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 }/item/editItem.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
8.7 加载 spring 容器
将mapper、service、controller 都加载到 spring 容器中。
建议使用通配符的方法加载spring配置文件。
applicationContext-dao.xml
applicationContext-service.xml
applicationContext-transaction.xml
在 web.xml 文件中添加监听器 来加载spring配置文件
<!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring/applicationContext-*.xml</param-value>
</context-param>
8.8 调试
9.商品修改功能开发
9.1 需求
操作流程:
1、进入商品查询列表。
2、点击修改连接,接入商品修改页面,页面中显示了要修改的商品信息(数据库中查询)
要修改的商品从数据库中查询,根据商品id(主键)来查询商品的信息,
3、在商品修改页面,进行商品信息修改,修改后,点击提交。
9.2 开发 mapper
mapper接口功能:
1.根据商品id从数据表items中查询商品信息。
2.根据商品id更新items表的数据。
3.不用开发了,使用逆向工程生成的代码。
9.3 开发 service
service接口功能:
1.根据商品id查询商品信息
2.修改商品信息。
package org.hxweb.web.ssm.service;
import java.util.List;
import org.hxweb.web.ssm.pojo.ItemsCustom;
import org.hxweb.web.ssm.pojo.ItemsQueryVo;
/**
* @see 商品管理 service
* @author Administrator
*
*/
public interface ItemsService {
/**
* @see 商品查询列表
* @param itemsQueryVo 查询商品列表的包装对象
* @return
* @throws Exception
*/
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
/**
* @see 根据商品id查询商品信息
* @param id 查询商品的id
* @return
* @throws Exception
*/
public ItemsCustom findItemsById(Integer id) throws Exception;
/**
* @see 修改商品信息
* @param id 修改的商品id
* @param itemsCustom 修改的商品信息
* @throws Exception
*/
public void updateItems(Integer id,ItemsCustom itemsCustom) throws Exception;
}
service接口实现:
package org.hxweb.web.ssm.service.impl;
import java.util.List;
import org.hxweb.web.ssm.mapper.ItemsMapper;
import org.hxweb.web.ssm.mapper.ItemsMapperCustom;
import org.hxweb.web.ssm.pojo.Items;
import org.hxweb.web.ssm.pojo.ItemsCustom;
import org.hxweb.web.ssm.pojo.ItemsQueryVo;
import org.hxweb.web.ssm.service.ItemsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
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 {
//利用ItemsMapper查询数据库
Items items = ItemsMapper.selectByPrimaryKey(id);
//中间对商品信息进行业务处理
//...
//最终返回ItemsCustom
ItemsCustom itemsCustom = new ItemsCustom();
//将items中的属性值拷贝到itemsCustom中
BeanUtils.copyProperties(items, itemsCustom);
return itemsCustom;
}
@Override
public void updateItems(Integer id, ItemsCustom itemsCustom) throws Exception {
//添加业务校验,通常在service接口对业务关键参数进行校验
//判断商品id是否为空,建议id 类型使用java的对象类,Integer、String等,如果为空则抛出异常。
//更新商品信息,根据updateByPrimaryKeyWithBLOBs可以根据id更新商品信息的所有字段,包括大文本类型字段
//updateByPrimaryKeyWithBLOBs要求必须传入id否则抛出异常
itemsCustom.setId(id);
ItemsMapper.updateByPrimaryKeyWithBLOBs(itemsCustom);
}
}
9.4 开发 controller
controller提供方法:
1.商品信息修改页面显示(展示)。
2.商品信息修改提交。
10 @ResquestMapping
10.1 url映射
定义controller方法对应的url,进行处理器映射使用。
10.2.窄化映射处理
//为了对url进行分类管理,可以对controller类通过@RequestMapping注解 来定义跟路径,
//调用其具体方法就是跟路径+子路经(通过@RequestMapping注解定义的分具体方法的url)
10.3.限制 http 请求方法
10.3.1.处于安全性考虑,对 http的链接进行方法请求限制。
10.3.2.如果限制请求为post方法,进行get请求,就会报错:
HTTP Status 405 - Request method 'GET' not supported
相关推荐
- ES6中 Promise的使用场景?(es6promise用法例子)
-
一、介绍Promise,译为承诺,是异步编程的一种解决方案,比传统的解决方案(回调函数)更加合理和更加强大在以往我们如果处理多层异步操作,我们往往会像下面那样编写我们的代码doSomething(f...
- JavaScript 对 Promise 并发的处理方法
-
Promise对象代表一个未来的值,它有三种状态:pending待定,这是Promise的初始状态,它可能成功,也可能失败,前途未卜fulfilled已完成,这是一种成功的状态,此时可以获取...
- Promise的九大方法(promise的实例方法)
-
1、promise.resolv静态方法Promise.resolve(value)可以认为是newPromise方法的语法糖,比如Promise.resolve(42)可以认为是以下代码的语...
- 360前端一面~面试题解析(360前端开发面试题)
-
1.组件库按需加载怎么做的,具体打包配了什么-按需加载实现:借助打包工具(如Webpack的require.context或ES模块动态导入),在使用组件时才引入对应的代码。例如在V...
- 前端面试-Promise 的 finally 怎么实现的?如何在工作中使用?
-
Promise的finally方法是一个非常有用的工具,它无论Promise是成功(fulfilled)还是失败(rejected)都会执行,且不改变Promise的最终结果。它的实现原...
- 最简单手写Promise,30行代码理解Promise核心原理和发布订阅模式
-
看了全网手写Promise的,大部分对于新手还是比较难理解的,其中几个比较难的点:状态还未改变时通过发布订阅模式去收集事件实例化的时候通过调用构造函数里传出来的方法去修改类里面的状态,这个叫Re...
- 前端分享-Promise可以中途取消啦(promise可以取消吗)
-
传统Promise就像一台需要手动组装的设备,每次使用都要重新接线。而Promise.withResolvers的出现,相当于给开发者发了一个智能遥控器,可以随时随地控制异步操作。它解决了三大...
- 手写 Promise(手写输入法 中文)
-
前言都2020年了,Promise大家肯定都在用了,但是估计很多人对其原理还是一知半解,今天就让我们一起实现一个符合PromiseA+规范的Promise。附PromiseA+规范地址...
- 什么是 Promise.allSettled()!新手老手都要会?
-
Promise.allSettled()方法返回一个在所有给定的promise都已经fulfilled或rejected后的promise,并带有一个对象数组,每个对象表示对应的pr...
- 前端面试-关于Promise解析与高频面试题示范
-
Promise是啥,直接上图:Promise就是处理异步函数的API,它可以包裹一个异步函数,在异步函数完成时抛出完成状态,让代码结束远古时无限回掉的窘境。配合async/await语法糖,可...
- 宇宙厂:为什么前端离不开 Promise.withResolvers() ?
-
大家好,很高兴又见面了,我是"高级前端进阶",由我带着大家一起关注前端前沿、深入前端底层技术,大家一起进步,也欢迎大家关注、点赞、收藏、转发。1.为什么需要Promise.with...
- Promise 新增了一个超实用的 API!
-
在JavaScript的世界里,Promise一直是处理异步操作的神器。而现在,随着ES2025的发布,Promise又迎来了一个超实用的新成员——Promise.try()!这个新方法简...
- 一次搞懂 Promise 异步处理(promise 异步顺序执行)
-
PromisePromise就像这个词的表面意识一样,表示一种承诺、许诺,会在后面给出一个结果,成功或者失败。现在已经成为了主流的异步编程的操作方式,写进了标准里面。状态Promise有且仅有...
- Promise 核心机制详解(promise机制的实现原理)
-
一、Promise的核心状态机Promise本质上是一个状态机,其行为由内部状态严格管控。每个Promise实例在创建时处于Pending(等待)状态,此时异步操作尚未完成。当异步操作成功...
- javascript——Promise(js实现promise)
-
1.PromiseES6开始支持,Promise对象用于一个异步操作的最终完成(包括成功和失败)及结果值的表示。简单说就是处理异步请求的。之所以叫Promise,就是我承诺,如果成功则怎么处理,失败怎...
你 发表评论:
欢迎- 一周热门
- 最近发表
-
- ES6中 Promise的使用场景?(es6promise用法例子)
- JavaScript 对 Promise 并发的处理方法
- Promise的九大方法(promise的实例方法)
- 360前端一面~面试题解析(360前端开发面试题)
- 前端面试-Promise 的 finally 怎么实现的?如何在工作中使用?
- 最简单手写Promise,30行代码理解Promise核心原理和发布订阅模式
- 前端分享-Promise可以中途取消啦(promise可以取消吗)
- 手写 Promise(手写输入法 中文)
- 什么是 Promise.allSettled()!新手老手都要会?
- 前端面试-关于Promise解析与高频面试题示范
- 标签列表
-
- hive行转列函数 (63)
- sourcemap文件是什么 (54)
- display none 隐藏后怎么显示 (56)
- 共享锁和排他锁的区别 (51)
- httpservletrequest 获取参数 (64)
- jstl包 (64)
- qsharedmemory (50)
- watch computed (53)
- java中switch (68)
- date.now (55)
- git-bash (56)
- 盒子垂直居中 (68)
- npm是什么命令 (62)
- python中+=代表什么 (70)
- fsimage (51)
- nginx break (61)
- mysql分区表的优缺点 (53)
- centos7切换到图形界面 (55)
- 前端深拷贝 (62)
- kmp模式匹配算法 (57)
- jsjson字符串转json对象 (53)
- jdbc connection (61)
- javascript字符串转换为数字 (54)
- mybatis 使用 (73)
- 安装mysql数据库 (55)