最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

一文徹底理清SpringBoot CURD處理邏輯、順序

 更新時(shí)間:2023年10月14日 11:36:46   作者:wei_shuo  
這篇文章主要給大家介紹了關(guān)于如何一文徹底理清SpringBoot CURD處理邏輯、順序的相關(guān)資料,CURD是一個(gè)數(shù)據(jù)庫技術(shù)中的縮寫詞,一般的項(xiàng)目開發(fā)的各種參數(shù)的基本功能都是CURD,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下

理清SpringBoot CURD處理邏輯、順序

1、Controller(控制器):

  • 控制器接收來自客戶端的請求,并負(fù)責(zé)處理請求的路由和參數(shù)解析。
  • 控制器通常會調(diào)用相應(yīng)的服務(wù)層方法來處理業(yè)務(wù)邏輯,并將結(jié)果返回給客戶端。

2、Service(服務(wù)層):

  • 服務(wù)層包含了應(yīng)用程序的業(yè)務(wù)邏輯。
  • 服務(wù)層通常會調(diào)用數(shù)據(jù)訪問對象(DAO)來進(jìn)行數(shù)據(jù)的讀取、寫入和修改。
  • 服務(wù)層可以對數(shù)據(jù)進(jìn)行處理、驗(yàn)證和轉(zhuǎn)換,并協(xié)調(diào)多個(gè)數(shù)據(jù)訪問對象的操作。
  • 服務(wù)層的方法可以被控制器調(diào)用,也可以被其他服務(wù)層方法調(diào)用。

3、DAO(數(shù)據(jù)訪問對象):

  • 數(shù)據(jù)訪問對象負(fù)責(zé)與數(shù)據(jù)源(如數(shù)據(jù)庫)進(jìn)行交互,執(zhí)行數(shù)據(jù)的讀取、寫入和修改操作。
  • DAO通常會定義一組方法,用于執(zhí)行CRUD操作(創(chuàng)建、讀取、更新、刪除)。
  • DAO可以使用ORM(對象關(guān)系映射)工具或手動(dòng)編寫SQL語句來與數(shù)據(jù)源進(jìn)行交互。
  • DAO的實(shí)現(xiàn)可以是直接操作數(shù)據(jù)庫的類,也可以是使用ORM框架生成的類。

4、PO(持久化對象):

  • 持久化對象是與數(shù)據(jù)源中的表或集合相對應(yīng)的對象。
  • 持久化對象通常具有與數(shù)據(jù)表字段相對應(yīng)的屬性,并提供了用于讀取和寫入數(shù)據(jù)的方法。
  • 持久化對象可以由ORM框架自動(dòng)生成,也可以手動(dòng)編寫。

5、Repo(倉庫接口):

  • 倉庫接口定義了對領(lǐng)域?qū)ο蟮某志没筒樵兎椒ā?/li>
  • 倉庫接口通常包含根據(jù)特定條件查詢領(lǐng)域?qū)ο蟮姆椒?,如根?jù)ID查詢、根據(jù)條件查詢等。
  • 倉庫接口提供了抽象的方法,用于與具體的數(shù)據(jù)訪問對象進(jìn)行交互。

6、RepoImpl(倉庫實(shí)現(xiàn)類):

  • 倉庫實(shí)現(xiàn)類是倉庫接口的具體實(shí)現(xiàn)。
  • 倉庫實(shí)現(xiàn)類負(fù)責(zé)將倉庫接口定義的方法與具體的數(shù)據(jù)訪問對象(DAO)進(jìn)行關(guān)聯(lián)。
  • 倉庫實(shí)現(xiàn)類實(shí)現(xiàn)了倉庫接口中定義的方法,并根據(jù)需要調(diào)用相應(yīng)的DAO方法。

7、Mapper(映射器):

  • 映射器是一種用于將持久化對象與數(shù)據(jù)庫表之間進(jìn)行映射的工具。
  • 映射器可以根據(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:實(shí)體類字段對應(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ù)傳輸對象
@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 = "首頁", 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ù)庫獲?。?/li>
  • @PostMapping:用于創(chuàng)建資源,不應(yīng)該用于查詢數(shù)據(jù)(數(shù)據(jù)庫編輯、修改)

Request和Response辨析

前端(客戶端)—— 后端(服務(wù)器端)

  • Request(請求):客戶端向服務(wù)器發(fā)送的一種信息,用于請求操作或獲取資源( 前端 ==》后端 )
  • Response(響應(yīng)):Response是服務(wù)器對客戶端請求的回應(yīng),包含服務(wù)器處理結(jié)果的數(shù)據(jù)( 后端 ==》前端 )

總結(jié) 

到此這篇關(guān)于SpringBoot CURD處理邏輯、順序的文章就介紹到這了,更多相關(guān)SpringBoot CURD處理邏輯順序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring動(dòng)態(tài)數(shù)據(jù)源實(shí)現(xiàn)讀寫分離詳解

    Spring動(dòng)態(tài)數(shù)據(jù)源實(shí)現(xiàn)讀寫分離詳解

    這篇文章主要為大家詳細(xì)介紹了Spring動(dòng)態(tài)數(shù)據(jù)源實(shí)現(xiàn)讀寫分離,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • java?spring?mvc處理器映射器介紹

    java?spring?mvc處理器映射器介紹

    這篇文章主要介紹了java?spring?mvc處理器映射器,文章圍繞equestMapping解析映射介紹展開源碼內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-03-03
  • java擴(kuò)展Hibernate注解支持java8新時(shí)間類型

    java擴(kuò)展Hibernate注解支持java8新時(shí)間類型

    這篇文章主要介紹了java擴(kuò)展Hibernate注解支持java8新時(shí)間類型,需要的朋友可以參考下
    2014-04-04
  • Java開發(fā)環(huán)境配置及Vscode搭建過程

    Java開發(fā)環(huán)境配置及Vscode搭建過程

    今天通過圖文并茂的形式給大家介紹Java開發(fā)環(huán)境配置及Vscode搭建過程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-07-07
  • Java實(shí)現(xiàn)歸并排序的方法詳解(包含遞歸+非遞歸)

    Java實(shí)現(xiàn)歸并排序的方法詳解(包含遞歸+非遞歸)

    歸并歸分n時(shí)間復(fù)雜度O(nlogn)的歸并排序,遞歸與迭代實(shí)現(xiàn),穩(wěn)定排序適用于大數(shù)據(jù)處理,本文詳細(xì)解析了遞歸與非遞歸版本的實(shí)現(xiàn)邏輯與應(yīng)用場景,需要的朋友可以參考下
    2026-05-05
  • 詳解Zookeeper基礎(chǔ)知識

    詳解Zookeeper基礎(chǔ)知識

    本文主要講解了Zookeeper的基礎(chǔ)知識,ZooKeeper提供了一個(gè)通用協(xié)調(diào)模式實(shí)現(xiàn)方法的開源共享庫,使程序員免于寫這類通用的協(xié)議。關(guān)于Zookeeper更多相關(guān)知識,感興趣的小伙伴參考一下這篇文章
    2021-09-09
  • Java 通過位運(yùn)算求一個(gè)集合的所有子集方法

    Java 通過位運(yùn)算求一個(gè)集合的所有子集方法

    下面小編就為大家?guī)硪黄狫ava 通過位運(yùn)算求一個(gè)集合的所有子集方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-03-03
  • Spring中過濾器(Filter)和攔截器(Interceptor)到底啥區(qū)別詳解

    Spring中過濾器(Filter)和攔截器(Interceptor)到底啥區(qū)別詳解

    在Spring框架中過濾器(Filter)和攔截器(Interceptor)都是用于處理HTTP請求的重要組件,但它們的工作原理和執(zhí)行順序有所不同,這篇文章主要介紹了Spring中過濾器(Filter)和攔截器(Interceptor)到底啥區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2026-04-04
  • Java開發(fā)者推薦的10種常用工具

    Java開發(fā)者推薦的10種常用工具

    這篇文章主要為大家詳細(xì)介紹了Java開發(fā)者推薦的10種常用工具,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • 解決Jackson解析嵌套類問題(MismatchedInputException)

    解決Jackson解析嵌套類問題(MismatchedInputException)

    這篇文章主要介紹了解決Jackson解析嵌套類問題(MismatchedInputException),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06

最新評論

宝坻区| 苍梧县| 南木林县| 柳州市| 咸阳市| 清水河县| 青州市| 华坪县| 界首市| 松原市| 巢湖市| 和顺县| 塔河县| 凌源市| 克东县| 抚顺市| 滨海县| 海伦市| 锡林郭勒盟| 湾仔区| 台北县| 石河子市| 临城县| 中宁县| 桐梓县| 万山特区| 南康市| 大庆市| 读书| 壤塘县| 莆田市| 和硕县| 通海县| 安福县| 洛南县| 武宁县| 定边县| 兰西县| 淄博市| 贺州市| 高碑店市|