一文彻底理清SpringBoot CURD处理逻辑、顺序
理清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
1 2 3 4 5 6 7 | @GetMapping ( "status" ) @ApiOperation ( "公告状态" ) @Logger (menu = "首页" , action = "公告状态" ) public Result noticeStatus() { List<SystemNoticeResponse> responses = systemNoticeService.SystemNoticeStatus(); return Result.succ(responses); } |
- Service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | /** * 公告状态 * * @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
- RepoImpl
1 2 3 4 | @Override public List<SystemNoticeReaderResponse> SystemNoticeReaderStatus(Long merchantId) { return baseMapper.systemNoticeStatus(merchantId); } |
- Mapper
- Mapper.xml
1 2 3 4 5 6 7 8 9 10 | < 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | @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:实体类字段对应的枚举类字段
1 2 3 4 5 | 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):数据传输对象
1 2 3 4 5 6 7 8 9 10 | @Data @AllArgsConstructor public class SystemNoticeReaderResponse { @ApiModelProperty ( "ID" ) private String id; @ApiModelProperty ( "阅读公告ID" ) private String NoticeId; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @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
1 2 3 4 5 6 7 | @PostMapping ( "add" ) @ApiOperation ( "新增公告" ) @Logger (menu = "公告管理" , action = "新增公告" ) public Result noticeAdd( @Validated @RequestBody SystemNoticeResponse insert) { systemNoticeService.addSystemNotice(insert); return Result.succ( "添加成功" ); } |
- Service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /** * 公告添加 * * @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
1 2 3 4 5 6 7 | @PostMapping ( "delete" ) @ApiOperation ( "删除公告" ) @Logger (menu = "公告管理" , action = "删除公告" ) public Result noticeDelete( @Validated @RequestBody CommonRequestId request) { systemNoticeRepo.removeById(request.getId()); return Result.succ( "删除成功" ); } |
update
- Controller
1 2 3 4 5 6 7 | @PostMapping ( "update" ) @ApiOperation ( "编辑公告" ) @Logger (menu = "公告管理" , action = "编辑公告" ) public Result noticeUpdate( @Validated @RequestBody SystemNoticeResponse insert) { systemNoticeService.updateSystemNotice(insert); return Result.succ( "更新成功" ); } |
- Service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /** * 编辑公告 * * @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
1 2 3 4 5 6 7 | @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
1 2 3 4 5 6 7 | @PostMapping ( "insert" ) @ApiOperation ( "已阅读" ) @Logger (menu = "首页" , action = "已阅读" ) public Result noticeReader( @Validated @RequestBody CommonRequestId request) { systemNoticeService.SystemNoticeReader(request.getId()); return Result.succ( "已阅读" ); } |
- Service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /** * 公告 已阅读 * * @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是服务器对客户端请求的回应,包含服务器处理结果的数据( 后端 ==》前端 )
总结
到此这篇关于SpringBoot CURD处理逻辑、顺序的文章就介绍到这了,更多相关SpringBoot CURD处理逻辑顺序内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

微信公众号搜索 “ 脚本之家 ” ,选择关注
程序猿的那些事、送书等活动等着你
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 reterry123@163.com 进行投诉反馈,一经查实,立即处理!
相关文章
java HttpURLConnection类的disconnect方法与http长连接详解
这篇文章主要介绍了java HttpURLConnection类的disconnect方法与http长连接,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-04-04idea springBoot项目自动注入mapper为空报错的解决方法
这篇文章主要介绍了idea springBoot项目自动注入mapper为空报错的解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2023-03-03
最新评论