1.公告接口增删改查实现
This commit is contained in:
parent
9af991348f
commit
a9d195ed96
|
|
@ -0,0 +1,41 @@
|
|||
package com.sqx.modules.announcement.controller;
|
||||
|
||||
import com.sqx.common.utils.Result;
|
||||
import com.sqx.modules.announcement.service.AnnouncementService;
|
||||
import com.sqx.modules.common.dto.AddAnnouncementDTO;
|
||||
import com.sqx.modules.common.dto.DeleteAnnouncementDTO;
|
||||
import com.sqx.modules.common.dto.UpdateAnnouncementDTO;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/announcement")
|
||||
public class AnnouncementController {
|
||||
|
||||
private final AnnouncementService announcementService;
|
||||
|
||||
public AnnouncementController(AnnouncementService announcementService) {
|
||||
this.announcementService = announcementService;
|
||||
}
|
||||
|
||||
@GetMapping("/list")
|
||||
public Result list(@RequestParam(required = false) String title, @RequestParam(required = false) Integer state, @RequestParam(required = false) Integer id) {
|
||||
return Result.success().put("data", announcementService.listInfo(title, state, id));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public Result add(@RequestBody @Validated AddAnnouncementDTO announcementDTO) {
|
||||
return Result.success().put("data", announcementService.add(announcementDTO));
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public Result update(@RequestBody @Validated UpdateAnnouncementDTO updateAnnouncementDTO) {
|
||||
return Result.success().put("data", announcementService.updateInfo(updateAnnouncementDTO));
|
||||
}
|
||||
|
||||
@DeleteMapping
|
||||
public Result delete(@RequestBody @Validated DeleteAnnouncementDTO deleteAnnouncementDTO) {
|
||||
return Result.success().put("data", announcementService.removeById(deleteAnnouncementDTO.getId()));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
package com.sqx.modules.announcement.controller.app;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.sqx.common.utils.Result;
|
||||
import com.sqx.modules.announcement.entity.Announcement;
|
||||
import com.sqx.modules.announcement.service.AnnouncementService;
|
||||
import com.sqx.modules.common.dto.AddAnnouncementDTO;
|
||||
import com.sqx.modules.common.dto.DeleteAnnouncementDTO;
|
||||
import com.sqx.modules.common.dto.UpdateAnnouncementDTO;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/app/announcement")
|
||||
public class AppAnnouncementController {
|
||||
|
||||
private final AnnouncementService announcementService;
|
||||
|
||||
public AppAnnouncementController(AnnouncementService announcementService) {
|
||||
this.announcementService = announcementService;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public Result get() {
|
||||
List<Announcement> records = announcementService.page(new Page<>(1, 1), new LambdaQueryWrapper<Announcement>()
|
||||
.orderByDesc(Announcement::getCreateTime)).getRecords();
|
||||
return Result.success().put("data", records.isEmpty() ? null : records.get(0));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,101 @@
|
|||
package com.sqx.modules.announcement.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 公告表
|
||||
* @TableName announcement
|
||||
*/
|
||||
@TableName(value ="announcement")
|
||||
@Data
|
||||
public class Announcement implements Serializable {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Integer id;
|
||||
|
||||
/**
|
||||
* 标题
|
||||
*/
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 内容
|
||||
*/
|
||||
private String content;
|
||||
|
||||
/**
|
||||
* 状态0关闭 1打开
|
||||
*/
|
||||
private Integer state;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
* 修改时间
|
||||
*/
|
||||
private Date updateTime;
|
||||
|
||||
@TableField(exist = false)
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public boolean equals(Object that) {
|
||||
if (this == that) {
|
||||
return true;
|
||||
}
|
||||
if (that == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != that.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Announcement other = (Announcement) that;
|
||||
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
|
||||
&& (this.getTitle() == null ? other.getTitle() == null : this.getTitle().equals(other.getTitle()))
|
||||
&& (this.getContent() == null ? other.getContent() == null : this.getContent().equals(other.getContent()))
|
||||
&& (this.getState() == null ? other.getState() == null : this.getState().equals(other.getState()))
|
||||
&& (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime()))
|
||||
&& (this.getUpdateTime() == null ? other.getUpdateTime() == null : this.getUpdateTime().equals(other.getUpdateTime()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
|
||||
result = prime * result + ((getTitle() == null) ? 0 : getTitle().hashCode());
|
||||
result = prime * result + ((getContent() == null) ? 0 : getContent().hashCode());
|
||||
result = prime * result + ((getState() == null) ? 0 : getState().hashCode());
|
||||
result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode());
|
||||
result = prime * result + ((getUpdateTime() == null) ? 0 : getUpdateTime().hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(getClass().getSimpleName());
|
||||
sb.append(" [");
|
||||
sb.append("Hash = ").append(hashCode());
|
||||
sb.append(", id=").append(id);
|
||||
sb.append(", title=").append(title);
|
||||
sb.append(", content=").append(content);
|
||||
sb.append(", state=").append(state);
|
||||
sb.append(", createTime=").append(createTime);
|
||||
sb.append(", updateTime=").append(updateTime);
|
||||
sb.append(", serialVersionUID=").append(serialVersionUID);
|
||||
sb.append("]");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
package com.sqx.modules.announcement.mapper;
|
||||
|
||||
import com.sqx.modules.announcement.entity.Announcement;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
* @description 针对表【announcement(公告表)】的数据库操作Mapper
|
||||
* @createDate 2025-01-02 14:33:44
|
||||
* @Entity com.sqx.modules.announcement.entity.Announcement
|
||||
*/
|
||||
@Mapper
|
||||
public interface AnnouncementMapper extends BaseMapper<Announcement> {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
package com.sqx.modules.announcement.service;
|
||||
|
||||
import com.sqx.modules.announcement.entity.Announcement;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.sqx.modules.common.dto.AddAnnouncementDTO;
|
||||
import com.sqx.modules.common.dto.DeleteAnnouncementDTO;
|
||||
import com.sqx.modules.common.dto.UpdateAnnouncementDTO;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
* @description 针对表【announcement(公告表)】的数据库操作Service
|
||||
* @createDate 2025-01-02 14:33:44
|
||||
*/
|
||||
public interface AnnouncementService extends IService<Announcement> {
|
||||
|
||||
Object add(AddAnnouncementDTO announcementDTO);
|
||||
|
||||
Object updateInfo(UpdateAnnouncementDTO updateAnnouncementDTO);
|
||||
|
||||
Object listInfo(String title, Integer state, Integer id);
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
package com.sqx.modules.announcement.service.impl;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.sqx.common.exception.SqxException;
|
||||
import com.sqx.modules.announcement.entity.Announcement;
|
||||
import com.sqx.modules.announcement.service.AnnouncementService;
|
||||
import com.sqx.modules.announcement.mapper.AnnouncementMapper;
|
||||
import com.sqx.modules.common.dto.AddAnnouncementDTO;
|
||||
import com.sqx.modules.common.dto.UpdateAnnouncementDTO;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
* @description 针对表【announcement(公告表)】的数据库操作Service实现
|
||||
* @createDate 2025-01-02 14:33:44
|
||||
*/
|
||||
@Service
|
||||
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, Announcement>
|
||||
implements AnnouncementService{
|
||||
|
||||
@Override
|
||||
public Object listInfo(String title, Integer state, Integer id) {
|
||||
LambdaQueryWrapper<Announcement> queryWrapper = new LambdaQueryWrapper<>();
|
||||
boolean flag = false;
|
||||
if (StrUtil.isNotBlank(title)) {
|
||||
queryWrapper.like(Announcement::getTitle, title);
|
||||
flag = true;
|
||||
}
|
||||
if (title != null) {
|
||||
queryWrapper.eq(Announcement::getState, state);
|
||||
flag = true;
|
||||
}
|
||||
if (id != null) {
|
||||
queryWrapper.eq(Announcement::getId, id);
|
||||
}
|
||||
return flag ? list(queryWrapper) : list();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object add(AddAnnouncementDTO announcementDTO) {
|
||||
Announcement announcement = new Announcement();
|
||||
BeanUtil.copyProperties(announcementDTO, announcement);
|
||||
announcement.setCreateTime(DateUtil.date());
|
||||
return announcement;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object updateInfo(UpdateAnnouncementDTO updateAnnouncementDTO) {
|
||||
Announcement announcement = getById(updateAnnouncementDTO.getId());
|
||||
if (announcement == null) {
|
||||
throw new SqxException("公告不存在");
|
||||
}
|
||||
BeanUtil.copyProperties(updateAnnouncementDTO, announcement);
|
||||
announcement.setUpdateTime(DateUtil.date());
|
||||
return updateById(announcement);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
package com.sqx.modules.common.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
@Data
|
||||
public class AddAnnouncementDTO {
|
||||
@NotBlank
|
||||
private String title;
|
||||
@NotBlank
|
||||
private String content;
|
||||
@NotNull
|
||||
private Integer state;
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package com.sqx.modules.common.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class AnnouncementDTO {
|
||||
private String title;
|
||||
private String content;
|
||||
private Integer state;
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package com.sqx.modules.common.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
@Data
|
||||
public class DeleteAnnouncementDTO {
|
||||
@NotNull
|
||||
private Integer id;
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
package com.sqx.modules.common.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
@Data
|
||||
public class UpdateAnnouncementDTO {
|
||||
@NotNull
|
||||
private Integer id;
|
||||
private String title;
|
||||
private String content;
|
||||
private Integer state;
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.sqx.modules.announcement.mapper.AnnouncementMapper">
|
||||
|
||||
<resultMap id="BaseResultMap" type="com.sqx.modules.announcement.entity.Announcement">
|
||||
<id property="id" column="id" jdbcType="INTEGER"/>
|
||||
<result property="title" column="title" jdbcType="VARCHAR"/>
|
||||
<result property="content" column="content" jdbcType="VARCHAR"/>
|
||||
<result property="state" column="state" jdbcType="TINYINT"/>
|
||||
<result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
|
||||
<result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
id,title,content,
|
||||
state,create_time,update_time
|
||||
</sql>
|
||||
</mapper>
|
||||
Loading…
Reference in New Issue