Spring Boot + vue-element 开发个人博客项目实战教程(十二、通知公告功能实现)

简介: Spring Boot + vue-element 开发个人博客项目实战教程(十二、通知公告功能实现)

⭐ 作者简介:码上言



⭐ 代表教程:Spring Boot + vue-element 开发个人博客项目实战教程



⭐专栏内容:零基础学Java个人博客系统

项目部署视频

https://www.bilibili.com/video/BV1sg4y1A7Kv/?vd_source=dc7bf298d3c608d281c16239b3f5167b

文章目录

前言

前面我们把基础的东西基本上搭建完了,现在剩下的都是一些业务上的逻辑和数据的增删改查,写一遍不会写十遍就会了,教程也出了一个月了,还是有粉丝在追着写项目的,以后你会发现在公司基本上都会用到,基本上都是类似的操作,当然除了一些项目的架构,复杂的逻辑,线程之类的,但一开始还是要学会走路,再去跑,希望能跟着一起学习,等毕业了直接拿来搞毕业设计,当然有毕业设计相关的问题都可以私信我进行讨论,好啦接下来还是接着干。

一、bug修改

前面我们设计的通知公告中有两个字段的名称不规范,我们现在调整一下

 `notice_content`      text                NULL                COMMENT '公告内容',
 `create_by`         VARCHAR(128)    NOT NULL                COMMENT '创建者',

二、通知公告的功能实现

这个和前面的两个功能模块基本上差不多,都是增删改查,后期如果有什么功能后期再去添加,首先要把基础的框架设计好,所以这个就可以仿照前面的写,你先不要看我的这个,先去自己想着写,然后再去和我的对比,慢慢的你就走通了这条路。

我这里不过多的赘述了,基本上都是增删改查的代码。

1、添加实体类

新建一个实体类:Notice.java

package com.blog.personalblog.entity;
import lombok.Data;
import java.time.LocalDateTime;
/**
 * 通知公告
 */
@Data
public class Notice {
    /**
     * 主键
     */
    private int noticeId;
    /**
     * 公告标题
     */
    private String noticeTitle;
    /**
     * 公告类型,默认0, 0-公告, 1-通知, 2-提醒
     */
    private int noticeType;
    /**
     * 状态,默认0, 0-正常, 1-关闭
     */
    private int status;
    /**
     * 公告内容
     */
    private String noticeContent;
    /**
     * 创建者
     */
    private String create_by;
    /**
     * 创建时间
     */
    private LocalDateTime createTime;
    /**
     * 更新时间
     */
    private LocalDateTime updateTime;
}

2、添加业务接口

新建一个接口:NoticeService.java

package com.blog.personalblog.service;
import com.blog.personalblog.config.page.PageRequest;
import com.blog.personalblog.entity.Notice;
import java.util.List;
/**
 * @author: SuperMan
 * @create: 2021-11-23
 */
public interface NoticeService {
    /**
     * 获取所有的分类(分页)
     * @return
     */
    List<Notice> getNoticePage(PageRequest pageRequest);
    /**
     * 新建分类
     * @param notice
     * @return
     */
    int saveNotice(Notice notice);
    /**
     * 修改分类
     * @param notice
     * @return
     */
    int updateNotice(Notice notice);
    /**
     * 删除分类
     * @param noticeId
     */
    void deleteNotice(Integer noticeId);
}

3、添加业务接口实现类

实现类: NoticeServiceImpl.java

package com.blog.personalblog.service.Impl;
import com.blog.personalblog.config.page.PageRequest;
import com.blog.personalblog.entity.Notice;
import com.blog.personalblog.mapper.NoticeMapper;
import com.blog.personalblog.service.NoticeService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
 * @author: SuperMan
 * @create: 2021-11-23
 */
@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    NoticeMapper noticeMapper;
    @Override
    public List<Notice> getNoticePage(PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum,pageSize);
        List<Notice> noticeList = noticeMapper.getNoticePage();
        return noticeList;
    }
    @Override
    public int saveNotice(Notice notice) {
        return noticeMapper.createNotice(notice);
    }
    @Override
    public int updateNotice(Notice notice) {
        return noticeMapper.updateNotice(notice);
    }
    @Override
    public void deleteNotice(Integer noticeId) {
        noticeMapper.deleteNotice(noticeId);
    }
}

4、数据库查询接口实现

新建一个Mapper接口:NoticeMapper.java

package com.blog.personalblog.mapper;
import com.blog.personalblog.entity.Category;
import com.blog.personalblog.entity.Notice;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
 * @author: SuperMan
 * @create: 2021-11-23
 */
@Repository
public interface NoticeMapper {
    /**
     * 创建
     * @param notice
     * @return
     */
    int createNotice(Notice notice);
    /**
     * 修改
     * @param notice
     * @return
     */
    int updateNotice(Notice notice);
    /**
     * 分类列表(分页)
     * @return
     */
    List<Notice> getNoticePage();
    /**
     * 删除
     * @param id
     */
    void deleteNotice(Integer id);
}

5、编写数据库xml

新建一个数据库xml:NoticeMapper.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.blog.personalblog.mapper.NoticeMapper">
    <resultMap id="BaseResultMap" type="com.blog.personalblog.entity.Notice">
        <result column="notice_id" jdbcType="INTEGER" property="noticeId"/>
        <result column="notice_title" jdbcType="VARCHAR" property="noticeTitle"/>
        <result column="notice_type" jdbcType="INTEGER" property="noticeType"/>
        <result column="status" jdbcType="INTEGER" property="status"/>
        <result column="notice_content" jdbcType="VARCHAR" property="noticeContent"/>
        <result column="create_by" jdbcType="VARCHAR" property="create_by"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
    </resultMap>
    <select id="getNoticePage" resultMap="BaseResultMap">
        select * from person_notice
    </select>
    <insert id="createNotice" parameterType="com.blog.personalblog.entity.Notice" useGeneratedKeys="true" keyProperty="categoryId">
        INSERT INTO person_notice (notice_title, notice_type, status, notice_content, create_by)
        VALUES(#{noticeTitle}, #{noticeType}, #{status}, #{noticeContent}, #{create_by})
    </insert>
    <update id="updateNotice" parameterType="com.blog.personalblog.entity.Notice">
        update person_notice
        <set>
            notice_title = #{noticeTitle},
            notice_type = #{noticeType},
            status = #{status},
            notice_content = #{noticeContent},
            create_by = #{create_by}
        </set>
        WHERE notice_id = #{notice_id}
    </update>
    <delete id="deleteNotice" parameterType="java.lang.Integer">
        delete from person_notice where notice_id = #{noticeId, jdbcType=INTEGER}
    </delete>
</mapper>

6、编写接口层

编写controller层类:NoticeController.java

package com.blog.personalblog.controller;
import com.blog.personalblog.config.page.PageRequest;
import com.blog.personalblog.config.page.PageResult;
import com.blog.personalblog.entity.Category;
import com.blog.personalblog.entity.Notice;
import com.blog.personalblog.service.NoticeService;
import com.blog.personalblog.util.JsonResult;
import com.blog.personalblog.util.PageUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
/**
 * 公告管理接口
 *
 * @author: SuperMan
 * @create: 2021-11-23
 */
@Api(tags = "公告管理")
@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    NoticeService noticeService;
    /**
     * 分页查询列表
     * @param pageRequest
     * @return
     */
    @ApiOperation(value = "公告列表")
    @PostMapping("list")
    public JsonResult<Object> listPage(@RequestBody @Valid PageRequest pageRequest) {
        List<Notice> noticeList = noticeService.getNoticePage(pageRequest);
        PageInfo pageInfo = new PageInfo(noticeList);
        PageResult pageResult = PageUtil.getPageResult(pageRequest, pageInfo);
        return JsonResult.success(pageResult);
    }
    /**
     * 添加公告
     * @return
     */
    @ApiOperation(value = "添加公告")
    @PostMapping("/create")
    public JsonResult<Object> categoryCreate(@RequestBody @Valid Notice notice) {
        int isStatus = noticeService.saveNotice(notice);
        if (isStatus == 0) {
            return JsonResult.error("添加公告失败");
        }
        return JsonResult.success();
    }
    /**
     * 修改公告
     * @return
     */
    @ApiOperation(value = "修改公告")
    @PostMapping("/update")
    public JsonResult<Object> categoryUpdate(@RequestBody @Valid Notice notice) {
        int isStatus = noticeService.updateNotice(notice);
        if (isStatus == 0) {
            return JsonResult.error("修改公告失败");
        }
        return JsonResult.success();
    }
    /**
     * 删除
     * @return
     */
    @ApiOperation(value = "删除公告")
    @PostMapping("/delete/{id}")
    public JsonResult<Object> categoryDelete(@PathVariable(value = "id") int id) {
        noticeService.deleteNotice(id);
        return JsonResult.success();
    }
}

以上就是通知公告的基础服务,等前后端联调的时候我们再适当的调整代码。

三、收集建议

大家觉得这样详细的写怎么样,还有什么需要改进的吗?尽量将每个知识点搞清楚,还是需要从Hello World开始写Java写HTML还是项目中去学习技术,我感觉后者要比前者效果好很多,欢迎评论区提意见,征集意见,眼看这教程要凉了啊!

目录
相关文章
|
1天前
|
存储 JSON 前端开发
利用Spring MVC开发程序2
利用Spring MVC开发程序
10 1
|
1天前
|
设计模式 JSON 前端开发
利用Spring MVC开发程序1
利用Spring MVC开发程序
11 0
|
1天前
|
JavaScript Java 测试技术
基于小程序的网上商城设计+springboot+vue.js附带文章和源代码设计说明文档ppt
基于小程序的网上商城设计+springboot+vue.js附带文章和源代码设计说明文档ppt
22 8
|
1天前
|
JavaScript Java 测试技术
停车场微信小程序+springboot+vue.js附带文章和源代码设计说明文档ppt
停车场微信小程序+springboot+vue.js附带文章和源代码设计说明文档ppt
25 7
|
1天前
|
JavaScript Java 测试技术
基于小程序的家政服务管理系统+springboot+vue.js附带文章和源代码设计说明文档ppt
基于小程序的家政服务管理系统+springboot+vue.js附带文章和源代码设计说明文档ppt
173 59
|
1天前
|
JavaScript Java 测试技术
儿童预防接种预约微信小程序+springboot+vue.js附带文章和源代码设计说明文档ppt
儿童预防接种预约微信小程序+springboot+vue.js附带文章和源代码设计说明文档ppt
17 1
|
1天前
|
JavaScript Java 测试技术
智慧旅游平台开发微信小程序+springboot+vue.js附带文章和源代码设计说明文档ppt
智慧旅游平台开发微信小程序+springboot+vue.js附带文章和源代码设计说明文档ppt
9 0
|
1天前
|
JavaScript Java 测试技术
基于小程序的汽车预约维修系统+springboot+vue.js附带文章和源代码设计说明文档ppt
基于小程序的汽车预约维修系统+springboot+vue.js附带文章和源代码设计说明文档ppt
12 0
|
1天前
|
JavaScript Java 测试技术
基于小程序的家政项目小程序+springboot+vue.js附带文章和源代码设计说明文档ppt
基于小程序的家政项目小程序+springboot+vue.js附带文章和源代码设计说明文档ppt
13 0
|
1天前
|
JavaScript Java 测试技术
基于小程序的大学生闲置物品交易平台+springboot+vue.js附带文章和源代码设计说明文档ppt
基于小程序的大学生闲置物品交易平台+springboot+vue.js附带文章和源代码设计说明文档ppt
15 0

热门文章

最新文章