在基本的項目中,無非就是基本的增刪改查,前面我們已經實現了一個簡單的查詢功能,現在來實現增刪改功能,來了解實際開發中的運用,以修改功能為例,因為修改功能基本覆蓋了增加和刪除的運用。
前面我們實現了查詢列表的功能,現在根據查詢列表進入到商品詳情,然後修改商品信息然後再返回商品列表頁面。
1、開發Mapper:根據id查詢商品信息、根據id更新Items表的數據
這個可以使用逆向工程實現,也可以自己實現。
2、開發service
service接口:
1 // 根據id查詢商品信息 2 public ItemsCustom findItemsById(Integer id) throws Exception; 3 // 修改商品信息 4 public void updateItems(Integer id, ItemsCustom itemsCustom) 5 throws Exception;
service實現類:
1 public ItemsCustom findItemsById(Integer id) throws Exception {
2
3 Items items = itemsMapper.selectByPrimaryKey(id);
4 // 中間對商品信息進行業務處理
5 // ....
6 // 返回ItemsCustom
7 ItemsCustom itemsCustom = new ItemsCustom();
8 // 將items的屬性值拷貝到itemsCustom
9 BeanUtils.copyProperties(items, itemsCustom);
10
11 return itemsCustom;
12
13 }
14
15 @Override
16 public void updateItems(Integer id, ItemsCustom itemsCustom)
17 throws Exception {
18 // 添加業務校驗,通常在service接口對關鍵參數進行校驗
19 // 校驗 id是否為空,如果為空拋出異常
20
21 // 更新商品信息使用updateByPrimaryKeyWithBLOBs根據id更新items表中所有字段,包括 大文本類型字段
22 // updateByPrimaryKeyWithBLOBs要求必須轉入id
23 itemsCustom.setId(id);
24 itemsMapper.updateByPrimaryKeyWithBLOBs(itemsCustom);
25 }
3、開發Controller
1 @Controller
2 @RequestMapping("/items")
3 public class ItemController {
4 @Autowired
5 private ItemsService itemsService;
6 @RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})
7 //@RequestParam裡邊指定request傳入參數名稱和形參進行綁定。
8 //通過required屬性指定參數是否必須要傳入
9 //通過defaultValue可以設置默認值,如果id參數沒有傳入,將默認值和形參綁定。
10 public String editItems(Model model,@RequestParam(value="id",required=true) Integer items_id)throws Exception {
11 //調用service根據商品id查詢商品信息
12 ItemsCustom itemsCustom = itemsService.findItemsById(items_id);
13 //通過形參中的model將model數據傳到頁面
14 //相當於modelAndView.addObject方法
15 model.addAttribute("itemsCustom", itemsCustom);
16
17 return "items/editItems";
18 }
19 // 商品信息修改提交
20 @RequestMapping("/editItemsSubmit")
21 public String editItemsSubmit(HttpServletRequest request, Integer id,
22 ItemsCustom itemsCustom) throws Exception {
23 // 調用service更新商品信息,頁面需要將商品信息傳到此方法
24 itemsService.updateItems(id, itemsCustom);
25 // 重定向到商品查詢列表
26 //return "redirect:queryItems.action";
27 // 頁面轉發
28 return "forward:queryItems.action";
29 //return "success";
30 }
31 }
從這兩個方法中有很多可以總結的:
1、在類前面加@RequestMapping("/items"),可以窄化請求,是請求根據類的url和方法的url拼接,這樣可以按控制器進行分類來實現不同的調用。
2、在方法前面加@RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET}),這樣可以對方法的訪問進行限制,只要get和post的方法可以訪問。
3、Controller的方法的返回類型有多種,可以是ModelAndView、void或string。
(1)、返回ModelAndView
需要方法結束時,定義ModelAndView,將model和view分別進行設置。
(2)、返回string
表示返回邏輯視圖名:真正視圖(jsp路徑)=前綴+邏輯視圖名+後綴
redirect重定向:"redirect:queryItems.action"
forward頁面轉發:"forward:queryItems.action"
(3)、返回void:
在controller方法形參上可以定義request和response,使用request或response指定響應結果:
1、使用request轉向頁面,如下:
request.getRequestDispatcher("頁面路徑").forward(request, response);
2、通過response頁面重定向:
response.sendRedirect("url")
3、通過response指定響應結果,例如響應json數據如下:
response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
response.getWriter().write("json串");