java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot CURD处理逻辑顺序

一文彻底理清SpringBoot CURD处理逻辑、顺序

作者:wei_shuo

这篇文章主要给大家介绍了关于如何一文彻底理清SpringBoot CURD处理逻辑、顺序的相关资料,CURD是一个数据库技术中的缩写词,一般的项目开发的各种参数的基本功能都是CURD,文中通过代码介绍的非常详细,需要的朋友可以参考下

理清SpringBoot CURD处理逻辑、顺序

1、Controller(控制器):

2、Service(服务层):

3、DAO(数据访问对象):

4、PO(持久化对象):

5、Repo(仓库接口):

6、RepoImpl(仓库实现类):

7、Mapper(映射器):

联表查询接口

    @GetMapping("status")
    @ApiOperation("公告状态")
    @Logger(menu = "首页", action = "公告状态")
    public Result noticeStatus() {
        List<SystemNoticeResponse> responses = systemNoticeService.SystemNoticeStatus();
        return Result.succ(responses);
    }
    /**
     * 公告状态
     *
     * @param
     * @return
     */
    public List<SystemNoticeResponse> SystemNoticeStatus() {
        Long merchantId = AuthContextHolder.getLoginMerchant().getId();
        List<SystemNoticeReaderResponse> systemNoticeReaderResponses = systemNoticeReaderRepo.SystemNoticeReaderStatus(merchantId);
        Map<String, Integer> idNoticeIdMap = new HashMap<>();
        for (SystemNoticeReaderResponse response : systemNoticeReaderResponses) {
            if (response.getId().equals(response.getNoticeId())) {
                idNoticeIdMap.put(response.getId(), 1);//已阅读:1
            } else {
                idNoticeIdMap.put(response.getId(), 0);//待阅读:0
            }
        }
        List<SystemNoticeResponse> noticeResponses = new ArrayList<>();
        for (Map.Entry<String, Integer> entry : idNoticeIdMap.entrySet()) {
            String id = entry.getKey();
            long parseLong = Long.parseLong(id);
            SystemNoticeResponse response = new SystemNoticeResponse(
                    parseLong,
                    systemNoticeRepo.getById(parseLong).getNoticeTitle(),
                    systemNoticeRepo.getById(parseLong).getNoticeContent(),
                    systemNoticeRepo.getById(parseLong).getCreateAt(),
                    entry.getValue()
            );
            noticeResponses.add(response);
        }

        noticeResponses = noticeResponses.stream()
                .sorted(Comparator.comparing(SystemNoticeResponse::getReadStatus)
                        .thenComparing(Comparator.comparing(SystemNoticeResponse::getCreateAt).reversed()))
                .collect(Collectors.toList());

        return noticeResponses;
    }
List<SystemNoticeReaderResponse> SystemNoticeReaderStatus(Long merchantId);
   @Override
    public List<SystemNoticeReaderResponse> SystemNoticeReaderStatus(Long merchantId) {
        return baseMapper.systemNoticeStatus(merchantId);
    }
List<SystemNoticeReaderResponse> systemNoticeStatus(Long id);
    <select id="systemNoticeStatus" parameterType="java.lang.Long" resultMap="systemNoticeStatusResultMap">
        SELECT y.id, s.notice_id
        FROM "system_notice" as y
                 LEFT JOIN "system_notice_reader" as s ON y."id" = s.notice_id AND s.merchant_id = #{id}
    </select>

    <resultMap id="systemNoticeStatusResultMap" type="com.moozumi.bean.response.notice.SystemNoticeReaderResponse">
        <result column="id" property="id" />
        <result column="notice_id" property="NoticeId" />
    </resultMap>
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName("system_notice_reader")
public class SystemNoticeReader {

    /**
     * null | system_notice_reader.id | @mbg.generated
     */
    @ApiModelProperty("null")
    @TableId
    private Long id;

    /**
     * 公告 ID | system_notice_reader.notice_id | @mbg.generated
     */
    @ApiModelProperty("公告 ID")
    private Long noticeId;

    /**
     * 已阅读商户 ID | system_notice_reader.merchant_id | @mbg.generated
     */
    @ApiModelProperty("已阅读商户 ID")
    private Long merchantId;

}
public class SystemNoticeReaderCol {
	public static final String ID = "id";
	public static final String NOTICE_ID = "notice_id";
	public static final String MERCHANT_ID = "merchant_id";
}
@Data
@AllArgsConstructor
public class SystemNoticeReaderResponse {

    @ApiModelProperty("ID")
    private String id;
    @ApiModelProperty("阅读公告ID")
    private String NoticeId;

}
@Data
@AllArgsConstructor
public class SystemNoticeResponse {

    @ApiModelProperty("id")
    private Long id;
    @ApiModelProperty("标题")
    private String noticeTitle;
    @ApiModelProperty("内容")
    private String noticeContent;
    @ApiModelProperty("创建时间")
    private Date createAt;
    @ApiModelProperty("阅读状态, 0: 待阅读, 1: 已阅读")
    private Integer readStatus;
}

CURD接口

add

    @PostMapping("add")
    @ApiOperation("新增公告")
    @Logger(menu = "公告管理", action = "新增公告")
    public Result noticeAdd(@Validated @RequestBody SystemNoticeResponse insert) {
        systemNoticeService.addSystemNotice(insert);
        return Result.succ("添加成功");
    }
    /**
     * 公告添加
     *
     * @param insert
     * @return
     */
    public SystemNotice addSystemNotice(SystemNoticeResponse insert) {
        SystemNotice notice = new SystemNotice(
                null,
                insert.getNoticeTitle(),
                insert.getNoticeContent(),
                new Date(),
                AuthContextHolder.getLoginManager().getUserRealName()
        );
        systemNoticeRepo.save(notice);
        return notice;
    }

delete

    @PostMapping("delete")
    @ApiOperation("删除公告")
    @Logger(menu = "公告管理", action = "删除公告")
    public Result noticeDelete(@Validated @RequestBody CommonRequestId request) {
        systemNoticeRepo.removeById(request.getId());
        return Result.succ("删除成功");
    }

update

    @PostMapping("update")
    @ApiOperation("编辑公告")
    @Logger(menu = "公告管理", action = "编辑公告")
    public Result noticeUpdate(@Validated @RequestBody SystemNoticeResponse insert) {
        systemNoticeService.updateSystemNotice(insert);
        return Result.succ("更新成功");
    }
    /**
     * 编辑公告
     *
     * @param insert
     * @return
     */
    public SystemNotice updateSystemNotice(SystemNoticeResponse insert) {
        SystemNotice notice = systemNoticeRepo.getById(insert.getId());
        if (notice != null) {
            notice.setNoticeTitle(insert.getNoticeTitle());
            notice.setNoticeContent(insert.getNoticeContent());
            notice.setCreateAt(new Date());
            systemNoticeRepo.updateById(notice);
        }
        return notice;
    }

list

    @GetMapping("list")
    @ApiOperation("展示公告")
    @Logger(menu = "公告管理", action = "展示公告")
    public Result<PageResult<SystemNotice>> list(SystemNoticeQuery query) {
        Page<SystemNotice> systemNoticePage = systemNoticeRepo.systemNoticeQuery(query);
        return Result.succ(PageResult.toPage(systemNoticePage));
    }

insert

@PostMapping("insert")
@ApiOperation("已阅读")
@Logger(menu = "首页", action = "已阅读")
public Result noticeReader(@Validated @RequestBody CommonRequestId request) {
    systemNoticeService.SystemNoticeReader(request.getId());
    return Result.succ("已阅读");
}
    /**
     * 公告 已阅读
     *
     * @param
     * @return
     */
    public SystemNoticeReader SystemNoticeReader(Long noticeId) {
        SystemNoticeReader notice = new SystemNoticeReader(
                null,
                noticeId,
                AuthContextHolder.getLoginMerchant().getId()
        );
        systemNoticeReaderRepo.save(notice);
        return notice;
    }

GetMapping和PostMapping辨析

Request和Response辨析

前端(客户端)—— 后端(服务器端)

总结 

到此这篇关于SpringBoot CURD处理逻辑、顺序的文章就介绍到这了,更多相关SpringBoot CURD处理逻辑顺序内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文