查詢商品列表
JS
EasyUI的datagrid的格式化輸出
預設情況下,會直接顯示返回的資料,但是有些情況下不能直接顯示,如:價格、日期、性別,需要指定formatter函數。
後台實現 Controller
/** * 查詢商品列表 * * @param page * @param rows * @return */ @RequestMapping(method = RequestMethod.GET) public ResponseEntity<EasyUIResult> queryItemList( @RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "rows", defaultValue = "30") Integer rows) { try { return ResponseEntity.ok(this.itemService.queryItemList(page, rows)); } catch (Exception e) { e.printStackTrace(); } return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null); }
Service
public EasyUIResult queryItemList(Integer page, Integer rows) { // 設定分頁參數 PageHelper.startPage(page, rows); Example example = new Example(Item.class); // 安裝建立時間排序 example.setOrderByClause("created DESC"); List<Item> items = this.itemMapper.selectByExample(example); PageInfo<Item> pageInfo = new PageInfo<Item>(items); return new EasyUIResult(pageInfo.getTotal(), pageInfo.getList()); }
EasyUIResult
package com.taotao.common.bean;import java.util.List;public class EasyUIResult { private Long total; private List<?> rows; public EasyUIResult() { } public EasyUIResult(Long total, List<?> rows) { this.total = total; this.rows = rows; } public Long getTotal() { return total; } public void setTotal(Long total) { this.total = total; } public List<?> getRows() { return rows; } public void setRows(List<?> rows) { this.rows = rows; }}
測試
日誌的書寫
private static final Logger LOGGER = LoggerFactory.getLogger(ItemController.class); @RequestMapping(method = RequestMethod.POST) public ResponseEntity<Void> saveItem(Item item, @RequestParam("desc") String desc) { try { if (LOGGER.isDebugEnabled()) { LOGGER.debug("新增商品, item = {}, desc = {}", item, desc); } if (StringUtils.isEmpty(item.getTitle())) { // TODO 未完成,待最佳化 // 參數有誤, 400 return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); } // 儲存商品 Boolean bool = this.itemService.saveItem(item, desc); if (!bool) { if (LOGGER.isInfoEnabled()) { LOGGER.info("新增商品失敗, item = {}", item); } // 儲存失敗 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); } if (LOGGER.isInfoEnabled()) { LOGGER.info("新增商品成功, itemId = {}", item.getId()); } return ResponseEntity.status(HttpStatus.CREATED).build(); } catch (Exception e) { LOGGER.error("新增商品出錯! item = " + item, e); } return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); }
總結:
1、 方法的入參處需要將參數列印出
2、 業務執行的狀態發生變化時,需要列印
3、 異常處需要列印