💗wei_shuo的个人主页
💫wei_shuo的学习社区
🌐Hello World !
理清SpringBoot CURD处理逻辑、顺序
- Controller(控制器):
- 控制器接收来自客户端的请求,并负责处理请求的路由和参数解析。
- 控制器通常会调用相应的服务层方法来处理业务逻辑,并将结果返回给客户端。
- Service(服务层):
- 服务层包含了应用程序的业务逻辑。
- 服务层通常会调用数据访问对象(DAO)来进行数据的读取、写入和修改。
- 服务层可以对数据进行处理、验证和转换,并协调多个数据访问对象的操作。
- 服务层的方法可以被控制器调用,也可以被其他服务层方法调用。
- DAO(数据访问对象):
- 数据访问对象负责与数据源(如数据库)进行交互,执行数据的读取、写入和修改操作。
- DAO通常会定义一组方法,用于执行CRUD操作(创建、读取、更新、删除)。
- DAO可以使用ORM(对象关系映射)工具或手动编写SQL语句来与数据源进行交互。
- DAO的实现可以是直接操作数据库的类,也可以是使用ORM框架生成的类。
- PO(持久化对象):
- 持久化对象是与数据源中的表或集合相对应的对象。
- 持久化对象通常具有与数据表字段相对应的属性,并提供了用于读取和写入数据的方法。
- 持久化对象可以由ORM框架自动生成,也可以手动编写。
- Repo(仓库接口):
- 仓库接口定义了对领域对象的持久化和查询方法。
- 仓库接口通常包含根据特定条件查询领域对象的方法,如根据ID查询、根据条件查询等。
- 仓库接口提供了抽象的方法,用于与具体的数据访问对象进行交互。
- RepoImpl(仓库实现类):
- 仓库实现类是仓库接口的具体实现。
- 仓库实现类负责将仓库接口定义的方法与具体的数据访问对象(DAO)进行关联。
- 仓库实现类实现了仓库接口中定义的方法,并根据需要调用相应的DAO方法。
- Mapper(映射器):
- 映射器是一种用于将持久化对象与数据库表之间进行映射的工具。
- 映射器可以根据配置文件或注解来定义对象与表之间的映射关系。
- 映射器可以将持久化对象的属性映射到数据库表的列,并提供了CRUD操作的方法
联表查询接口
• Controller
@GetMapping("status")
@ApiOperation("公告状态")
@Logger(menu = "首页", action = "公告状态")
public Result noticeStatus() {
List<SystemNoticeResponse> responses = systemNoticeService.SystemNoticeStatus();
return Result.succ(responses);
}
• Service
/**
* 公告状态
*
* @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):数据传输对象
@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("内容")
private String noticeContent;
@ApiModelProperty("创建时间")
private Date createAt;
@ApiModelProperty("阅读状态, 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:用于获取(查询)资源,不应该用于修改数据(数据库获取)
- @PostMapping:用于创建资源,不应该用于查询数据(数据库编辑、修改)
Request和Response辨析
前端(客户端)—— 后端(服务器端)
- Request(请求):客户端向服务器发送的一种信息,用于请求操作或获取资源( 前端 ==》后端 )
- Response(响应):Response是服务器对客户端请求的回应,包含服务器处理结果的数据( 后端 ==》前端 )
🌼 结语:创作不易,如果觉得博主的文章赏心悦目,还请——
点赞
👍收藏
⭐️评论
📝