使用逆向工程 mybatis-generator生成代碼。java
在com.tianlang.service.ItemsService
中添加兩個接口數據庫
/** * 根據id查詢商品信息 */ ItemsCustom findItemsById(Integer id); /** * 修改商品信息 */ void updateItems(Integer id,ItemsCustom itemsCustom);
在com.tianlang.service.impl.ItemsServiceImpl
中實現接口,增長itemsMapper
屬性json
@Autowired private ItemsMapper itemsMapper; public ItemsCustom findItemsById(Integer id) { Items items = itemsMapper.selectByPrimaryKey(id); //返回ItemsCustom ItemsCustom itemsCustom = new ItemsCustom(); //將items的屬性值拷貝到itemsCustom BeanUtils.copyProperties(items, itemsCustom); return itemsCustom; } public void updateItems(Integer id, ItemsCustom itemsCustom) { //更新商品信息使用updateByPrimaryKeyWithBLOBs根據id更新items表中全部字段,包括 大文本類型字段 //updateByPrimaryKeyWithBLOBs要求必須轉入id itemsCustom.setId(id); itemsMapper.updateByPrimaryKeyWithBLOBs(itemsCustom); }
方法:瀏覽器
@Controller public class ItemsController { @Autowired private ItemsService itemsService; //商品查詢列表 @RequestMapping("/queryItems") public ModelAndView queryItems() { //調用service查找數據庫,查詢商品列表 List<ItemsCustom> itemsList = itemsService.findItemsList(null); //返回ModelAndView ModelAndView modelAndView = new ModelAndView(); //至關於request的setAttribute方法,在jsp頁面中經過itemsList取數據 modelAndView.addObject("itemsList",itemsList); //指定視圖 modelAndView.setViewName("items/itemsList"); return modelAndView; } //商品信息修改頁面顯示 @RequestMapping("/editItems") public ModelAndView editItems() { //調用service根據商品id查詢商品信息 ItemsCustom itemsCustom = itemsService.findItemsById(1); // 返回ModelAndView ModelAndView modelAndView = new ModelAndView(); //將商品信息放到model modelAndView.addObject("itemsCustom", itemsCustom); //商品修改頁面 modelAndView.setViewName("items/editItems"); return modelAndView; } //商品信息修改提交 @RequestMapping("/editItemsSubmit") public ModelAndView editItemsSubmit(HttpServletRequest request, Integer id, ItemsCustom itemsCustom) { //調用service更新商品信息,頁面須要將商品信息傳到此方法 itemsService.updateItems(id, itemsCustom); //返回ModelAndView ModelAndView modelAndView = new ModelAndView(); //返回一個成功頁面 modelAndView.setViewName("success"); return modelAndView; } }
定義controller方法對應的url,進行處理器映射使用。安全
爲了對url進行分類管理 ,能夠類上定義根路徑,最終訪問url是根路徑+子路徑mybatis
@Controller @RequestMapping("/items") public class ItemsController {
出於安全性考慮,對http的連接進行方法限制。app
@RequestMapping(value="/editItems",method={RequestMethod.POST, RequestMethod.GET}) public ModelAndView editItems()throws Exception {
若是限制請求爲post方法,進行get請求,即將上面代碼的註解改成@RequestMapping(value="/editItems",method={RequestMethod.POST})
jsp
ModelAndView
須要方法結束時,定義ModelAndView,將model和view分別進行設置。post
若是controller方法返回stringurl
1.表示返回邏輯視圖名。
真正視圖(jsp路徑)=前綴+邏輯視圖名+後綴
@RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET}) public String editItems(Model model) { //調用service根據商品id查詢商品信息 ItemsCustom itemsCustom = itemsService.findItemsById(1); //經過形參中的model將model數據傳到頁面 //至關於modelAndView.addObject方法 model.addAttribute("itemsCustom", itemsCustom); return "items/editItems"; }
2.redirect重定向
商品修改提交後,重定向到商品查詢列表。
redirect重定向特色:瀏覽器地址欄中的url會變化。修改提交的request數據沒法傳到重定向的地址。由於重定向後從新進行request(request沒法共享)
//重定向到商品查詢列表 return "redirect:queryItems.action";
3.forward頁面轉發
經過forward進行頁面轉發,瀏覽器地址欄url不變,request能夠共享。
//頁面轉發 return "forward:queryItems.action";
在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串");