cms_文章管理 (5)

/**
* @Title: BaseQuery.java
* @Package:cn.itsource.util
* @Description:(作用:分页查询的工具类,分页查询都需要继承该类方便实现分页参数的接收和传递 )
* @author:long
* @date:2021年1月15日
* @version:V1.0
*/
public class BaseQuery{
private Integer localPage=0;
private Integer pageSize=0;
public Integer getBegin(){
return (localPage-1)*pageSize;
}

public Integer getLocalPage() {
return localPage;
}
public void setLocalPage(Integer localPage) {
this.localPage = localPage;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
@Override
public String toString() {
return "BaseQuery [localPage=" + localPage + ", pageSize=" + pageSize + "]";
}
}

 

 

 

 

 

文章管理页面处理器调用的方法

 

package cn.itsource.service;


import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import cn.itsource.domain.Article;
import cn.itsource.query.IArticleQuery;
import cn.itsource.util.PageBase;

public interface IArtService {
PageBase<Article> findall(IArticleQuery query);
void del(Long id,HttpServletRequest req);
Map<String, List<Article>> findArticle();
void save(Article article,HttpServletRequest req);
void delete(Long id);
Article updateClick(String url);
}

 

 

 

 

文章管理页面处理器调用的抽象类的实现

 

 

 

package cn.itsource.service.impl;

 

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

 

import javax.servlet.http.HttpServletRequest;

 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

 

import cn.itsource.domain.Article;
import cn.itsource.domain.ArticleType;
import cn.itsource.mapper.Articlemapper;
import cn.itsource.mapper.Artictypelemapper;
import cn.itsource.query.IArticleQuery;
import cn.itsource.service.IArtService;
import cn.itsource.util.Constant;
import cn.itsource.util.FreeMarkerUtil;
import cn.itsource.util.PageBase;
@Service
public class ArtServiceimpl implements IArtService{

@Autowired
private Articlemapper mapper;
@Autowired
private Artictypelemapper typemapper;

/*查询*/
@Override
public PageBase<Article> findall(IArticleQuery query) {
/*查询文章总数量: 根据高级查询拿三个条件来决定*/
Integer totalsCount = mapper.findTotalCount(query);
/*按照分页条件查数据: 高级查询三条件加入进来*/
List<Article> list = mapper.findAll(query);
/*给每一个article对象的type属性赋值*/
for (Article article : list) {
Long id = article.getTypeId();
ArticleType type = typemapper.findbyid(id);
//System.out.println(type);
article.setType(type);
}
PageBase<Article> li = new PageBase<>(list, totalsCount);

return li;
}

/*删除*/
@Override
public void delete(Long id) {
System.out.println("文章管理执行了删除操作");
mapper.delete(id);
}

/*前台文章分栏查询*/
@Override
public Map<String, List<Article>> findArticle() {
Map<String, List<Article>> map= new HashMap<>();
/*查询技术文章*/
List<Article> technologys = mapper.findArticleByCode(Constant.TECHNOLOGY);
/*查询行业新闻*/
List<Article> industrys = mapper.findArticleByCode(Constant.INDUSTRY);
/*查询学科咨询*/
List<Article> subjects = mapper.findArticleByCode(Constant.SUBJECT);
map.put("technologys", technologys);
map.put("industrys", industrys);
map.put("subjects", subjects);
return map;
}

/*添加*/
@Override
public void save(Article article, HttpServletRequest req) {
String realPath = req.getServletContext().getRealPath("/static/template");
File file = new File(realPath);
if(!file.exists()){//如果文件夹不存在,就创建文件夹
file.mkdirs();
}
String url = FreeMarkerUtil.createFile(realPath, "article.ftl", article, ".html");
article.setUrl("/static/template/"+url);
if(article.getId()!=null){
Article oldArticle = mapper.findById(article.getId());//得到了数据库中旧的那条数据
String oldUrl = oldArticle.getUrl();// /static/template/1605176075312.html
//F:/workspace/PojOneSpace/Day47_cms03/src/main/webapp+
String real = req.getServletContext().getRealPath("http://www.likecs.com/");
new File(real,oldUrl).delete();//删除旧的文件
mapper.update(article);
}else{
mapper.add(article);
}
}

/*删除*/
@Override
public void del(Long id, HttpServletRequest req) {
Article article = mapper.findOne(id);
mapper.del(id);
String realPath = req.getServletContext().getRealPath("http://www.likecs.com/");
new File(realPath, article.getUrl()).delete();

}

 

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpdzsz.html