理清SpringBoot CURD处理逻辑、顺序

2023-11-11 23:50

本文主要是介绍理清SpringBoot CURD处理逻辑、顺序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在这里插入图片描述

💗wei_shuo的个人主页

💫wei_shuo的学习社区

🌐Hello World !


理清SpringBoot CURD处理逻辑、顺序

  1. Controller(控制器):
    • 控制器接收来自客户端的请求,并负责处理请求的路由和参数解析。
    • 控制器通常会调用相应的服务层方法来处理业务逻辑,并将结果返回给客户端。
  2. Service(服务层):
    • 服务层包含了应用程序的业务逻辑。
    • 服务层通常会调用数据访问对象(DAO)来进行数据的读取、写入和修改。
    • 服务层可以对数据进行处理、验证和转换,并协调多个数据访问对象的操作。
    • 服务层的方法可以被控制器调用,也可以被其他服务层方法调用。
  3. DAO(数据访问对象):
    • 数据访问对象负责与数据源(如数据库)进行交互,执行数据的读取、写入和修改操作。
    • DAO通常会定义一组方法,用于执行CRUD操作(创建、读取、更新、删除)。
    • DAO可以使用ORM(对象关系映射)工具或手动编写SQL语句来与数据源进行交互。
    • DAO的实现可以是直接操作数据库的类,也可以是使用ORM框架生成的类。
  4. PO(持久化对象):
    • 持久化对象是与数据源中的表或集合相对应的对象。
    • 持久化对象通常具有与数据表字段相对应的属性,并提供了用于读取和写入数据的方法。
    • 持久化对象可以由ORM框架自动生成,也可以手动编写。
  5. Repo(仓库接口):
    • 仓库接口定义了对领域对象的持久化和查询方法。
    • 仓库接口通常包含根据特定条件查询领域对象的方法,如根据ID查询、根据条件查询等。
    • 仓库接口提供了抽象的方法,用于与具体的数据访问对象进行交互。
  6. RepoImpl(仓库实现类):
    • 仓库实现类是仓库接口的具体实现。
    • 仓库实现类负责将仓库接口定义的方法与具体的数据访问对象(DAO)进行关联。
    • 仓库实现类实现了仓库接口中定义的方法,并根据需要调用相应的DAO方法。
  7. Mapper(映射器):
    • 映射器是一种用于将持久化对象与数据库表之间进行映射的工具。
    • 映射器可以根据配置文件或注解来定义对象与表之间的映射关系。
    • 映射器可以将持久化对象的属性映射到数据库表的列,并提供了CRUD操作的方法

联表查询接口

  • Controller
    @GetMapping("status")@ApiOperation("公告状态")@Logger(menu = "首页", action = "公告状态")public Result noticeStatus() {List<SystemNoticeResponse> responses = systemNoticeService.SystemNoticeStatus();return Result.succ(responses);}
  • Service
    /*** 公告状态** @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;}
  • Repo
List<SystemNoticeReaderResponse> SystemNoticeReaderStatus(Long merchantId);
  • RepoImpl
   @Overridepublic List<SystemNoticeReaderResponse> SystemNoticeReaderStatus(Long merchantId) {return baseMapper.systemNoticeStatus(merchantId);}
  • Mapper
List<SystemNoticeReaderResponse> systemNoticeStatus(Long id);
  • Mapper.xml
    <select id="systemNoticeStatus" parameterType="java.lang.Long" resultMap="systemNoticeStatusResultMap">SELECT y.id, s.notice_idFROM "system_notice" as yLEFT 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>
  • Dao
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName("system_notice_reader")
public class SystemNoticeReader {/*** null | system_notice_reader.id | @mbg.generated*/@ApiModelProperty("null")@TableIdprivate 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;}
  • DaoCol:实体类字段对应的枚举类字段
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";}
  • DTO(VO):数据传输对象
@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
  • Controller
    @PostMapping("add")@ApiOperation("新增公告")@Logger(menu = "公告管理", action = "新增公告")public Result noticeAdd(@Validated @RequestBody SystemNoticeResponse insert) {systemNoticeService.addSystemNotice(insert);return Result.succ("添加成功");}
  • Service
    /*** 公告添加** @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
  • Controller
    @PostMapping("delete")@ApiOperation("删除公告")@Logger(menu = "公告管理", action = "删除公告")public Result noticeDelete(@Validated @RequestBody CommonRequestId request) {systemNoticeRepo.removeById(request.getId());return Result.succ("删除成功");}
update
  • Controller
    @PostMapping("update")@ApiOperation("编辑公告")@Logger(menu = "公告管理", action = "编辑公告")public Result noticeUpdate(@Validated @RequestBody SystemNoticeResponse insert) {systemNoticeService.updateSystemNotice(insert);return Result.succ("更新成功");}
  • Service
    /*** 编辑公告** @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
  • Controller
    @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
  • Controller
@PostMapping("insert")
@ApiOperation("已阅读")
@Logger(menu = "首页", action = "已阅读")
public Result noticeReader(@Validated @RequestBody CommonRequestId request) {systemNoticeService.SystemNoticeReader(request.getId());return Result.succ("已阅读");
}
  • Service
    /*** 公告 已阅读** @param* @return*/public SystemNoticeReader SystemNoticeReader(Long noticeId) {SystemNoticeReader notice = new SystemNoticeReader(null,noticeId,AuthContextHolder.getLoginMerchant().getId());systemNoticeReaderRepo.save(notice);return notice;}

GetMapping和PostMapping辨析

  • @GetMapping:用于获取(查询)资源,不应该用于修改数据(数据库获取)
  • @PostMapping:用于创建资源,不应该用于查询数据(数据库编辑、修改)

Request和Response辨析

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

  • Request(请求):客户端向服务器发送的一种信息,用于请求操作或获取资源( 前端 ==》后端 )
  • Response(响应):Response是服务器对客户端请求的回应,包含服务器处理结果的数据( 后端 ==》前端 )

🌼 结语:创作不易,如果觉得博主的文章赏心悦目,还请——点赞👍收藏⭐️评论📝


在这里插入图片描述

这篇关于理清SpringBoot CURD处理逻辑、顺序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/393671

相关文章

javax.net.ssl.SSLHandshakeException:异常原因及解决方案

《javax.net.ssl.SSLHandshakeException:异常原因及解决方案》javax.net.ssl.SSLHandshakeException是一个SSL握手异常,通常在建立SS... 目录报错原因在程序中绕过服务器的安全验证注意点最后多说一句报错原因一般出现这种问题是因为目标服务器

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

电脑提示xlstat4.dll丢失怎么修复? xlstat4.dll文件丢失处理办法

《电脑提示xlstat4.dll丢失怎么修复?xlstat4.dll文件丢失处理办法》长时间使用电脑,大家多少都会遇到类似dll文件丢失的情况,不过,解决这一问题其实并不复杂,下面我们就来看看xls... 在Windows操作系统中,xlstat4.dll是一个重要的动态链接库文件,通常用于支持各种应用程序

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

SQL Server数据库死锁处理超详细攻略

《SQLServer数据库死锁处理超详细攻略》SQLServer作为主流数据库管理系统,在高并发场景下可能面临死锁问题,影响系统性能和稳定性,这篇文章主要给大家介绍了关于SQLServer数据库死... 目录一、引言二、查询 Sqlserver 中造成死锁的 SPID三、用内置函数查询执行信息1. sp_w

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是