8.查看文章详情

查看文章详情

1. 需求分析

image-20221127160127657

2. 选择的实现方案

image-20221127160233576

3. 实现步骤

3.1.在artile微服务中添加MinIO和freemarker的支持

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>
    <dependency>
        <groupId>com.heima</groupId>
        <artifactId>heima-file-starter</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>

3. 2 模板文件(article.ftl)拷贝到article微服务下(课程资料,篇幅过长,不宜粘贴)

3.3 index.js和index.css两个文件手动上传到MinIO中(课程资料有)

3.4 新建ApArticleContentMapper

package com.heima.article.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.heima.model.article.pojos.ApArticleContent;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface ApArticleContentMapper extends BaseMapper<ApArticleContent> {
}

6.在artile微服务中新增测试类(后期新增文章的时候创建详情静态页,目前暂时手动生成)

package co.heima.test;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.article.ArticleApplication;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.service.ApArticleService;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleContent;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ArticleApplication.class)
public class ArticleFreemarkTest {


    @Autowired
    private ApArticleContentMapper apArticleContentMapper;


    @Autowired
    private ApArticleService apArticleService;
   
    @Autowired
    private Configuration configuration;

    @Autowired
    private FileStorageService fileStorageService;


    @Test
    public  void test() throws  Exception {
        //1.先根据文章id查询文章内容
        LambdaQueryWrapper<ApArticleContent> lwq = new LambdaQueryWrapper<>();
        lwq.eq(ApArticleContent::getArticleId, "1302864436297482242");

        ApArticleContent articleContent = apArticleContentMapper.selectOne(lwq);
        
        //2.如果文章内容对象与文章内容都不为空,用freemark模板为文章内容生成html文件
        if(articleContent != null && StringUtils.isNotBlank(articleContent.getContent())){
     
            Template template = configuration.getTemplate("article.ftl");//找到模板
           
            Map<String,Object> map = new HashMap<>();
            map.put(" content", JSON.parseArray(articleContent.getContent()));
           
            StringWriter out = new StringWriter();
            
            template.process(map,out);

            //3.把生成的html文件上传至minio
            InputStream is = new ByteArrayInputStream(out.toString().getBytes(StandardCharsets.UTF_8));
            String static_url  = fileStorageService.uploadHtmlFile("", articleContent.getArticleId() + ".html", is);

           //4.上传成功,修改文章表的静态页面路径字段
            LambdaUpdateWrapper<ApArticle> luw = new LambdaUpdateWrapper<>();
            luw.eq(ApArticle::getId, articleContent.getArticleId())
                .set(ApArticle::getStaticUrl,static_url);
                
            apArticleService.update(luw);
        }


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糖分你俩颗~~~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值