一文徹底理清SpringBoot CURD處理邏輯、順序
理清SpringBoot CURD處理邏輯、順序
1、Controller(控制器):
- 控制器接收來(lái)自客戶端的請(qǐng)求,并負(fù)責(zé)處理請(qǐng)求的路由和參數(shù)解析。
- 控制器通常會(huì)調(diào)用相應(yīng)的服務(wù)層方法來(lái)處理業(yè)務(wù)邏輯,并將結(jié)果返回給客戶端。
2、Service(服務(wù)層):
- 服務(wù)層包含了應(yīng)用程序的業(yè)務(wù)邏輯。
- 服務(wù)層通常會(huì)調(diào)用數(shù)據(jù)訪問(wèn)對(duì)象(DAO)來(lái)進(jìn)行數(shù)據(jù)的讀取、寫入和修改。
- 服務(wù)層可以對(duì)數(shù)據(jù)進(jìn)行處理、驗(yàn)證和轉(zhuǎn)換,并協(xié)調(diào)多個(gè)數(shù)據(jù)訪問(wèn)對(duì)象的操作。
- 服務(wù)層的方法可以被控制器調(diào)用,也可以被其他服務(wù)層方法調(diào)用。
3、DAO(數(shù)據(jù)訪問(wèn)對(duì)象):
- 數(shù)據(jù)訪問(wèn)對(duì)象負(fù)責(zé)與數(shù)據(jù)源(如數(shù)據(jù)庫(kù))進(jìn)行交互,執(zhí)行數(shù)據(jù)的讀取、寫入和修改操作。
- DAO通常會(huì)定義一組方法,用于執(zhí)行CRUD操作(創(chuàng)建、讀取、更新、刪除)。
- DAO可以使用ORM(對(duì)象關(guān)系映射)工具或手動(dòng)編寫SQL語(yǔ)句來(lái)與數(shù)據(jù)源進(jìn)行交互。
- DAO的實(shí)現(xiàn)可以是直接操作數(shù)據(jù)庫(kù)的類,也可以是使用ORM框架生成的類。
4、PO(持久化對(duì)象):
- 持久化對(duì)象是與數(shù)據(jù)源中的表或集合相對(duì)應(yīng)的對(duì)象。
- 持久化對(duì)象通常具有與數(shù)據(jù)表字段相對(duì)應(yīng)的屬性,并提供了用于讀取和寫入數(shù)據(jù)的方法。
- 持久化對(duì)象可以由ORM框架自動(dòng)生成,也可以手動(dòng)編寫。
5、Repo(倉(cāng)庫(kù)接口):
- 倉(cāng)庫(kù)接口定義了對(duì)領(lǐng)域?qū)ο蟮某志没筒樵兎椒ā?/li>
- 倉(cāng)庫(kù)接口通常包含根據(jù)特定條件查詢領(lǐng)域?qū)ο蟮姆椒?,如根?jù)ID查詢、根據(jù)條件查詢等。
- 倉(cāng)庫(kù)接口提供了抽象的方法,用于與具體的數(shù)據(jù)訪問(wèn)對(duì)象進(jìn)行交互。
6、RepoImpl(倉(cāng)庫(kù)實(shí)現(xiàn)類):
- 倉(cāng)庫(kù)實(shí)現(xiàn)類是倉(cāng)庫(kù)接口的具體實(shí)現(xiàn)。
- 倉(cāng)庫(kù)實(shí)現(xiàn)類負(fù)責(zé)將倉(cāng)庫(kù)接口定義的方法與具體的數(shù)據(jù)訪問(wèn)對(duì)象(DAO)進(jìn)行關(guān)聯(lián)。
- 倉(cāng)庫(kù)實(shí)現(xiàn)類實(shí)現(xiàn)了倉(cāng)庫(kù)接口中定義的方法,并根據(jù)需要調(diào)用相應(yīng)的DAO方法。
7、Mapper(映射器):
- 映射器是一種用于將持久化對(duì)象與數(shù)據(jù)庫(kù)表之間進(jìn)行映射的工具。
- 映射器可以根據(jù)配置文件或注解來(lái)定義對(duì)象與表之間的映射關(guān)系。
- 映射器可以將持久化對(duì)象的屬性映射到數(shù)據(jù)庫(kù)表的列,并提供了CRUD操作的方法
聯(lián)表查詢接口
- Controller
@GetMapping("status") @ApiOperation("公告狀態(tài)") @Logger(menu = "首頁(yè)", 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:實(shí)體類字段對(duì)應(yīng)的枚舉類字段
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ù)傳輸對(duì)象
@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("標(biāo)題") private String noticeTitle; @ApiModelProperty("內(nèi)容") private String noticeContent; @ApiModelProperty("創(chuàng)建時(shí)間") 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 = "首頁(yè)", 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:用于獲?。ú樵儯┵Y源,不應(yīng)該用于修改數(shù)據(jù)(數(shù)據(jù)庫(kù)獲?。?/li>
- @PostMapping:用于創(chuàng)建資源,不應(yīng)該用于查詢數(shù)據(jù)(數(shù)據(jù)庫(kù)編輯、修改)
Request和Response辨析
前端(客戶端)—— 后端(服務(wù)器端)
- Request(請(qǐng)求):客戶端向服務(wù)器發(fā)送的一種信息,用于請(qǐng)求操作或獲取資源( 前端 ==》后端 )
- Response(響應(yīng)):Response是服務(wù)器對(duì)客戶端請(qǐng)求的回應(yīng),包含服務(wù)器處理結(jié)果的數(shù)據(jù)( 后端 ==》前端 )
總結(jié)
到此這篇關(guān)于SpringBoot CURD處理邏輯、順序的文章就介紹到這了,更多相關(guān)SpringBoot CURD處理邏輯順序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot+Jackson自定義注解數(shù)據(jù)脫敏的項(xiàng)目實(shí)踐
數(shù)據(jù)脫敏可以對(duì)敏感數(shù)據(jù)比如 手機(jī)號(hào)、銀行卡號(hào)等信息進(jìn)行轉(zhuǎn)換或者修改,本文主要介紹了Springboot+Jackson?自定義注解數(shù)據(jù)脫敏,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08關(guān)于Java中常見(jiàn)的負(fù)載均衡算法
這篇文章主要介紹了關(guān)于Java中常見(jiàn)的負(fù)載均衡算法,負(fù)載平衡是一種電子計(jì)算機(jī)技術(shù),用來(lái)在多個(gè)計(jì)算機(jī)、網(wǎng)絡(luò)連接、CPU、磁盤驅(qū)動(dòng)器或其他資源中分配負(fù)載,以達(dá)到優(yōu)化資源使用、最大化吞吐率、最小化響應(yīng)時(shí)間、同時(shí)避免過(guò)載的目的,需要的朋友可以參考下2023-08-08springboot接入netty實(shí)現(xiàn)在線統(tǒng)計(jì)人數(shù)
本文主要介紹了springboot接入netty實(shí)現(xiàn)在線統(tǒng)計(jì)人數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03通過(guò)JWT來(lái)解決登錄認(rèn)證問(wèn)題的方案
Json web token (JWT),是為了在網(wǎng)絡(luò)應(yīng)用環(huán)境間傳遞聲明而執(zhí)行的一種基于JSON的開放標(biāo)準(zhǔn)((RFC7519),該token被設(shè)計(jì)為緊湊且安全的,特別適用于分布式站點(diǎn)的單點(diǎn)登錄(SSO)場(chǎng)景,本文給大家介紹了如何通過(guò) JWT 來(lái)解決登錄認(rèn)證問(wèn)題,需要的朋友可以參考下2024-12-12深入淺析Java中Static Class及靜態(tài)內(nèi)部類和非靜態(tài)內(nèi)部類的不同
上次有朋友問(wèn)我,java中的類可以是static嗎?我給他肯定的回答是可以的,在java中我們可以有靜態(tài)實(shí)例變量、靜態(tài)方法、靜態(tài)塊。當(dāng)然類也可以是靜態(tài)的,下面小編整理了些關(guān)于java中的static class相關(guān)資料分享在腳本之家平臺(tái)供大家參考2015-11-11StackTraceElement獲取方法調(diào)用棧信息實(shí)例詳解
這篇文章主要介紹了StackTraceElement獲取方法調(diào)用棧信息實(shí)例詳解,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02MyEclipse設(shè)置Console輸出到文件的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇MyEclipse設(shè)置Console輸出到文件的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07JavaWeb學(xué)習(xí)筆記之Filter和Listener
這篇文章主要給大家介紹了關(guān)于JavaWeb學(xué)習(xí)筆記之Filter和Listener的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03