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

//原先的 
`noticeContent`    text                  NULL                                COMMENT '公告内容',
 `createBy`   		VARCHAR(128)      NOT NULL                                COMMENT '创建者',
//修改成
 `notice_content`   text                  NULL                                COMMENT '公告内容',
 `create_by`   		VARCHAR(128)      NOT NULL                                COMMENT '创建者',

一、通知公告功能的实现

和前面两个功能模块差不多,都是增删改查,

1.1 添加实体类

 

entity包下新建一个Notice.java

Notice.java完整代码

package com.blog.personblog.entity;

import lombok.Data;


import java.time.LocalDateTime;

@Data
public class Notice {

    /**
     * 主键ID
     */
    private int noticeId;

    /**
     * 标题
     */
    private  String noticeTitle;

    /**
     *公告类型,默认0, 0-公告, 1-通知, 2-提醒'
     */
    private int noticeType;

    /**
     * 状态,默认0, 0-正常, 1-关闭
     */
    private int status;

    /**
     * 公告内容
     */
    private String noticeContent;

    /**
     * 创建者
     */
    private String createBy;

    /**
     * 创建时间
     */
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    private LocalDateTime updateTime;
    

}

1.2 添加业务接口

 

 在service层添加业务接口,使得业务暴露出来

添加接口,创建一个NoticeService.java

 NoticeService.java完整代码

package com.blog.personblog.service;

import com.blog.personblog.config.page.PageRequest;
import com.blog.personblog.entity.Notice;

import java.util.List;

public interface NoticeService {

    /**
     * 获取所有的分类(分页)
     */
    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);

}

1.3 接口实现类

在写完了接口之后,需要在Impl包下新建一个NoticeServiceImpl.java实现Service

package com.blog.personblog.service;

import com.blog.personblog.config.page.PageRequest;
import com.blog.personblog.entity.Notice;

import java.util.List;

@Service
public interface NoticeService {

    /**
     * 获取所有的分类(分页)
     */
    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);

}

1.4 数据库查询接口实现

mapper层下创建一个NoticeMapper.java

 NoticeMapper.java完整代码

package com.blog.personblog.mapper;

import com.blog.personblog.entity.Notice;
import org.springframework.stereotype.Repository;

import javax.validation.Valid;
import java.util.List;

@Repository
public interface NoticeMapper {

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

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


    /**
     * 删除
     * @param noticeId
     */
    void deleteNotice(Integer noticeId);

    /**
     * 分类列表
     * @return
     */
    List<Notice> getNoticePage();
}

1.5 编写数据库xml

resources文件夹下的mapper文件新建一个NoticeMapper.xml文件

NoticeMapper.xml完整代码(update标签中记得不要漏掉逗号

<?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.personblog.mapper.NoticeMapper">
    <resultMap id="BaseResultMap" type="com.blog.personblog.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="createBy"/>
        <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.personblog.entity.Notice" useGeneratedKeys="true" keyProperty="noticeId">
        INSERT INTO person_notice (notice_title, notice_type, status, notice_content, create_by)
        VALUES (#{noticeTitle},#{noticeType},#{status},#{noticeContent},#{createBy})
    </insert>

    <update id="updateNotice" parameterType="com.blog.personblog.entity.Notice">
        update person_notice
        <set>
            notice_title = #{noticeTitle},
            notice_type = #{noticeType},
            status = #{status},
            notice_content = #{noticeContent},
            create_by = #{createBy}
        </set>
        WHERE notice_id = #{noticeId}
    </update>

    <delete id="deleteNotice" parameterType="java.lang.Integer">
        delete from person_notice where notice_id = #{noticeId,jdbcType=INTEGER}
    </delete>

    
</mapper>

数据库的接口已经写好了,

接下来回到NoticeService的实现类NoticeServiceimpl中,

使用@Autowired引入Mapper

  @Autowired
  NoticeMapper noticeMapper;

使用@Autowired引入NoticeMapper,然后用noticeMapper去调用NoticeMapper中的接口

package com.blog.personblog.service.Impl;

import com.blog.personblog.config.page.PageRequest;
import com.blog.personblog.entity.Notice;
import com.blog.personblog.mapper.NoticeMapper;
import com.blog.personblog.service.NoticeService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class NoticeServiceImpl implements NoticeService {

   @Autowired
   NoticeMapper noticeMapper;


    @Override
    public List<Notice> getNoticePage(PageRequest pageRequest) {
        int pageSize = pageRequest.getPageSize();
        int pageNum  = pageRequest.getPageNum();
        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);

    }
}

1.6 编写控制层

编写Controller层,NoticeController.java

package com.blog.personblog.controller;


import com.blog.personblog.config.page.PageRequest;
import com.blog.personblog.config.page.PageResult;
import com.blog.personblog.entity.Notice;
import com.blog.personblog.service.NoticeService;
import com.blog.personblog.util.JsonResult;
import com.blog.personblog.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;

@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> noticePage = noticeService.getNoticePage(pageRequest);
        PageInfo pageInfo = new PageInfo(noticePage);
        PageResult pageResult = PageUtil.getPageResult(pageRequest, pageInfo);
        return JsonResult.success(pageResult);
    }

    /**
     * 添加
     * @param notice
     * @return
     */
    @ApiOperation(value = "添加公告")
    @PostMapping("/create")
    public JsonResult<Object> createNotice(@RequestBody @Valid Notice notice) {
        int saveNotice = noticeService.saveNotice(notice);
        if ( saveNotice == 0) {
            return JsonResult.error("添加公告失败");
        }
        return JsonResult.success();
    }

    /**
     * 修改公告
     * @param notice
     * @return
     */
    @ApiOperation(value = "修改公告")
    @PostMapping("/update")
    public JsonResult<Object> updateNotice(@RequestBody @Valid Notice notice) {
        int updateNotice = noticeService.updateNotice(notice);
        if ( updateNotice == 0) {
            return JsonResult.error("修改公告失败");
        }
        return JsonResult.success();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除公告")
    @PostMapping("/delete/{id}")
    public JsonResult<Object> deleteNotice(@PathVariable(value = "id") int id) {
        noticeService.deleteNotice(id);
        return JsonResult.success();
    }


}

完成之后打开Swagger接口文档地址:http://localhost:8081/blog/swagger-ui/index.html#

 

说明成功 

三、测试

打开Postman,进行测试

先测试“添加功能”

新建一个“公告管理”公共接口,在其下新建“添加功能”接口,填入相应的JSON数据,点击Send发送

 200表示添加成功

打开数据库,打开person_notice表格,刷新,可以看到新增的数据

测试”查询功能“

 在“公告管理”其下新建“公告列表”接口,填入相应的JSON数据,点击Send发送

 返回200,表示成功

测试”修改功能“

在“公告管理”其下新建“修改公告”接口,填入相应的JSON数据,点击Send发送

200表示修改成功

打开数据库,刷新person_notice表格 

修改前

修改后:

 

 测试”删除功能“

打开数据库,刷新person_notice表格 ,数据删除成功

到此,四个接口全部测试完毕

记得提交代码到gitee! 

Logo

前往低代码交流专区

更多推荐