一文徹底理清SpringBoot CURD處理邏輯、順序
理清SpringBoot CURD處理邏輯、順序
1、Controller(控制器):
- 控制器接收來自客戶端的請求,并負責處理請求的路由和參數(shù)解析。
- 控制器通常會調(diào)用相應的服務層方法來處理業(yè)務邏輯,并將結(jié)果返回給客戶端。
2、Service(服務層):
- 服務層包含了應用程序的業(yè)務邏輯。
- 服務層通常會調(diào)用數(shù)據(jù)訪問對象(DAO)來進行數(shù)據(jù)的讀取、寫入和修改。
- 服務層可以對數(shù)據(jù)進行處理、驗證和轉(zhuǎn)換,并協(xié)調(diào)多個數(shù)據(jù)訪問對象的操作。
- 服務層的方法可以被控制器調(diào)用,也可以被其他服務層方法調(diào)用。
3、DAO(數(shù)據(jù)訪問對象):
- 數(shù)據(jù)訪問對象負責與數(shù)據(jù)源(如數(shù)據(jù)庫)進行交互,執(zhí)行數(shù)據(jù)的讀取、寫入和修改操作。
- DAO通常會定義一組方法,用于執(zhí)行CRUD操作(創(chuàng)建、讀取、更新、刪除)。
- DAO可以使用ORM(對象關(guān)系映射)工具或手動編寫SQL語句來與數(shù)據(jù)源進行交互。
- DAO的實現(xiàn)可以是直接操作數(shù)據(jù)庫的類,也可以是使用ORM框架生成的類。
4、PO(持久化對象):
- 持久化對象是與數(shù)據(jù)源中的表或集合相對應的對象。
- 持久化對象通常具有與數(shù)據(jù)表字段相對應的屬性,并提供了用于讀取和寫入數(shù)據(jù)的方法。
- 持久化對象可以由ORM框架自動生成,也可以手動編寫。
5、Repo(倉庫接口):
- 倉庫接口定義了對領(lǐng)域?qū)ο蟮某志没筒樵兎椒ā?/li>
- 倉庫接口通常包含根據(jù)特定條件查詢領(lǐng)域?qū)ο蟮姆椒ǎ绺鶕?jù)ID查詢、根據(jù)條件查詢等。
- 倉庫接口提供了抽象的方法,用于與具體的數(shù)據(jù)訪問對象進行交互。
6、RepoImpl(倉庫實現(xiàn)類):
- 倉庫實現(xiàn)類是倉庫接口的具體實現(xiàn)。
- 倉庫實現(xiàn)類負責將倉庫接口定義的方法與具體的數(shù)據(jù)訪問對象(DAO)進行關(guān)聯(lián)。
- 倉庫實現(xiàn)類實現(xiàn)了倉庫接口中定義的方法,并根據(jù)需要調(diào)用相應的DAO方法。
7、Mapper(映射器):
- 映射器是一種用于將持久化對象與數(shù)據(jù)庫表之間進行映射的工具。
- 映射器可以根據(jù)配置文件或注解來定義對象與表之間的映射關(guān)系。
- 映射器可以將持久化對象的屬性映射到數(shù)據(jù)庫表的列,并提供了CRUD操作的方法
聯(lián)表查詢接口
- Controller
@GetMapping("status") @ApiOperation("公告狀態(tài)") @Logger(menu = "首頁", action = "公告狀態(tài)") public Result noticeStatus() { List<SystemNoticeResponse> responses = systemNoticeService.SystemNoticeStatus(); return Result.succ(responses); }
- Service
/** * 公告狀態(tài) * * @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
@Override public 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_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>
- Dao
@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; }
- 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):數(shù)據(jù)傳輸對象
@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("內(nèi)容") private String noticeContent; @ApiModelProperty("創(chuàng)建時間") private Date createAt; @ApiModelProperty("閱讀狀態(tài), 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:用于獲取(查詢)資源,不應該用于修改數(shù)據(jù)(數(shù)據(jù)庫獲?。?/li>
- @PostMapping:用于創(chuàng)建資源,不應該用于查詢數(shù)據(jù)(數(shù)據(jù)庫編輯、修改)
Request和Response辨析
前端(客戶端)—— 后端(服務器端)
- Request(請求):客戶端向服務器發(fā)送的一種信息,用于請求操作或獲取資源( 前端 ==》后端 )
- Response(響應):Response是服務器對客戶端請求的回應,包含服務器處理結(jié)果的數(shù)據(jù)( 后端 ==》前端 )
總結(jié)
到此這篇關(guān)于SpringBoot CURD處理邏輯、順序的文章就介紹到這了,更多相關(guān)SpringBoot CURD處理邏輯順序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot+Jackson自定義注解數(shù)據(jù)脫敏的項目實踐
數(shù)據(jù)脫敏可以對敏感數(shù)據(jù)比如 手機號、銀行卡號等信息進行轉(zhuǎn)換或者修改,本文主要介紹了Springboot+Jackson?自定義注解數(shù)據(jù)脫敏,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-08-08springboot接入netty實現(xiàn)在線統(tǒng)計人數(shù)
本文主要介紹了springboot接入netty實現(xiàn)在線統(tǒng)計人數(shù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-03-03深入淺析Java中Static Class及靜態(tài)內(nèi)部類和非靜態(tài)內(nèi)部類的不同
上次有朋友問我,java中的類可以是static嗎?我給他肯定的回答是可以的,在java中我們可以有靜態(tài)實例變量、靜態(tài)方法、靜態(tài)塊。當然類也可以是靜態(tài)的,下面小編整理了些關(guān)于java中的static class相關(guān)資料分享在腳本之家平臺供大家參考2015-11-11StackTraceElement獲取方法調(diào)用棧信息實例詳解
這篇文章主要介紹了StackTraceElement獲取方法調(diào)用棧信息實例詳解,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02MyEclipse設(shè)置Console輸出到文件的實現(xiàn)方法
下面小編就為大家?guī)硪黄狹yEclipse設(shè)置Console輸出到文件的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07