1.修改NewService接口;
NewServicer中添加:
void deleteNew(Long id);
2.修改NewServiceImpl类;
NewServiceImpl中添加:
@Override
public void deleteNew(Long id) {
newRepository.deleteById(id);
}
3.修改NewControllerr类;
NewController中添加:
@GetMapping("/news/{id}/delete")
public String delete(@PathVariable Long id,RedirectAttributes attributes){
newService.deleteNew(id);
attributes.addFlashAttribute("message","删除成功");
return REDIRECT_LIST;
}
Test
1.进入新闻管理界面,点击删除按钮;
2.新闻删除成功;
1.修改TypeRepository接口;
TypeRepositoryr中添加:
@Query("select t from Type t")
List<Type> findTop(Pageable pageable);
2.修改TypeService接口;
TypeServicer中添加:
List<Type> listTypeTop(Integer size);
3.修改TypeServiceImpl类;
TypeServiceImpl中添加:
@Override
public List<Type> listTypeTop(Integer size) {
Sort sort = Sort.by(Sort.Direction.DESC,"news.size");
Pageable pageable = PageRequest.of(0,size,sort);
return typeRepository.findTop(pageable);
}
1.修改TagRepository接口;
TagRepository中添加:
@Query("select t from Tag t")
List<Tag> findTop(Pageable pageable);
2.修改TagService接口;
TagServicer中添加:
List<Tag> listTagTop(Integer size);
3.修改TagServiceImpl类;
TagServiceImpl中添加:
@Override
public List<Tag> listTagTop(Integer size) {
Sort sort = Sort.by(Sort.Direction.DESC,"newsList.size");
Pageable pageable = PageRequest.of(0,size,sort);
return tagRepository.findTop(pageable);
}
1.修改NewRepository接口;
NewRepository中添加:
@Query("select n from News n where n.title like ?1 or n.content like ?1")
Page<News> findByQuery(String query, Pageable pageable);
@Query("select n from News n where n.recommend = true")
List<News> findTop(Pageable pageable);
2.修改NewService接口;
NewServicer中添加:
//主页显示列表
Page<News> listNew(Pageable pageable);
//主页推荐最新新闻列表
List<News> listRecommendNewTop(Integer size);
//全局搜索
Page<News> listNew(String query,Pageable pageable);
3.修改NewServiceImpl类;
NewServiceImpl中添加:
@Override
public Page<News> listNew(Pageable pageable) {
return newRepository.findAll(pageable);
}
@Override
public List<News> listRecommendNewTop(Integer size) {
Sort sort = Sort.by(Sort.Direction.DESC,"updateTime");
Pageable pageable = PageRequest.of(0,size,sort);
return newRepository.findTop(pageable);
}
@Override
public Page<News> listNew(String query, Pageable pageable) {
return newRepository.findByQuery(query,pageable);
}
4.修改IndexControllerr类;
IndexController中添加:
@Autowired
private NewService newService;
@Autowired
private TypeService typeService;
@Autowired
private TagService tagService;
@GetMapping("/")
public String index(@PageableDefault(size = 3,sort = {
"updateTime"},direction = Sort.Direction.DESC)
Pageable pageable, Model model){
model.addAttribute("page",newService.listNew(pageable));
model.addAttribute("types",typeService.listTypeTop(3));
model.addAttribute("tags",tagService.listTagTop(3));
model.addAttribute("recommendNews",newService.listRecommendNewTop(3));
return "index";
}
@PostMapping("/search")
public String search(@PageableDefault(size = 3,sort = {
"updateTime"},direction = Sort.Direction.DESC)
Pageable pageable, @RequestParam String query, Model model){
model.addAttribute("page",newService.listNew("%"+query+"%",pageable));
model.addAttribute("query",query);
return "search";
}
1.news下创建util包下创建MarkdownUtils工具类;
MarkdownUtils中添加:
public class MarkdownUtils {
/**
* markdown格式转换成HTML格式
* @param markdown
* @return
*/
public static String markdownToHtml(String markdown) {
Parser parser = Parser.builder().build();
Node document = parser.parse(markdown);
HtmlRenderer renderer = HtmlRenderer.builder().build();
return renderer.render(document);
}
/**
* 增加扩展[标题锚点,表格生成]
* Markdown转换成HTML
* @param markdown
* @return
*/
public static String markdownToHtmlExtensions(String markdown) {
//h标题生成id
Set<Extension> headingAnchorExtensions = Collections.singleton(HeadingAnchorExtension.create());
//转换table的HTML
List<Extension> tableExtension = Arrays.asList(TablesExtension.create());
Parser parser = Parser.builder()
.extensions(tableExtension)
.build();
Node document = parser.parse(markdown);
HtmlRenderer renderer = HtmlRenderer.builder()
.extensions(headingAnchorExtensions)
.extensions(tableExtension)
.attributeProviderFactory(new AttributeProviderFactory() {
public AttributeProvider create(AttributeProviderContext context) {
return new CustomAttributeProvider();
}
})
.build();
return renderer.render(document);
}
/**
* 处理标签的属性
*/
static class CustomAttributeProvider implements AttributeProvider {
@Override
public void setAttributes(Node node, String tagName, Map<String, String> attributes) {
//改变a标签的target属性为_blank
if (node instanceof Link) {
attributes.put("target", "_blank");
}
if (node instanceof TableBlock) {
attributes.put("class", "ui celled table");
}
}
}
public static void main(String[] args) {
String table = "| hello | hi | 哈哈哈 |\n" +
"| ----- | ---- | ----- |\n" +
"| 斯维尔多 | 士大夫 | f啊 |\n" +
"| 阿什顿发 | 非固定杆 | 撒阿什顿发 |\n" +
"\n";
String a = "[imCoding 爱编程](http://www.lirenmi.cn)";
System.out.println(markdownToHtmlExtensions(a));
}
}
2.修改NewService接口;
NewServicer中添加:
//主页显示列表
News getAndConvert(Long id);
3.修改NewServiceImpl类;
NewServiceImpl中添加:
@Override
public News getAndConvert(Long id) {
News news = newRepository.findById(id).orElse(null);
if (news == null){
System.out.println("该新闻不存在");
}
News news1 = new News();
BeanUtils.copyProperties(news,news1);
System.out.println("转换前:"+news1.getContent());
String content = news1.getContent();
news1.setContent(MarkdownUtils.markdownToHtmlExtensions(content));
System.out.println("转换后:"+news1.getContent());
return news1;
}
4.修改IndexControllerr类;
IndexController中添加:
@RequestMapping("/news/{id}")
public String news(@PathVariable Long id,Model model){
model.addAttribute("news",newService.getAndConvert(id));
return "new";
}
Tips
解决IDEA中创建的数据库表名报红线问题;
1.点击Database;
2.点击+号添加使用对应的数据库;
3.Drivers→MySQL→点击+号→Custom Jars…找到如下路径下的仓库中以及下好的对应mysql版本的connector.jar添加;
4.编辑好User、Password、Database,后点击Test Connection,显示连接成功→点击Ok;
5.即配置好数据源,且可在IDEA中可视化数据库及表;
6.找到报红的数据库表名,按Alt+Enter,选择Assign Data Source
7.实体管理工厂,右侧添加配置好的数据源即可;