6.参数绑定-基本数据类型和pojo类型(传智播客)

需求:根据商品id修改商品信息

需求分析

  1. 进入商品查询列表页面
  2. 点击修改,进入商品修改页面,页面中显示了要修改的商品(从数据库根据商品id主键查询)
  3. 在商品修改页面,修改商品信息,修改后,点击提交

一.根据商品id查询商品信息

1.controller层开发

//根据商品id查询商品信息
@RequestMapping("/selectItem")
public ModelAndView selectItem(@RequestParam(value="item_id")Integer item_id) throws Exception{
   Items item = itemsService.getItemById(item_id);
   ModelAndView modelAndView = new ModelAndView();
   modelAndView.addObject("item",item);
   modelAndView.setViewName("items/selectItem");
   return modelAndView;
}

2.service层开发

//ItemsService
//根据商品id查询商品信息
Items getItemById(Integer itemId) throws Exception;
//ItemsServiceImpl
//根据商品id查询商品信息
@Override
public Items getItemById(Integer itemId) throws Exception {
    return itemsMapperCustom.getItemById(itemId);
}

3.dao层开发

//mapper接口
//根据商品id查询商品信息
Items getItemById(Integer itemId) throws Exception;
//sql映射文件
<!--根据商品id查询商品信息-->
<select id="getItemById" parameterType="int" resultMap="queryItems">
    select * from item where item_id = #{itemId}
</select>
//pojo类
public class Items {
    private Integer itemId;
    private String itemName;
    private Long itemPrice;
    private String itemDetail;
    private Date itemCreateDate;
    //get和set方法......
}

4.web层开发

//提交需要查询的商品id
商品列表:
<table width="100%" border=1>
    <tr>
        <td>选择</td>
        <td>商品编号</td>
        <td>商品名称</td>
        <td>商品价格</td>
        <td>商品描述</td>
        <td>操作</td>
    </tr>
    <c:forEach items="${itemsList}" var="item">
        <tr>
            <td><input type="checkbox" name="items_id" value="${item.itemId}"/></td>
            <td>${item.itemId}</td>
            <td>${item.itemName}</td>
            <td>${item.itemPrice}</td>
            <td>${item.itemDetail}</td>
            <td><a href="${pageContext.request.contextPath}/items/selectItem?item_id=${item.itemId}">修改</a></td>
        </tr>
    </c:forEach>
</table>
//显示商品信息的表单
<form id="itemForm" action="${pageContext.request.contextPath}/items/updateItem" method="post">
    商品列表:
    <table width="100%" border=1>
        <tr>
            <td>商品编号</td>
            <td>商品名称</td>
            <td>商品价格</td>
            <td>商品描述</td>
            <td>商品生产日期</td>
            <td>操作</td>
        </tr>

        <tr>
            <td><input type="hidden" name="itemId" value="${item.itemId}"/></td>
            <td><input type="text" name="itemName" value="${item.itemName}"/></td>
            <td><input type="text" name="itemPrice" value="${item.itemPrice}"/></td>
            <td><input type="text" name="itemDetail" value="${item.itemDetail}"/></td>
            <td><input type="text" name="itemCreateDate" value="<fmt:formatDate value="${item.itemCreateDate}" pattern="yyyy-MM-dd hh:mm:ss"/>"/></td>
            <td><input type="submit" value="修改"></td>
        </tr>
    </table>
</form>

二.根据提交的商品信息进行数据库记录修改

1.controller层开发

//修改商品信息
@RequestMapping(value = "/updateItem")
public ModelAndView updateItem(@RequestParam(value="itemId")Integer item_id, ItemsCustom itemsCustom) throws Exception{
   ModelAndView modelAndView = new ModelAndView();
   int rs = itemsService.updateItems(item_id,itemsCustom);
   if(rs == 1) {
       modelAndView.setViewName("items/success");
   }else{
       modelAndView.setViewName("items/failed");
   }
   return modelAndView;
}

2.service层开发

//ItemsService
//修改商品信息
int updateItems(Integer id, ItemsCustom itemsCustom) throws Exception;
//ItemsServiceImpl
//修改商品信息
@Override
public int updateItems(Integer id, ItemsCustom itemsCustom) throws Exception {
    //添加业务校验,通常在service接口对关键参数进行校验
    //校验id是否为空,如果为空抛出异常

    //更新商品信息使用updateByPrimaryKeyWithBLOBs根据id更新items表中所有字段,包括 大文本类型字段
    //updateByPrimaryKeyWithBLOBs要求必须转入id
    itemsCustom.setId(id);
    return itemsMapperCustom.updateByPrimaryKey(itemsCustom);
}

3.dao层开发

//mapper接口
//修改商品信息
int updateByPrimaryKey(ItemsCustom record);
//sql映射文件
<!--修改商品信息-->
<update id="updateByPrimaryKey" parameterType="itemsCustom">
    update item
    set item_name = #{itemName,jdbcType=VARCHAR},
    item_price = #{itemPrice,jdbcType=DECIMAL},
    item_detail = #{itemDetail,jdbcType=VARCHAR},
    item_createDate = #{itemCreateDate,jdbcType=TIMESTAMP}
    where item_id = #{itemId,jdbcType=INTEGER}
</update>
//pojo类
public class ItemsCustom extends Items {
    private int id;
    //get和set方法......
}

猜你喜欢

转载自blog.csdn.net/u010286027/article/details/84861674
今日推荐