学习进度条

今日所花时间:三小时
今日代码量:300行
博客量:一篇 继续完成springboot3+vue3的项目练习
了解到的知识点:分页查询 使用映射配置文件实现动态查询 动态sql编写 使用PageHelper 使用工具类PageBean 文件上传(UUID解决文件覆盖问题)
相关的测试代码:

Acticle实体类

package com.example.day0225.pojo;



import com.example.day0225.anno.State;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import org.hibernate.validator.constraints.URL;

import java.time.LocalDateTime;

public class Article {
    private Integer id;//主键ID
    @NotEmpty
    @Pattern(regexp = "^\\S{1,10}$")
    private String title;//文章标题
    @NotEmpty
    private String content;//文章内容
    @NotEmpty
    @URL
    private String coverImg;//封面图像
    @State
    private String state;//发布状态 已发布|草稿
    @NotNull
    private Integer categoryId;//文章分类id
    private Integer createUser;//创建人ID
    private LocalDateTime createTime;//创建时间
    private LocalDateTime updateTime;//更新时间

    public Article() {
    }

    public Article(Integer id, String title, String content, String coverImg, String state, Integer categoryId, Integer createUser, LocalDateTime createTime, LocalDateTime updateTime) {
        this.id = id;
        this.title = title;
        this.content = content;
        this.coverImg = coverImg;
        this.state = state;
        this.categoryId = categoryId;
        this.createUser = createUser;
        this.createTime = createTime;
        this.updateTime = updateTime;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getCoverImg() {
        return coverImg;
    }

    public void setCoverImg(String coverImg) {
        this.coverImg = coverImg;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public Integer getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Integer categoryId) {
        this.categoryId = categoryId;
    }

    public Integer getCreateUser() {
        return createUser;
    }

    public void setCreateUser(Integer createUser) {
        this.createUser = createUser;
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public LocalDateTime getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(LocalDateTime updateTime) {
        this.updateTime = updateTime;
    }
}

实体类PageBean

package com.example.day0225.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

//分页返回结果对象
@Data

public class PageBean <T>{
    private Long total;//总条数
    private List<T> items;//当前页数据集合

    public PageBean() {
    }

    public PageBean(Long total, List<T> items) {
        this.total = total;
        this.items = items;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public List<T> getItems() {
        return items;
    }

    public void setItems(List<T> items) {
        this.items = items;
    }
}

ArticleController

package com.example.day0225.controller;

import com.example.day0225.mapper.ArticleMapper;
import com.example.day0225.pojo.Article;
import com.example.day0225.pojo.Category;
import com.example.day0225.pojo.PageBean;
import com.example.day0225.pojo.Result;
import com.example.day0225.service.ArticleService;
import com.example.day0225.utils.JwtUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.coyote.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.HttpServerErrorException;

import java.util.Map;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleMapper articleMapper;

    @PostMapping
    public Result<String> add(@RequestBody @Validated Article article) {
        articleService.add(article);
        return Result.success();
    }
    @DeleteMapping
    public Result<String> delete(@RequestParam Integer id) {
        articleService.delete(id);
        return Result.success();
    }
    @GetMapping("/detail")
    public Result<Article> detail(Integer id) {
        Article a = articleService.findById(id);
        return Result.success(a);
    }
    @GetMapping
    public Result<PageBean<Article>> list(Integer pageNum,
                                          Integer pageSize,
                                         @RequestParam(required = false)Integer categoryId,
                                         @RequestParam(required = false) String state){
        PageBean<Article>  pb = articleService.list(pageNum,pageSize,categoryId,state);
        return Result.success(pb);
    }
    @PutMapping
    public Result<String> update(@RequestBody @Validated Article article) {
        articleService.update(article);
        return Result.success();
    }
}


ArticleService

package com.example.day0225.service;

import com.example.day0225.pojo.Article;
import com.example.day0225.pojo.PageBean;

public interface ArticleService {
    //新增文章
    void add(Article article);
    //条件分类列表查询
    PageBean<Article> list(Integer pageNum, Integer pageSize, Integer categoryId, String state);
    //删除文章
    void delete(Integer id);
    //更新文章
    void update(Article article);
    //获取文章详细信息
    Article findById(Integer id);
}


ArticleServiceImpl

package com.example.day0225.service.impl;

import com.example.day0225.mapper.ArticleMapper;
import com.example.day0225.pojo.Article;
import com.example.day0225.pojo.Category;
import com.example.day0225.pojo.PageBean;
import com.example.day0225.service.ArticleService;
import com.example.day0225.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public void add(Article article) {
        //补充属性值
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        article.setCreateUser(userId);
        articleMapper.add(article);
    }

    @Override
    public PageBean<Article> list(Integer pageNum, Integer pageSize, Integer categoryId, String state) {
        //创建PageBean对象
        PageBean<Article> pb = new PageBean<>();
        //开始分页查询PageHelper
        PageHelper.startPage(pageNum, pageSize);
        //调用mapper完成查询
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        List<Article> as = articleMapper.list(userId,categoryId, state);
        //Page中提供了方法,可以获取PageHelper分页查询后,得到的总记录条数和当前页数据
        Page<Article> p = (Page<Article>) as;
        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public void delete(Integer id) {
        articleMapper.delete(id);
    }


    @Override
    public void update(Article article) {
        article.setUpdateTime(LocalDateTime.now()); // 设置更新时间
        articleMapper.update(article);
    }

    @Override
    public Article findById(Integer id) {
        Article a = articleMapper.findById(id);
        return a;
    }
}


ArticleMapper

package com.example.day0225.mapper;

import com.example.day0225.pojo.Article;
import com.example.day0225.pojo.Category;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ArticleMapper {
    // 新增
    @Insert("insert into article(title, content, cover_img, state, category_id, create_user, create_time, update_time) " +
            "VALUES (#{title},#{content},#{coverImg},#{state},#{categoryId},#{createUser},#{createTime},#{updateTime})")
    void add(Article article);
    //使用映射配置文件实现动态查询
    List<Article> list(Integer userId, Integer categoryId, String state);
    //删除
    @Delete("delete from article where id = #{id}")
    void delete(Integer id);
    //更新
    @Update("update article set title = #{title}, content = #{content}, cover_img = #{coverImg}, state = #{state}, category_id = #{categoryId}, update_time = #{updateTime} where id = #{id}")
    void update(Article article);
    //查询文章详细信息
    @Select("select * from article where id = #{id}")
    Article findById(Integer id);
}

根据我的项目在resource目录下创建com.example.day0225.mapper目录然后创建ArticleMapper.xml,编写如下动态sql代码

<?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.day0225.mapper.ArticleMapper">
    <!-- 动态 sql -->
    <select id="list" resultType="com.example.day0225.pojo.Article">
        select * from article
        <where>
            <if test="categoryId != null">
                category_id = #{categoryId}
            </if>
            <if test="state != null">
                and state = #{state}
            </if>
            and create_user = #{userId}
        </where>
    </select>
</mapper>

在controller层中添加一个FileUploadController,用来控制文件的上传

package com.example.day0225.controller;

import com.example.day0225.pojo.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class FileUploadController {
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws IOException {
        //把文件的内容存储到本地磁盘上
        String originalFilename = file.getOriginalFilename();
        //保证文件的名称是唯一的(UUID实现),从而防止文件被覆盖
        String filename = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        file.transferTo(new File("E:\\files\\"+filename));
        return Result.success("url访问地址...");
    }
}

其他内容:
令牌主动失效机制
●登录成功后,给浏览器响应令牌的同时,把该令牌存储到redis中
●Loginlnterceptor拦截器中,需要验证浏览器携带的令牌,并同时需要获取到redis中存储的与之相同的令牌
●当用户修改密码成功后,删除redis中存储的旧令牌

1.如何生成jar包?
执行package命令即可
2.如何运行jar包?
Java -jar jarf包位置
3.Jar包部署对服务器有什么要求?

配置优先级(从上到下)
项目中resources目录下的application.yml
Jar包所在目录下的application.yml
操作系统环境变量
命令行参数
必须有jre环境

posted @ 2025-03-08 14:22  haoyinuo  阅读(17)  评论(0)    收藏  举报