网站开发小程序定制,管理系统入口admin,广告设计专业就业方向,吉林省公司注册网站#x1f497;wei_shuo的个人主页 #x1f4ab;wei_shuo的学习社区 #x1f310;Hello World #xff01; 理清SpringBoot CURD处理逻辑、顺序 Controller#xff08;控制器#xff09;#xff1a; 控制器接收来自客户端的请求#xff0c;并负责处理请求的路由和参数解析… wei_shuo的个人主页 wei_shuo的学习社区 Hello World 理清SpringBoot CURD处理逻辑、顺序 Controller控制器 控制器接收来自客户端的请求并负责处理请求的路由和参数解析。控制器通常会调用相应的服务层方法来处理业务逻辑并将结果返回给客户端。 Service服务层 服务层包含了应用程序的业务逻辑。服务层通常会调用数据访问对象DAO来进行数据的读取、写入和修改。服务层可以对数据进行处理、验证和转换并协调多个数据访问对象的操作。服务层的方法可以被控制器调用也可以被其他服务层方法调用。 DAO数据访问对象 数据访问对象负责与数据源如数据库进行交互执行数据的读取、写入和修改操作。DAO通常会定义一组方法用于执行CRUD操作创建、读取、更新、删除。DAO可以使用ORM对象关系映射工具或手动编写SQL语句来与数据源进行交互。DAO的实现可以是直接操作数据库的类也可以是使用ORM框架生成的类。 PO持久化对象 持久化对象是与数据源中的表或集合相对应的对象。持久化对象通常具有与数据表字段相对应的属性并提供了用于读取和写入数据的方法。持久化对象可以由ORM框架自动生成也可以手动编写。 Repo仓库接口 仓库接口定义了对领域对象的持久化和查询方法。仓库接口通常包含根据特定条件查询领域对象的方法如根据ID查询、根据条件查询等。仓库接口提供了抽象的方法用于与具体的数据访问对象进行交互。 RepoImpl仓库实现类 仓库实现类是仓库接口的具体实现。仓库实现类负责将仓库接口定义的方法与具体的数据访问对象DAO进行关联。仓库实现类实现了仓库接口中定义的方法并根据需要调用相应的DAO方法。 Mapper映射器 映射器是一种用于将持久化对象与数据库表之间进行映射的工具。映射器可以根据配置文件或注解来定义对象与表之间的映射关系。映射器可以将持久化对象的属性映射到数据库表的列并提供了CRUD操作的方法 联表查询接口 Controller GetMapping(status)ApiOperation(公告状态)Logger(menu 首页, action 公告状态)public Result noticeStatus() {ListSystemNoticeResponse responses systemNoticeService.SystemNoticeStatus();return Result.succ(responses);}Service /*** 公告状态** param* return*/public ListSystemNoticeResponse SystemNoticeStatus() {Long merchantId AuthContextHolder.getLoginMerchant().getId();ListSystemNoticeReaderResponse systemNoticeReaderResponses systemNoticeReaderRepo.SystemNoticeReaderStatus(merchantId);MapString, 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}}ListSystemNoticeResponse noticeResponses new ArrayList();for (Map.EntryString, 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 ListSystemNoticeReaderResponse SystemNoticeReaderStatus(Long merchantId);RepoImpl Overridepublic ListSystemNoticeReaderResponse SystemNoticeReaderStatus(Long merchantId) {return baseMapper.systemNoticeStatus(merchantId);}Mapper ListSystemNoticeReaderResponse systemNoticeStatus(Long id);Mapper.xml select idsystemNoticeStatus parameterTypejava.lang.Long resultMapsystemNoticeStatusResultMapSELECT 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}/selectresultMap idsystemNoticeStatusResultMap typecom.moozumi.bean.response.notice.SystemNoticeReaderResponseresult columnid propertyid /result columnnotice_id propertyNoticeId //resultMapDao 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;}DTOVO:数据传输对象 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 ResultPageResultSystemNotice list(SystemNoticeQuery query) {PageSystemNotice 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是服务器对客户端请求的回应包含服务器处理结果的数据 后端 》前端 结语创作不易如果觉得博主的文章赏心悦目还请——点赞收藏⭐️评论