天天看點

宜立方商城内容管理課後作業

CMS系統中的内容管理主要是管理前端首頁的展示,如首頁輪播展示的商品等

作業共三個子產品,内容顯示,修改,删除

目錄

​​内容顯示​​

​​編輯​​

​​批量删除​​

内容顯示

首先在前端代碼中找到相應HTML代碼,并在其中找到URL

<table class="easyui-datagrid" id="contentList"
                   data-options="toolbar:contentListToolbar,singleSelect:false,collapsible:true,pagination:true,method:'get',pageSize:20,url:'/content/query/list',queryParams:{categoryId:0}">      

由HTML代碼加以分析可以得出需要傳回的應該是一個對象,并需要對其進行分頁(分頁部分模仿第四天内容)

宜立方商城内容管理課後作業

接着開始寫代碼

interface層

/**
   * 顯示内容清單
   *
   * @param categoryId 商品id
   * @param page       頁數
   * @param rows       行數
   * @return EasyUIDataGridResult
   */
  EasyUIDataGridResult getItemList(Long categoryId, Integer page, Integer rows);      

Service層

@Override
  public EasyUIDataGridResult getItemList(Long categoryId, Integer page, Integer rows) {
    //設定分頁資訊
    PageHelper.startPage(page,rows);
    //建立查詢條件
    TbContentExample example = new TbContentExample();
    TbContentExample.Criteria criteria = example.createCriteria();
    //設定查詢條件
    criteria.andCategoryIdEqualTo(categoryId);
    //執行查詢
    List<TbContent> tbContents = contentMapper.selectByExample(example);
    //提取分頁資訊
    PageInfo<TbContent> pageInfo = new PageInfo<>(tbContents);
    //建立傳回結果對象
    EasyUIDataGridResult easyUIDataGridResult = new EasyUIDataGridResult();
    //顯示商品資訊
    easyUIDataGridResult.setRows(tbContents);
    //顯示分頁
    easyUIDataGridResult.setTotal(pageInfo.getTotal());
    return easyUIDataGridResult;
  }      

controller層

/**
   * 分頁顯示内容管理資料
   *
   * @param categoryId 内容id
   * @param page       頁數
   * @param rows       行數
   * @return EasyUIDataGridResult
   */
  @RequestMapping("/content/query/list")
  @ResponseBody
  public EasyUIDataGridResult getItemList(Long categoryId, Integer page, Integer rows) {
    return contentService.getItemList(categoryId, page, rows);
  }      

編輯

首先分析URL

宜立方商城内容管理課後作業

要做編輯功能,首先要做的就是回寫,這個和前面的編輯思路的一樣的

首先查詢本來的資訊異步回寫到頁面中

interface層

/**
   * 回寫内容描述
   * @param id 内容id
   * @return TbContent
   */
  TbContent selectByIdContent(Long id);      

Service層

@Override
  public TbContent selectByIdContent(Long id) {
    return contentMapper.selectByPrimaryKey(id);
  }      

Controller層

/**
   * 回寫内容描述
   *
   * @param id 内容id
   * @return TbContent
   */
  @RequestMapping("/query/content/{id}")
  @ResponseBody
  public TbContent selectContent(@PathVariable Long id) {
    return contentService.selectByIdContent(id);
  }      

此時内容已經傳到了前端,卻并沒有顯示出來,經過分析,在jsp頁面中加入

// 加載商品描述
                    $.getJSON('/query/content/' + data.id, function (data) {
                        //if(_data.status == 200){
                        //UM.getEditor('itemeEditDescEditor').setContent(_data.data.itemDesc, false);
                        contentEditEditor.html(data.content);
                        // }
                    });      

此時能夠進行正常回寫,然後開始修改、送出

Interface

/**
   * 修改内容
   *
   * @param content 内容pojo
   * @return E3Result
   */
  E3Result editContent(TbContent content);      

Service

@Override
  public E3Result editContent(TbContent content) {
    //設定修改時間
    content.setUpdated(new Date());
    //執行修改
    contentMapper.updateByPrimaryKey(content);
    return E3Result.ok();
  }      

Controller

/**
   * 修改内容
   *
   * @param content 修改内容pojo
   * @return E3Result
   */
  @RequestMapping("/rest/content/edit")
  @ResponseBody
  public E3Result editContent(TbContent content) {
    return contentService.editContent(content);
  }      

到這兒本來已經結束了,然而在測試時發現每次寫的描述并不能更新成功,也不會持久化到資料庫,剛開始太信賴逆向工程,就一直在分析前端的傳輸,後來再進行背景調試,發現資訊是能到達背景的,是以才開始看mpaaer檔案,發現果然是插入語句的問題。應改為

<update id="updateByPrimaryKey" parameterType="cn.e3mall.pojo.TbContent">
        update tb_content
        set category_id = #{categoryId,jdbcType=BIGINT},
            title       = #{title,jdbcType=VARCHAR},
            sub_title   = #{subTitle,jdbcType=VARCHAR},
            title_desc  = #{titleDesc,jdbcType=VARCHAR},
            url         = #{url,jdbcType=VARCHAR},
            pic         = #{pic,jdbcType=VARCHAR},
            pic2        = #{pic2,jdbcType=VARCHAR},
            created     = #{created,jdbcType=TIMESTAMP},
            updated     = #{updated,jdbcType=TIMESTAMP},
            content     = #{content}
        where id = #{id,jdbcType=BIGINT}
    </update>      

或通過其他方式,調用updateByPrimaryKeyWithBLOBs方法

批量删除

找到URL為

宜立方商城内容管理課後作業

Interface

/**
   * 批量删除選中删除内容
   * @param ids 選中内容清單
   * @return E3Result
   */
  E3Result deleteBatchContent(String[] ids);      
@Override
  public E3Result deleteBatchContent(String[] ids) {
    for (String id  :ids) {
      //執行删除操作
      contentMapper.deleteByPrimaryKey(Long.valueOf(id));
      TbContent content = contentMapper.selectByPrimaryKey((long) Integer.parseInt(id));
    }
    return E3Result.ok();
  }      
/**
   * 批量删除内容
   *
   * @param ids 批量内容id
   * @return E3Result
   */
  @RequestMapping("/content/delete")
  @ResponseBody
  public E3Result deleteContent(String[] ids) {
    return contentService.deleteBatchContent(ids);
  }