springboot项目记录01

一、项目搭建

1.数据库准备

1.数据库创建 create database bolg

2.导入blog.sql

source d:/blog.sql

2.springboot项目准备

1.创建springboot项目,勾选springbootweb,lombok,mysql,jdbcAPI,mysql Driver

2.导入mybatis和其他依赖

<dependency>
           <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.21</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

3.完善包结构

com.example.config

com.example.controller

com.example.dao

com.example.pojo

com.example.service

4.编写application.yaml

spring:
  datasource:
    username: root
    password: 123456
    #?serverTimezone=UTC解决时区的报错
    url: jdbc:mysql://localhost:3306/blog?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

    #Spring Boot 默认是不注入这些属性值的,需要自己绑定
    #druid 数据源专有配置
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true

    #配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
    #如果允许时报错  java.lang.ClassNotFoundException: org.apache.log4j.Priority
    #则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

5.druid的配置

package com.example.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class DruidConfig {
    /*
       将自定义的 Druid数据源添加到容器中,不再让 Spring Boot 自动创建
       绑定全局配置文件中的 druid 数据源属性到 com.alibaba.druid.pool.DruidDataSource从而让它们生效
       @ConfigurationProperties(prefix = "spring.datasource"):作用就是将 全局配置文件中
       前缀为 spring.datasource的属性值注入到 com.alibaba.druid.pool.DruidDataSource 的同名参数中
     */
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource() {
        return new DruidDataSource();
    }
}

6.测试数据源有没有正常导入

package com.example;

import com.alibaba.druid.pool.DruidDataSource;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.*;

@SpringBootTest
class SpringbootBlogApplicationTests {

    //DI注入数据源
    @Autowired
    DataSource dataSource;
    @Test
    void contextLoads() throws SQLException {
        //看一下默认数据源
        System.out.println(dataSource.getClass());
        //获得连接
        Connection connection =   dataSource.getConnection();
        System.out.println(connection);

        DruidDataSource druidDataSource = (DruidDataSource) dataSource;
        System.out.println("druidDataSource 数据源最大连接数:" + druidDataSource.getMaxActive());
        System.out.println("druidDataSource 数据源初始化连接数:" + druidDataSource.getInitialSize());

        //关闭连接
        connection.close();
    }

}

看能否正常输出

3.测试

由于tag表比较简单,所有用tag进行测试

配置资源导出

很重要

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.yaml</include>
                    <include>**/*.properties</include>
                </includes>
                <filtering>false</filtering>
            </resource>

            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.yaml</include>
                    <include>**/*.properties</include>
                </includes>
                <filtering>false</filtering>
            </resource>

        </resources>
    </build>

从底层开始写

dao层

TagMapper.java

package com.example.dao;
import com.example.pojo.Tag;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
//@Mapper : 表示本类是一个 MyBatis 的 Mapper
@Mapper
@Repository
public interface TagMapper {
    // 获得所有
    List<Tag> getTags();
    // 根据id获取
    Tag getTagById(long id);
}

TagMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.example.dao.TagMapper">

    <select id="getTags" resultType="com.example.pojo.Tag">
       select * from ms_tag;
    </select>

    <select id="getTagById" resultType="com.example.pojo.Tag" parameterType="long">
       select * from ms_tag where id = #{id};
    </select>
</mapper>
service层

TagService.java

package com.example.service;
import com.example.pojo.Tag;
import java.util.List;
public interface TagService {
    // 获得所有
    List<Tag> getTags();
    // 根据id获取
    Tag getTagById(long id);
}

TagServiceImpl.java

package com.example.service;
import com.example.dao.TagMapper;
import com.example.pojo.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class TagServiceImpl implements TagService {

    @Autowired
    TagMapper tagMapper;

    @Override
    public List<Tag> getTags() {
        return tagMapper.getTags();
    }

    @Override
    public Tag getTagById(long id) {
        return tagMapper.getTagById(id);
    }
}
controller层

TagController.java

package com.example.controller;
import com.example.dao.TagMapper;
import com.example.pojo.Tag;
import com.example.service.TagService;
import com.example.service.TagServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class TagController {
    @Autowired
    TagService tagService;
//    TagMapper tagMapper;
    @GetMapping("/t1")
    public String testTag(){
//        List<Tag> tags = tagService.getTags();
        Tag tag = tagService.getTagById(1);
        return tag.toString();

    }
}
测试

然后浏览器访问 losthost:8080/t1

查看能否正常访问

更改端口号(8080是前端项目的端口)

开启别名支持(xml文件返回值类型全写全称太麻烦)

在application.yaml中配置

server:
  port: 8888
mybatis:
  type-aliases-package: com.example.pojo

二、首页

articles

1.根据接口文档分析

请求地址:/articles

请求方式:POST

请求参数:

参数名称参数类型说明
pageint当前页数
pageSizeint每页显示的数量

返回数据

{
    "success": true,
    "code": 200,
    "msg": "success",
    "data": []
}

新建vo包结构

com.example.vo

vo包下是返回给前端数据的一些实体类

返回给前端的数据和直接从数据库里获得的数据是有一些区别的,所有要进行一些处理(在service层)

返回结果类Result

Result.java

package com.example.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {
    private boolean success;
    private int code;
    private String msg;
    private Object data;
    /*
    * 成功结果集
    * */
    public static Result success(Object data){
        return new Result(true,200,"success",data);
    }
    /*
     * 失败结果集
     * */
    public static Result fail(int code,String msg){
        return new Result(false,code,msg,null);
    }
}

说明:

由于文章表关联了作者和标签。因此需要不同表的查询数据汇合。

1.dao层
public interface ArticleMapper {
    // 分页查询
    List<Article> selectAndLimit(int page,int pageSize);
}
public interface SysUserMapper {
    // 根据id查询用户
    SysUser findById(Long id);
}
public interface TagMapper {
    // 通过文章id找tag
    List<Tag> getTagByArticleId(Long id);
}
<mapper namespace="com.example.dao.ArticleMapper">
    <select id="selectAndLimit" resultType="Article">
        select * from  ms_article
        limit #{page},#{pageSize}
    </select>
</mapper>


<mapper namespace="com.example.dao.SysUserMapper">
    <select id="findById" resultType="SysUser" parameterType="long">
        select * from ms_sys_user
        where id = #{id}
    </select>
</mapper>

<mapper namespace="com.example.dao.TagMapper">
    <select id="getTagByArticleId" resultType="Tag" parameterType="long">
        select * from ms_tag
        <where>
            id = #{id}
        </where>
    </select>
</mapper>
2.service层

调用dao层获得数据的同时,将数据库pojo类转化为前端展示的vo类

public interface ArticleService {
    // 分页查询
    List<ArticleVo> selectAndLimit(int page, int pageSize);
}
public interface SysUserService {
    SysUser findById(Long id);
}
public interface TagService {
    List<TagVo> getTagByArticleId(Long id);
}

实现类


@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    TagService tagService;
    @Autowired
    SysUserService sysUserService;

    @Override
    public List<ArticleVo> selectAndLimit(int page, int pageSize) {

        List<Article> articleList = articleMapper.selectAndLimit(page, pageSize);
        List<ArticleVo> articleVoList = copyList(articleList,true,true);
        return articleVoList;
    }

    // 将数据库的字段转换为前端展示的字段
    public ArticleVo copy(Article article,boolean isAuthor,boolean isTags){
        ArticleVo articleVo = new ArticleVo();
        // 相同属性进行赋值
        BeanUtils.copyProperties(article,articleVo);
        // Article实体类中作者,tag这些都是id,要转为具体的值
        // 需要携带作者信息
        if(isAuthor){

            // 用文章结果携带的作者id
            SysUser sysUser = sysUserService.findById(article.getAuthorId());
            articleVo.setAuthor(sysUser.getNickname());
        }
        //articleVo.setCreateDate(new DateTime(article.getCreateDate()).toString("yyyy-MM-dd HH:mm"));
        articleVo.setCreateDate(new Date(article.getCreateDate()).toString());
        if(isTags){
            List<TagVo> tagVoList = tagService.getTagByArticleId(article.getId());
            articleVo.setTags(tagVoList);
        }
        return articleVo;
    }

    //
    public List<ArticleVo> copyList(List<Article> articleList,boolean isAuthor,boolean isTags){
        List<ArticleVo> articleVoList =new ArrayList();
        for (Article article : articleList) {
            ArticleVo articleVo = copy(article,isAuthor,isTags);
            articleVoList.add(articleVo);
        }
        return articleVoList;
    }
}
@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    SysUserMapper sysUserMapper;

    @Override
    public SysUser findById(Long id) {
        SysUser sysUser = sysUserMapper.findById(id);
        if(sysUser==null){
            sysUser = new SysUser();
            sysUser.setNickname("blog");
        }
        return sysUser;
    }
}
@Service
public class TagServiceImpl implements TagService {
    @Autowired
    TagMapper tagMapper;
    @Override
    public List<TagVo> getTagByArticleId(Long id) {
        List<Tag> tagList = tagMapper.getTagByArticleId(id);
        List<TagVo> tagVos = copyList(tagList);
        return tagVos;
    }

    // 数据库实体类 与  前端展示数据的转化
    public TagVo copy(Tag tag){
        TagVo tagVo = new TagVo();
        tagVo.setId(tag.getId().toString());
        tagVo.setAvatar(tag.getAvatar());
        tagVo.setTagName(tag.getTagName());
        return tagVo;
    }
    public List<TagVo> copyList(List<Tag> tagList){
        List<TagVo> tagVoList = new ArrayList<>();
        for (Tag tag : tagList) {
            TagVo tagVo = copy(tag);
            tagVoList.add(tagVo);
        }
        return tagVoList;
    }
}
3.controller层
@RestController
public class ArticleController {
    @Autowired
    ArticleService articleService;
    @PostMapping("/articles")
    public Result articles(@RequestBody PageParams pageParams){
//        System.out.println(pageParams.getPage()+"---"+pageParams.getPageSize());
        List<ArticleVo> articleVoList = articleService.selectAndLimit(pageParams.getPage(), pageParams.getPageSize());
        return Result.success(articleVoList);
    }
}

pagParams类 用来接收post请求提交的参数。

@Data
public class PageParams {
    private int page;
    private int pageSize = 10;
}

articles/hot

post请求,没有携带参数

data 里为id 和title

按viewCounts数前五进行展示

articles/new

post请求,没有携带参数

data 里为id 和title

按创建时间前五进行展示

articles/listArchives

post请求,没有携带参数

data {year,moth,count}

新的实体类

public class Archives {
    private Integer year;
    private Integer month;
    private Integer count;
}

dao

这几个都是差不多的,

先在dao层编mapper和mapper.xml

    <select id="selectByHot" resultType="Article">
        select * from ms_article
        order by view_counts
        desc
        limit #{limit}
    </select>
    <select id="selectByNew" resultType="Article">
        select * from ms_article
        order by create_date
        desc
        limit #{limit}
    </select>
    <select id="selectArchives" resultType="Archives">
        select year(create_date) as year,
        month(create_date) as month,
        count(*) as count
        from ms_article
        group by
        year,month
    </select>

service

    @Override
    public Result selectByHot(int limit) {
        List<Article> list = articleMapper.selectByHot(limit);
        List<ArticleVo> articleVoList = copyList(list, false, false);
        return Result.success(articleVoList);
    }

    @Override
    public Result selectByNew(int limit) {
        List<Article> articleList = articleMapper.selectByNew(limit);
        return Result.success(copyList(articleList,false,false));
    }

    @Override
    public Result selectArchives() {
        List<Archives> archivesList = articleMapper.selectArchives();

        return Result.success(archivesList);
    }

controller

    @PostMapping("/articles/hot")
    public Result articlesHot(){
        int limit = 5;
        return articleService.selectByHot(limit);
    }
    @PostMapping("/articles/new")
    public Result articlesNew(){
        int limit = 5;
        return articleService.selectByNew(limit);
    }
    @PostMapping("/articles/listArchives")
    public Result listArchives(){
        return articleService.selectArchives();
    }

三、登录

分析:

1.post请求,携带参数account,password, /login

2.返回data 是一个token

说明:

由于数据库中本身存在的密码就是加密了的,可能会因为加密盐不同而查不出结果。所以测试的时候可以只去查用户名。
或者自己添加修改密码功能,把密码改成自己的加密盐加密后的密码
1.使用token ,需要导入jwt的依赖
2.需要一个创建token和解析token的工具类
3.接收携带参数需要一个参数实体类接收
4.处理业务(判断账户名密码正确性)要在service层,所以需要loginService的抽象类和实现类
5.用户信息在SysUser表中,所有在SysUserMapper中添加根据用户名获得用户的方法
6.密码是经过加密后存在数据库中的,所以需要对用户输入的密码进行加密。用到md5加密,需要导入依赖。需要json处理的依赖

1.导入依赖

        <!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt -->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.1</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.76</version>
        </dependency>

2.工具类和参数类

package com.example.utils;
import io.jsonwebtoken.Jwt;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class JWTUtil {
    private static final String jwtToken = "123456@###$$";

    // 获得token
    public static String createToken(Long userId){
        Map<String,Object> claims = new HashMap<>();
        claims.put("userId",userId);
        JwtBuilder jwtBuilder = Jwts.builder()
                .signWith(SignatureAlgorithm.HS256, jwtToken) // 签发算法,秘钥为jwtToken
                .setClaims(claims) // body数据,要唯一,自行设置
                .setIssuedAt(new Date()) // 设置签发时间
                .setExpiration(new Date(System.currentTimeMillis() + 24 * 60 * 60 * 60 * 1000));// 一天的有效时间
        String token = jwtBuilder.compact();
        return token;

    }

    // 解析token
    public static Map<String,Object> checkToken(String token){
        try {
            Jwt parse = Jwts.parser().setSigningKey(jwtToken).parse(token);
            return (Map<String, Object>) parse.getBody();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
package com.example.vo.Params;
import lombok.Data;
@Data
public class LoginParams {
    private String account;
    private String password;

}

3.dao层

 // 根据用户名和密码查询用户
SysUser findByNameAndPwd(@Param("account") String account,@Param("password") String password);
    <select id="findByNameAndPwd" resultType="SysUser" parameterType="string">
        select * from ms_sys_user
        where account = #{account}
        and password=#{password}
    </select>
说明: 第一次查询时可以先不带密码,只根据用户名查询

4.service

SysUser findByNameAndPwd(String account,String password);
    @Override
    public SysUser findByNameAndPwd(String account, String password) {
        SysUser sysUser = sysUserMapper.findByNameAndPwd(account, password);
        return sysUser;
    }

5.编写login的service

package com.example.service;
import com.example.vo.Result;
public interface LoginService {
    Result findByNameAndPwd(String account,String password);
}
package com.example.service;
import com.example.dao.SysUserMapper;
import com.example.pojo.SysUser;
import com.example.utils.JWTUtil;
import com.example.vo.Result;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class LoginServiceImpl implements LoginService{
    // 加密盐
    private static final String slat = "@###";
    @Autowired
    private SysUserService sysUserService;
    @Override
    public Result findByNameAndPwd(String account, String password) {
        // 对参数进行 核验
        if(account=="" || password ==""){
            return Result.fail(10001,"参数错误");
        }
        // 对密码进行加密
        password = DigestUtils.md5Hex(password+slat);
        // 从数据库中获取
        SysUser sysUser = sysUserService.findByNameAndPwd(account, password);
        // 为空 说明没有相匹配的用户
        if(sysUser==null){
            return Result.fail(100002,"账号或密码错误");
        }
        // 登录成功 返回token
        String token = JWTUtil.createToken(sysUser.getId());
        return Result.success(token);
    }
}

6.controller

package com.example.controller;
import com.example.service.LoginService;
import com.example.vo.Params.LoginParams;
import com.example.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;
    @PostMapping("/login")
    public Result login(@RequestBody LoginParams loginParams){
        String account = loginParams.getAccount();
        String password = loginParams.getPassword();
        return loginService.findByNameAndPwd(account,password);
    }
}

7.测试

使用postMan软件,点击body下的raw 选择json格式

输入

{
	"account":"admin",
	"password":"admin"
}

四、文章详情

分析

/articles/view/{id}
post
返回数据有文章的详细信息,包括标签,分类,详情
多表管理。
`ms_article_body`文章内容表,根据article_id查询
`ms_category`
ms_article_tag表中有article_id 和tag_id的对应信息

准备

实体类

@Data
public class ArticleBody {
    private Long id;
    private String content;
    private String contentHtml;
    private Long articleId;
}
@Data
public class Category {
    private Long id;
    private String avatar;
    private String categoryName;
    private String description;
}

vo类

@Data
public class CategoryVo {
    private Long id;
    private String avatar;
    private String categoryName;
}
@Data
public class ArticleBodyVo {
    private String content;
}

articleVo中新增属性ArticleBodyVo,CategoryVo

    private ArticleBodyVo body;
    private CategoryVo categorys;

编写ArticleBodyMapper和CategoryMapper以及相应的实现类

@Mapper
@Repository
public interface ArticleBodyMapper {
    ArticleBody findById(Long id);
}

@Mapper
@Repository
public interface CategoryMapper {
    Category findById(Long id);
}
<mapper namespace="com.example.dao.ArticleBodyMapper">
    <select id="findById" resultType="ArticleBody" parameterType="long">
    select * from ms_article_body
    where id = #{id}
</select>
</mapper>
<mapper namespace="com.example.dao.CategoryMapper">
<select id="findById" resultType="Category" parameterType="long">
    select * from ms_category
    where id = #{id}
</select>

</mapper>

articleMapper中新增一个根据id查询的方法(代码我不放了,很简单)

service

public interface ArticleBodyService {
    ArticleBodyVo findById(Long id);
}
@Service
public class ArticleBodyServiceImpl implements ArticleBodyService{
    @Autowired
    ArticleBodyMapper articleBodyMapper;
    @Override
    public ArticleBodyVo findById(Long id) {
        ArticleBody articleBody = articleBodyMapper.findById(id);
        ArticleBodyVo articleBodyVo = new ArticleBodyVo();
        articleBodyVo.setContent(articleBody.getContent());
        return articleBodyVo;
    }
}
public interface CategoryService {
    CategoryVo findById(Long id);
}
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;
    @Override
    public CategoryVo findById(Long id) {
        Category category = categoryMapper.findById(id);
        CategoryVo categoryVo = new CategoryVo();
        categoryVo.setId(category.getId());
        categoryVo.setAvatar(category.getAvatar());
        categoryVo.setCategoryName(category.getCategoryName());
        return categoryVo;
    }
}

controller

articleController这个类

    @PostMapping("/articles/view/{id}")
    public Result view(@PathVariable("id") Long id){
        ArticleVo articleVo = articleService.findArticleById(id);
        return Result.success(articleVo);
    }

articleSerivce

ArticleVo findArticleById(Long id);
public ArticleVo findArticleById(Long id) {
     Article article = articleMapper.findArticleById(id);
     return copy(article,true,true,true,true);
}

对之前的copy函数和copylist函数稍稍的修改一下就行

public ArticleVo copy(Article article,boolean isAuthor,boolean isTags,boolean isBody, boolean isCategory){
        ArticleVo articleVo = new ArticleVo();
        // 相同属性进行赋值
//        BeanUtils.copyProperties(article,articleVo);
        articleVo.setId(article.getId());
        articleVo.setCommentCounts(article.getCommentCounts());
        articleVo.setSummary(article.getSummary());
        articleVo.setTitle(article.getTitle());
        articleVo.setViewCounts(article.getViewCounts());
        articleVo.setWeight(article.getWeight());


        // Article实体类中作者,tag这些都是id,要转为具体的值
        // 需要携带作者信息
        if(isAuthor){

            // 用文章结果携带的作者id
            SysUser sysUser = sysUserService.findById(article.getAuthorId());
            articleVo.setAuthor(sysUser.getNickname());
        }
        //articleVo.setCreateDate(new DateTime(article.getCreateDate()).toString("yyyy-MM-dd HH:mm"));
        articleVo.setCreateDate(new Date(article.getCreateDate()).toString());
        // 根据不同的情况把信息装在vo对象中
        if(isTags){
            List<TagVo> tagVoList = tagService.getTagByArticleId(article.getId());
            articleVo.setTags(tagVoList);
        }
        if(isBody){
            ArticleBodyVo articleBodyVo = articleBodyService.findById(article.getBodyId());
            articleVo.setBody(articleBodyVo);
        }
        if(isCategory){
            CategoryVo categoryVo = categoryService.findById(article.getCategoryId());
            articleVo.setCategorys(categoryVo);
        }
        return articleVo;
    }
 public List<ArticleVo> copyList(List<Article> articleList,boolean isAuthor,boolean isTags){
        List<ArticleVo> articleVoList =new ArrayList();
        for (Article article : articleList) {
            ArticleVo articleVo = copy(article,isAuthor,isTags,false,false);
            articleVoList.add(articleVo);
        }
        return articleVoList;
    }
    public List<ArticleVo> copyList(List<Article> articleList,boolean isAuthor,boolean isTags,boolean isBody, boolean isCategory){
        List<ArticleVo> articleVoList =new ArrayList();
        for (Article article : articleList) {
            ArticleVo articleVo = copy(article,isAuthor,isTags,isBody,isCategory);
            articleVoList.add(articleVo);
        }
        return articleVoList;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值