0


理清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 ="公告状态")publicResultnoticeStatus(){List<SystemNoticeResponse> responses =systemNoticeService.SystemNoticeStatus();returnResult.succ(responses);}
  • Service
/**
     * 公告状态
     *
     * @param
     * @return
     */publicList<SystemNoticeResponse>SystemNoticeStatus(){Long merchantId =AuthContextHolder.getLoginMerchant().getId();List<SystemNoticeReaderResponse> systemNoticeReaderResponses =systemNoticeReaderRepo.SystemNoticeReaderStatus(merchantId);Map<String,Integer> idNoticeIdMap =newHashMap<>();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 =newArrayList<>();for(Map.Entry<String,Integer> entry : idNoticeIdMap.entrySet()){String id = entry.getKey();long parseLong =Long.parseLong(id);SystemNoticeResponse response =newSystemNoticeResponse(
                    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
@OverridepublicList<SystemNoticeReaderResponse>SystemNoticeReaderStatus(Long merchantId){return baseMapper.systemNoticeStatus(merchantId);}
  • Mapper
List<SystemNoticeReaderResponse>systemNoticeStatus(Long id);
  • Mapper.xml
<selectid="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><resultMapid="systemNoticeStatusResultMap"type="com.moozumi.bean.response.notice.SystemNoticeReaderResponse"><resultcolumn="id"property="id"/><resultcolumn="notice_id"property="NoticeId"/></resultMap>
  • Dao
@Data@Builder@NoArgsConstructor@AllArgsConstructor@TableName("system_notice_reader")publicclassSystemNoticeReader{/**
     * null | system_notice_reader.id | @mbg.generated
     */@ApiModelProperty("null")@TableIdprivateLong id;/**
     * 公告 ID | system_notice_reader.notice_id | @mbg.generated
     */@ApiModelProperty("公告 ID")privateLong noticeId;/**
     * 已阅读商户 ID | system_notice_reader.merchant_id | @mbg.generated
     */@ApiModelProperty("已阅读商户 ID")privateLong merchantId;}
  • DaoCol:实体类字段对应的枚举类字段
publicclassSystemNoticeReaderCol{publicstaticfinalStringID="id";publicstaticfinalStringNOTICE_ID="notice_id";publicstaticfinalStringMERCHANT_ID="merchant_id";}
  • DTO(VO):数据传输对象
@Data@AllArgsConstructorpublicclassSystemNoticeReaderResponse{@ApiModelProperty("ID")privateString id;@ApiModelProperty("阅读公告ID")privateStringNoticeId;}
@Data@AllArgsConstructorpublicclassSystemNoticeResponse{@ApiModelProperty("id")privateLong id;@ApiModelProperty("标题")privateString noticeTitle;@ApiModelProperty("内容")privateString noticeContent;@ApiModelProperty("创建时间")privateDate createAt;@ApiModelProperty("阅读状态, 0: 待阅读, 1: 已阅读")privateInteger readStatus;}

CURD接口

add

  • Controller
@PostMapping("add")@ApiOperation("新增公告")@Logger(menu ="公告管理", action ="新增公告")publicResultnoticeAdd(@Validated@RequestBodySystemNoticeResponse insert){
        systemNoticeService.addSystemNotice(insert);returnResult.succ("添加成功");}
  • Service
/**
     * 公告添加
     *
     * @param insert
     * @return
     */publicSystemNoticeaddSystemNotice(SystemNoticeResponse insert){SystemNotice notice =newSystemNotice(null,
                insert.getNoticeTitle(),
                insert.getNoticeContent(),newDate(),AuthContextHolder.getLoginManager().getUserRealName());
        systemNoticeRepo.save(notice);return notice;}

delete

  • Controller
@PostMapping("delete")@ApiOperation("删除公告")@Logger(menu ="公告管理", action ="删除公告")publicResultnoticeDelete(@Validated@RequestBodyCommonRequestId request){
        systemNoticeRepo.removeById(request.getId());returnResult.succ("删除成功");}

update

  • Controller
@PostMapping("update")@ApiOperation("编辑公告")@Logger(menu ="公告管理", action ="编辑公告")publicResultnoticeUpdate(@Validated@RequestBodySystemNoticeResponse insert){
        systemNoticeService.updateSystemNotice(insert);returnResult.succ("更新成功");}
  • Service
/**
     * 编辑公告
     *
     * @param insert
     * @return
     */publicSystemNoticeupdateSystemNotice(SystemNoticeResponse insert){SystemNotice notice = systemNoticeRepo.getById(insert.getId());if(notice !=null){
            notice.setNoticeTitle(insert.getNoticeTitle());
            notice.setNoticeContent(insert.getNoticeContent());
            notice.setCreateAt(newDate());
            systemNoticeRepo.updateById(notice);}return notice;}

list

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

insert

  • Controller
@PostMapping("insert")@ApiOperation("已阅读")@Logger(menu ="首页", action ="已阅读")publicResultnoticeReader(@Validated@RequestBodyCommonRequestId request){systemNoticeService.SystemNoticeReader(request.getId());returnResult.succ("已阅读");}
  • Service
/**
     * 公告 已阅读
     *
     * @param
     * @return
     */publicSystemNoticeReaderSystemNoticeReader(Long noticeId){SystemNoticeReader notice =newSystemNoticeReader(null,
                noticeId,AuthContextHolder.getLoginMerchant().getId());
        systemNoticeReaderRepo.save(notice);return notice;}

GetMapping和PostMapping辨析

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

Request和Response辨析

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

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

🌼 结语:创作不易,如果觉得博主的文章赏心悦目,还请——

点赞

👍

收藏

⭐️

评论

📝


在这里插入图片描述


本文转载自: https://blog.csdn.net/weixin_62765017/article/details/131816767
版权归原作者 wei_shuo 所有, 如有侵权,请联系我们删除。

“理清SpringBoot CURD处理逻辑、顺序”的评论:

还没有评论