1. 程式人生 > >4.引數繫結-基本資料型別和pojo型別(傳智播客)

4.引數繫結-基本資料型別和pojo型別(傳智播客)

需求:根據商品id修改商品資訊

一.根據商品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層開發

//根據商品id查詢商品資訊
Items getItemById(Integer itemId) throws Exception;
//根據商品id查詢商品資訊
@Override
public Items getItemById(Integer itemId) throws Exception {
    return itemsMapperCustom.getItemById(itemId);
}

3.dao層開發

//根據商品id查詢商品資訊
Items getItemById(Integer itemId) throws Exception;
<!--根據商品id查詢商品資訊-->
<select id="getItemById" parameterType="int" resultMap="queryItems">
    select * from item where item_id = #{itemId}
</select>
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層開發

//修改商品資訊
int updateItems(Integer id, ItemsCustom itemsCustom) throws Exception;
//修改商品資訊
@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層開發

 //修改商品資訊
int updateByPrimaryKey(ItemsCustom record);
<!--修改商品資訊-->
<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>
public class ItemsCustom extends Items {
    private int id;
    //get和set方法......
}