[Spring Boot practical tutorial] Spring Boot + Vue + mobile terminal project development practical tutorial (XII. Implementation of notification and announcement function)

preface

We have basically built the basic things before. Now what is left is the addition, deletion, modification and query of some business logic and data. We can write it once rather than ten times. The tutorial has been out for a month. There are still fans chasing after writing projects. In the future, you will find that they are basically used in the company. They are basically similar operations, except for the architecture of some projects, Complex logic, threads and so on, but at the beginning, we still have to learn to walk and then run. We hope to learn together. When we graduate, we can directly use it to do the graduation project. Of course, we can discuss the problems related to the graduation project by private letter. Well, let's continue next.

1, bug modification

The name of two fields in the notice notice we designed earlier is not standardized. Let's adjust it now

 `notice_content`      text                NULL                COMMENT 'Announcement content',
 `create_by`   		   VARCHAR(128)    NOT NULL                COMMENT 'creator',

2, Function realization of notification and announcement

This is basically the same as the previous two function modules. They are both added, deleted, modified and checked. If there are any functions to be added later, first design the basic framework, so this can be written in accordance with the previous one. Don't look at my one, think about writing it yourself, and then compare it with me. Slowly, you will get through this road.
I don't want to go into too much detail here. It's basically the code of addition, deletion, modification and query.

1. Add entity class

Create a new entity class: Notice.java

package com.blog.personalblog.entity;

import lombok.Data;

import java.time.LocalDateTime;

/**
 * Notice announcement
 */

@Data
public class Notice {

    /**
     * Primary key
     */
    private int noticeId;

    /**
     * Announcement title
     */
    private String noticeTitle;

    /**
     * Announcement type, default 0, 0-announcement, 1-notification, 2-reminder
     */
    private int noticeType;

    /**
     * Status, default 0, 0-normal, 1-Off
     */
    private int status;

    /**
     * Announcement content
     */
    private String noticeContent;

    /**
     * creator
     */
    private String create_by;

    /**
     * Creation time
     */
    private LocalDateTime createTime;

    /**
     * Update time
     */
    private LocalDateTime updateTime;

}

2. Add business interface

Create a new interface: 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 {
    /**
     * Get all categories (pagination)
     * @return
     */
    List<Notice> getNoticePage(PageRequest pageRequest);

    /**
     * New classification
     * @param notice
     * @return
     */
    int saveNotice(Notice notice);

    /**
     * Modify classification
     * @param notice
     * @return
     */
    int updateNotice(Notice notice);

    /**
     * Delete classification
     * @param noticeId
     */
    void deleteNotice(Integer noticeId);

}

3. Add business interface implementation class

Implementation class: 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. Implementation of database query interface

Create a Mapper interface: 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 {

    /**
     * establish
     * @param notice
     * @return
     */
    int createNotice(Notice notice);

    /**
     * modify
     * @param notice
     * @return
     */
    int updateNotice(Notice notice);

    /**
     * Classification list (pagination)
     * @return
     */
    List<Notice> getNoticePage();

    /**
     * delete
     * @param id
     */
    void deleteNotice(Integer id);

}

5. Writing database xml

Create a new database 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. Write interface layer

Write the controller layer class: 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;

/**
 * Announcement management interface
 *
 * @author: SuperMan
 * @create: 2021-11-23
 */
@Api(tags = "Announcement management")
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    NoticeService noticeService;

    /**
     * Paging query list
     * @param pageRequest
     * @return
     */
    @ApiOperation(value = "Announcement list")
    @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);
    }

    /**
     * Add announcement
     * @return
     */
    @ApiOperation(value = "Add announcement")
    @PostMapping("/create")
    public JsonResult<Object> categoryCreate(@RequestBody @Valid Notice notice) {
        int isStatus = noticeService.saveNotice(notice);
        if (isStatus == 0) {
            return JsonResult.error("Failed to add announcement");
        }
        return JsonResult.success();
    }

    /**
     * Modification announcement
     * @return
     */
    @ApiOperation(value = "Modification announcement")
    @PostMapping("/update")
    public JsonResult<Object> categoryUpdate(@RequestBody @Valid Notice notice) {
        int isStatus = noticeService.updateNotice(notice);
        if (isStatus == 0) {
            return JsonResult.error("Failed to modify announcement");
        }
        return JsonResult.success();
    }

    /**
     * delete
     * @return
     */
    @ApiOperation(value = "Delete announcement")
    @PostMapping("/delete/{id}")
    public JsonResult<Object> categoryDelete(@PathVariable(value = "id") int id) {
        noticeService.deleteNotice(id);
        return JsonResult.success();
    }

}

The above is the basic service of notification and announcement. We will adjust the code appropriately when the front-end and back-end joint commissioning is carried out.

3, Collect suggestions

What do you think of such a detailed writing? Is there anything else to improve? Try to find out every knowledge point. You still need to start from Hello World, write Java, write HTML, or learn technology in the project. I feel that the latter is much better than the former. You are welcome to comment in the comment area and solicit opinions. This tutorial is getting cold!

Previous: Spring Boot + Vue + mobile terminal project development practical tutorial (XI. Implementation of article classification function)
Next: Spring Boot + Vue + mobile terminal project development practical tutorial (XIII. Implementation of article tag function)

Keywords: Spring Boot Vue.js

Added by rockindano30 on Sat, 27 Nov 2021 01:14:01 +0200