新闻、分类、标签管理
删除功能
1.修改NewService接口;
NewServicer中添加:
void deleteById(Long id);
2.修改NewServiceImpl类;
NewServiceImpl中添加:
@Override
public void deleteById(Long id) {
newsRepository.deleteById(id);
}
3.修改NewControllerr类;
NewController中添加:
@GetMapping("/news/{id}/delete")
public String delete(@PathVariable Long id, RedirectAttributes attributes) {
newsService.deleteById(id);
attributes.addFlashAttribute("message", "删除成功");
return "redirect:/admin/news";
}
Test
1.进入新闻管理界面,点击删除按钮;
更新功能
1.修改NewService接口;
NewServicer中添加:
News updateNews(Long id, News news);
2.修改NewServiceImpl类;
NewServiceImpl中添加:
@Override
public News updateNews(Long id, News news) {
News n = newsRepository.getOne(id);
if (n == null) {
System.out.println("不存在该新闻");
return null;
}
news.setUpdateTime(new Date());
return newsRepository.save(news);
}
3.修改NewControllerr类;
NewController中添加:
@GetMapping("news/{id}/toUpdate")
public String toUpdate(@PathVariable Long id, Model model) {
News news = newsService.getNewsById(id);
news.init(); // tag的list集合 转换成了 对应的字符串
model.addAttribute("news", news);
model.addAttribute("types", typeService.listType());
model.addAttribute("tags", tagService.listTag());
return "admin/news-input";
}
Test
模糊搜索
1.创建新实体类NewQuery
package com.zhongruan.entity;
import javax.persistence.Entity;
import javax.persistence.Table;
/**
* @author serena
*
*/
@Entity
@Table(name = "t_query")
public class NewQuery {
private String title;
private Long typeId;
private boolean recommend;
public NewQuery() {
}
public NewQuery(String title, Long typeId, boolean recommend) {
this.title = title;
this.typeId = typeId;
this.recommend = recommend;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Long getTypeId() {
return typeId;
}
public void setTypeId(Long typeId) {
this.typeId = typeId;
}
public boolean isRecommend() {
return recommend;
}
public void setRecommend(boolean recommend) {
this.recommend = recommend;
}
@Override
public String toString() {
return "NewQuery{" +
"title='" + title + '\'' +
", typeId=" + typeId +
", recommend=" + recommend +
'}';
}
}
2.修改NewService接口;
NewServicer中添加:
News getNewsById(Long id);
3.修改newServiceImpl类
@Override
public News getNewsById(Long id) {
return newsRepository.getOne(id);
}
test
局部刷新实现分页
2.修改NewService接口;
NewServicer中添加:
// 分页的全查
Page<News> listNews(Pageable pageable);
// 带查询条件的分页全查
Page<News> listNews(Pageable pageable, NewQuery newQuery);
3.修改newServiceImpl类
@Override
public Page<News> listNews(Pageable pageable, NewQuery newQuery) {
return newsRepository.findAll(new Specification() {
@Override
public Predicate toPredicate(Root root, CriteriaQuery cq, CriteriaBuilder cb) {
// root 数据库中的字段名
// CriteriaBuilder 构建查询条件
// CriteriaQuery 执行查询条件
// 1. 创建集合 存储查询条件
List<Predicate> list = new ArrayList<>();
// 2. 添加查询条件
if (!"".equals(newQuery.getTitle()) && newQuery.getTitle() != null) {
// 当title 有值时 项目list集合中存储查询条件 select * from t_user where name like %zhang%
list.add(cb.like(root.<String>get("title"), "%" + newQuery.getTitle() + "%"));
}
if (newQuery.getTypeId() != null) {
list.add(cb.equal(root.<Long>get("type").get("id"), newQuery.getTypeId()));
}
if (newQuery.isRecommend()) {
list.add(cb.equal(root.<Boolean>get("recommend"), newQuery.isRecommend()));
}
// 3. 执行查询
cq.where(list.toArray(new Predicate[list.size()]));
return null;
}
},pageable);
}
test