zoukankan      html  css  js  c++  java
  • SpringMVC由浅入深day01_9商品修改功能开发

    商品修改功能开发

      9.1 需求

    操作流程:

    1、进入商品查询列表页面

    2、点击修改,进入商品修改页面,页面中显示了要修改的商品(从数据库查询)

      要修改的商品从数据库查询,根据商品id(主键)查询商品信息

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

      9.2 开发mapper

    mapper:

      根据id查询商品信息

      根据id更新Items表的数据

    不用开发了,使用逆向工程生成的代码。

    ItemsMapper.java

    package cn.itcast.ssm.mapper;
    
    import cn.itcast.ssm.po.Items;
    import cn.itcast.ssm.po.ItemsExample;
    import java.util.List;
    import org.apache.ibatis.annotations.Param;
    
    public interface ItemsMapper {
        int countByExample(ItemsExample example);
    
        int deleteByExample(ItemsExample example);
    
        int deleteByPrimaryKey(Integer id);
    
        int insert(Items record);
    
        int insertSelective(Items record);
    
        List<Items> selectByExampleWithBLOBs(ItemsExample example);
    
        List<Items> selectByExample(ItemsExample example);
    
        Items selectByPrimaryKey(Integer id);
    
        int updateByExampleSelective(@Param("record") Items record, @Param("example") ItemsExample example);
    
        int updateByExampleWithBLOBs(@Param("record") Items record, @Param("example") ItemsExample example);
    
        int updateByExample(@Param("record") Items record, @Param("example") ItemsExample example);
    
        int updateByPrimaryKeySelective(Items record);
    
        int updateByPrimaryKeyWithBLOBs(Items record);
    
        int updateByPrimaryKey(Items record);
    }

    ItemsMapper.xml

    <?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="cn.itcast.ssm.mapper.ItemsMapper" >
      <resultMap id="BaseResultMap" type="cn.itcast.ssm.po.Items" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="name" property="name" jdbcType="VARCHAR" />
        <result column="price" property="price" jdbcType="REAL" />
        <result column="pic" property="pic" jdbcType="VARCHAR" />
        <result column="createtime" property="createtime" jdbcType="TIMESTAMP" />
      </resultMap>
      <resultMap id="ResultMapWithBLOBs" type="cn.itcast.ssm.po.Items" extends="BaseResultMap" >
        <result column="detail" property="detail" jdbcType="LONGVARCHAR" />
      </resultMap>
      <sql id="Example_Where_Clause" >
        <where >
          <foreach collection="oredCriteria" item="criteria" separator="or" >
            <if test="criteria.valid" >
              <trim prefix="(" suffix=")" prefixOverrides="and" >
                <foreach collection="criteria.criteria" item="criterion" >
                  <choose >
                    <when test="criterion.noValue" >
                      and ${criterion.condition}
                    </when>
                    <when test="criterion.singleValue" >
                      and ${criterion.condition} #{criterion.value}
                    </when>
                    <when test="criterion.betweenValue" >
                      and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                    </when>
                    <when test="criterion.listValue" >
                      and ${criterion.condition}
                      <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                        #{listItem}
                      </foreach>
                    </when>
                  </choose>
                </foreach>
              </trim>
            </if>
          </foreach>
        </where>
      </sql>
      <sql id="Update_By_Example_Where_Clause" >
        <where >
          <foreach collection="example.oredCriteria" item="criteria" separator="or" >
            <if test="criteria.valid" >
              <trim prefix="(" suffix=")" prefixOverrides="and" >
                <foreach collection="criteria.criteria" item="criterion" >
                  <choose >
                    <when test="criterion.noValue" >
                      and ${criterion.condition}
                    </when>
                    <when test="criterion.singleValue" >
                      and ${criterion.condition} #{criterion.value}
                    </when>
                    <when test="criterion.betweenValue" >
                      and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                    </when>
                    <when test="criterion.listValue" >
                      and ${criterion.condition}
                      <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                        #{listItem}
                      </foreach>
                    </when>
                  </choose>
                </foreach>
              </trim>
            </if>
          </foreach>
        </where>
      </sql>
      <sql id="Base_Column_List" >
        id, name, price, pic, createtime
      </sql>
      <sql id="Blob_Column_List" >
        detail
      </sql>
      <select id="selectByExampleWithBLOBs" resultMap="ResultMapWithBLOBs" parameterType="cn.itcast.ssm.po.ItemsExample" >
        select
        <if test="distinct" >
          distinct
        </if>
        <include refid="Base_Column_List" />
        ,
        <include refid="Blob_Column_List" />
        from items
        <if test="_parameter != null" >
          <include refid="Example_Where_Clause" />
        </if>
        <if test="orderByClause != null" >
          order by ${orderByClause}
        </if>
      </select>
      <select id="selectByExample" resultMap="BaseResultMap" parameterType="cn.itcast.ssm.po.ItemsExample" >
        select
        <if test="distinct" >
          distinct
        </if>
        <include refid="Base_Column_List" />
        from items
        <if test="_parameter != null" >
          <include refid="Example_Where_Clause" />
        </if>
        <if test="orderByClause != null" >
          order by ${orderByClause}
        </if>
      </select>
      <select id="selectByPrimaryKey" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer" >
        select 
        <include refid="Base_Column_List" />
        ,
        <include refid="Blob_Column_List" />
        from items
        where id = #{id,jdbcType=INTEGER}
      </select>
      <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
        delete from items
        where id = #{id,jdbcType=INTEGER}
      </delete>
      <delete id="deleteByExample" parameterType="cn.itcast.ssm.po.ItemsExample" >
        delete from items
        <if test="_parameter != null" >
          <include refid="Example_Where_Clause" />
        </if>
      </delete>
      <insert id="insert" parameterType="cn.itcast.ssm.po.Items" >
        insert into items (id, name, price, 
          pic, createtime, detail
          )
        values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{price,jdbcType=REAL}, 
          #{pic,jdbcType=VARCHAR}, #{createtime,jdbcType=TIMESTAMP}, #{detail,jdbcType=LONGVARCHAR}
          )
      </insert>
      <insert id="insertSelective" parameterType="cn.itcast.ssm.po.Items" >
        insert into items
        <trim prefix="(" suffix=")" suffixOverrides="," >
          <if test="id != null" >
            id,
          </if>
          <if test="name != null" >
            name,
          </if>
          <if test="price != null" >
            price,
          </if>
          <if test="pic != null" >
            pic,
          </if>
          <if test="createtime != null" >
            createtime,
          </if>
          <if test="detail != null" >
            detail,
          </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
          <if test="id != null" >
            #{id,jdbcType=INTEGER},
          </if>
          <if test="name != null" >
            #{name,jdbcType=VARCHAR},
          </if>
          <if test="price != null" >
            #{price,jdbcType=REAL},
          </if>
          <if test="pic != null" >
            #{pic,jdbcType=VARCHAR},
          </if>
          <if test="createtime != null" >
            #{createtime,jdbcType=TIMESTAMP},
          </if>
          <if test="detail != null" >
            #{detail,jdbcType=LONGVARCHAR},
          </if>
        </trim>
      </insert>
      <select id="countByExample" parameterType="cn.itcast.ssm.po.ItemsExample" resultType="java.lang.Integer" >
        select count(*) from items
        <if test="_parameter != null" >
          <include refid="Example_Where_Clause" />
        </if>
      </select>
      <update id="updateByExampleSelective" parameterType="map" >
        update items
        <set >
          <if test="record.id != null" >
            id = #{record.id,jdbcType=INTEGER},
          </if>
          <if test="record.name != null" >
            name = #{record.name,jdbcType=VARCHAR},
          </if>
          <if test="record.price != null" >
            price = #{record.price,jdbcType=REAL},
          </if>
          <if test="record.pic != null" >
            pic = #{record.pic,jdbcType=VARCHAR},
          </if>
          <if test="record.createtime != null" >
            createtime = #{record.createtime,jdbcType=TIMESTAMP},
          </if>
          <if test="record.detail != null" >
            detail = #{record.detail,jdbcType=LONGVARCHAR},
          </if>
        </set>
        <if test="_parameter != null" >
          <include refid="Update_By_Example_Where_Clause" />
        </if>
      </update>
      <update id="updateByExampleWithBLOBs" parameterType="map" >
        update items
        set id = #{record.id,jdbcType=INTEGER},
          name = #{record.name,jdbcType=VARCHAR},
          price = #{record.price,jdbcType=REAL},
          pic = #{record.pic,jdbcType=VARCHAR},
          createtime = #{record.createtime,jdbcType=TIMESTAMP},
          detail = #{record.detail,jdbcType=LONGVARCHAR}
        <if test="_parameter != null" >
          <include refid="Update_By_Example_Where_Clause" />
        </if>
      </update>
      <update id="updateByExample" parameterType="map" >
        update items
        set id = #{record.id,jdbcType=INTEGER},
          name = #{record.name,jdbcType=VARCHAR},
          price = #{record.price,jdbcType=REAL},
          pic = #{record.pic,jdbcType=VARCHAR},
          createtime = #{record.createtime,jdbcType=TIMESTAMP}
        <if test="_parameter != null" >
          <include refid="Update_By_Example_Where_Clause" />
        </if>
      </update>
      <update id="updateByPrimaryKeySelective" parameterType="cn.itcast.ssm.po.Items" >
        update items
        <set >
          <if test="name != null" >
            name = #{name,jdbcType=VARCHAR},
          </if>
          <if test="price != null" >
            price = #{price,jdbcType=REAL},
          </if>
          <if test="pic != null" >
            pic = #{pic,jdbcType=VARCHAR},
          </if>
          <if test="createtime != null" >
            createtime = #{createtime,jdbcType=TIMESTAMP},
          </if>
          <if test="detail != null" >
            detail = #{detail,jdbcType=LONGVARCHAR},
          </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
      </update>
      <update id="updateByPrimaryKeyWithBLOBs" parameterType="cn.itcast.ssm.po.Items" >
        update items
        set name = #{name,jdbcType=VARCHAR},
          price = #{price,jdbcType=REAL},
          pic = #{pic,jdbcType=VARCHAR},
          createtime = #{createtime,jdbcType=TIMESTAMP},
          detail = #{detail,jdbcType=LONGVARCHAR}
        where id = #{id,jdbcType=INTEGER}
      </update>
      <update id="updateByPrimaryKey" parameterType="cn.itcast.ssm.po.Items" >
        update items
        set name = #{name,jdbcType=VARCHAR},
          price = #{price,jdbcType=REAL},
          pic = #{pic,jdbcType=VARCHAR},
          createtime = #{createtime,jdbcType=TIMESTAMP}
        where id = #{id,jdbcType=INTEGER}
      </update>
    </mapper>

      9.3 开发service

    接口功能:

      根据id查询商品信息

      修改商品信息

    ItemsServiceImpl.java

      9.4 开发controller

    方法:

      商品信息修改页面显示

      商品信息修改提交

    ItemsController.java

    添加成功页面

    修改修改链接

    修改页面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 }/items/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>

    测试:访问http://localhost:8080/springmvc_mybatis1217/editItems.action

  • 相关阅读:
    mysqld参数配置
    mysql配置详解
    前台获取json未定义问题之两种常用解决办法
    Java相关框架资料及其基础资料、进阶资料、测试资料之分享
    Jmeter之tomcat性能测试+性能改进措施
    Jmeter之mysql性能测试
    MyBatis之反射技术+JDK动态代理+cglib代理
    MyBatis之动态sql
    MyBatis+Hibernate+JDBC对比分析
    前台返回json数据的常用方式+常用的AJAX请求后台数据方式
  • 原文地址:https://www.cnblogs.com/justdoitba/p/8335598.html
Copyright © 2011-2022 走看看