本来图文发布中不包含评论量这一列,现在我们需要经这一列添加到模板中.
一,实体类层
首先实体类要在原来的基础上添加一个字段(评论量)
package com.eastcom.publicity.domain;
import com.eastcom.article.domain.ArticleDO;
public class PublicityDO extends ArticleDO {
private int commentNum;
public int getCommentNum() {
return commentNum;
}
public void setCommentNum(int commentNum) {
this.commentNum = commentNum;
}
}
继承原来的实体类模板articleDO
二 controller的改变
@Autowired
private ArticleService articleService;
@Autowired
private PublicityService publicityService;
@ResponseBody
@GetMapping("/publicity")
@RequiresPermissions("publicity:publicity:queryList")
public R queryArticles(ArticleQueryParamDO queryParam) {
//模板获取所有的文章
/* return R.success(articleService.queryArticles(templateId, queryParam, getUser()));*/
//添加一列后获取所有的文章
return R.success(publicityService.queryArticles(templateId, queryParam, getUser()));
}
由代码可知我们改变了获取所有文章了接口文件,因为我们增加了个字段
三 新的接口文件
package com.eastcom.publicity.service;
import com.eastcom.common.domain.PagingList;
import com.eastcom.common.domain.QueryParamDO;
import com.eastcom.common.domain.UserDO;
public interface PublicityService {
public PagingList queryArticles(int templateId, QueryParamDO queryParamDO, UserDO userDO);
}
引用的两个工具类 PagingList和QueryParamDO
package com.eastcom.common.domain;
import java.util.List;
public class PagingList {
private int total;
private List<?> rows;
public static PagingList create(int total, List<?> rows) {
return new PagingList(total, rows);
}
public PagingList(int total, List<?> rows) {
this.total = total;
this.rows = rows;
}
public List<?> getRows() {
return rows;
}
public void setRows(List<?> rows) {
this.rows = rows;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
}
package com.eastcom.common.domain;
import com.eastcom.common.utils.StringUtils;
public class QueryParamDO {
private int offset;
private int limit;
private String sortName;
private String sortOrder;
private String search;
public String getSearch() {
return search;
}
public void setSearch(String search) {
this.search = search;
}
public int getOffset() {
return offset;
}
public void setOffset(int offset) {
this.offset = offset;
}
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
public String getSortName() {
if (StringUtils.isEmpty(sortName)) {
return "1";
}
return "`" + StringUtils.HumpToUnderline(sortName) + "`";
}
public void setSortName(String sortName) {
this.sortName = sortName;
}
public String getSortOrder() {
if ("desc".equalsIgnoreCase(sortOrder))
return "desc";
return "asc";
}
public void setSortOrder(String sortOrder) {
this.sortOrder = sortOrder;
}
}
四 dao层的变化
package com.eastcom.publicity.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import com.eastcom.common.domain.QueryParamDO;
import com.eastcom.common.domain.UserDO;
import com.eastcom.publicity.domain.PublicityDO;
@Mapper
public interface PublicityDao {
public int queryArticlesCount(@Param("templateId") int templateId, @Param("queryParamDO") QueryParamDO queryParamDO, @Param("userDO") UserDO userDO);
public List<PublicityDO> queryArticles(@Param("templateId") int templateId, @Param("queryParamDO") QueryParamDO queryParamDO, @Param("userDO") UserDO userDO);
}
五 impl层
package com.eastcom.publicity.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.eastcom.common.domain.PagingList;
import com.eastcom.common.domain.QueryParamDO;
import com.eastcom.common.domain.UserDO;
import com.eastcom.publicity.dao.PublicityDao;
import com.eastcom.publicity.domain.PublicityDO;
import com.eastcom.publicity.service.PublicityService;
@Service
public class PublictyServiceImpl implements PublicityService {
@Autowired
private PublicityDao publicityDao;
@Override
public PagingList queryArticles(int templateId, QueryParamDO queryParamDO, UserDO userDO) {
int total = publicityDao.queryArticlesCount(templateId, queryParamDO, userDO);
List<PublicityDO> rows = publicityDao.queryArticles(templateId, queryParamDO, userDO);
return PagingList.create(total, rows);
}
}
六 js的变化
$(function() {
//在这添加一列
var columns = ArticleList.options.table.columns;
//设置添加的位置
columns.splice(columns.length - 1, 0, {
//添加的字段
field : 'commentNum',
//添加的字段名称
title : '评论量',
sortable : true,
searchable : true,
align : 'center',
//内容
formatter : function(value, row, index) {
var a = value ;
return a;
},
});
ArticleList.init({
url : '../../publicity/publicity',
view : 'publicity/publicity/view',
table : {}
});
});