新增分页查询
This commit is contained in:
@@ -1,14 +1,18 @@
|
||||
package com.genepioneer.common.result;
|
||||
|
||||
import lombok.Data;
|
||||
import java.io.Serializable; // 1. 导入Serializable包
|
||||
|
||||
@Data
|
||||
public class Result<T> {
|
||||
public class Result<T> implements Serializable {
|
||||
// 注意:添加序列化版本号
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private Integer code; // 200成功,500失败
|
||||
private String msg;
|
||||
private T data;
|
||||
|
||||
// 成功返回
|
||||
// 原有方法完全保留
|
||||
public static <T> Result<T> success(T data) {
|
||||
Result<T> result = new Result<>();
|
||||
result.setCode(200);
|
||||
@@ -17,7 +21,6 @@ public class Result<T> {
|
||||
return result;
|
||||
}
|
||||
|
||||
// 失败返回
|
||||
public static <T> Result<T> error(String msg) {
|
||||
Result<T> result = new Result<>();
|
||||
result.setCode(500);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.genepioneer.controller;
|
||||
|
||||
import com.genepioneer.common.result.PageResult;
|
||||
import com.genepioneer.common.result.Result;
|
||||
import com.genepioneer.pojo.dto.LiteratureDTO;
|
||||
import com.genepioneer.service.LiteratureService;
|
||||
@@ -8,6 +9,7 @@ import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@CrossOrigin
|
||||
@RestController
|
||||
@RequestMapping("/literature")
|
||||
@Tag(name = "文献管理", description = "文献相关接口")
|
||||
@@ -31,7 +33,7 @@ public class LiteratureController {
|
||||
}
|
||||
}
|
||||
|
||||
// 新增:删除文献接口
|
||||
// 删除文献
|
||||
@DeleteMapping("/delete/{id}") // 通过路径变量接收文献ID
|
||||
@Operation(summary = "删除文献")
|
||||
public Result<String> deleteLiterature(@PathVariable Long id) { // @PathVariable绑定路径中的id参数
|
||||
@@ -43,4 +45,15 @@ public class LiteratureController {
|
||||
return Result.error("删除文献失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
//分页查询
|
||||
@GetMapping("/list/{page}/{size}")
|
||||
@Operation(summary = "分页查询文献")
|
||||
public Result<PageResult<LiteratureDTO>> listLiterature(
|
||||
@PathVariable int page,
|
||||
@PathVariable int size
|
||||
) {
|
||||
PageResult<LiteratureDTO> result = literatureService.pageQuery(page, size);
|
||||
return Result.success(result);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,9 @@ import com.genepioneer.pojo.entity.LiteratureInfo;
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface LiteratureMapper {
|
||||
@@ -17,4 +20,12 @@ public interface LiteratureMapper {
|
||||
// 根据文献ID删除数据
|
||||
@Delete("DELETE FROM literature_info WHERE id = #{id}")
|
||||
void deleteLiteratureById(Long id);
|
||||
|
||||
// 查询总数
|
||||
@Select("SELECT COUNT(*) FROM literature_info")
|
||||
int countLiterature();
|
||||
|
||||
// 分页查询
|
||||
@Select("SELECT * FROM literature_info ORDER BY id DESC LIMIT #{size} OFFSET #{offset}")
|
||||
List<LiteratureInfo> selectLiteratureByPage(int offset, int size);
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import java.math.BigDecimal;
|
||||
@Data
|
||||
public class LiteratureDTO {
|
||||
// 字段名要和前端提交的参数名一致
|
||||
private Long id;
|
||||
private Integer year;
|
||||
private String articleTitle;
|
||||
private String journalName;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.genepioneer.service;
|
||||
|
||||
import com.genepioneer.common.result.PageResult;
|
||||
import com.genepioneer.pojo.dto.LiteratureDTO;
|
||||
|
||||
public interface LiteratureService {
|
||||
@@ -8,4 +9,8 @@ public interface LiteratureService {
|
||||
|
||||
// 删除文献
|
||||
void deleteLiterature(Long id);
|
||||
|
||||
// 文献分页查询
|
||||
PageResult<LiteratureDTO> pageQuery(int page, int size);
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.genepioneer.service.impl;
|
||||
|
||||
import com.genepioneer.common.result.PageResult;
|
||||
import com.genepioneer.pojo.dto.LiteratureDTO;
|
||||
import com.genepioneer.pojo.entity.LiteratureInfo;
|
||||
import com.genepioneer.mapper.LiteratureMapper;
|
||||
@@ -8,6 +9,9 @@ import org.springframework.beans.BeanUtils; // Spring工具类,用于对象属
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class LiteratureServiceImpl implements LiteratureService {
|
||||
|
||||
@@ -37,4 +41,28 @@ public class LiteratureServiceImpl implements LiteratureService {
|
||||
public void deleteLiterature(Long id) {
|
||||
literatureMapper.deleteLiteratureById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文献分页查询
|
||||
* @param page
|
||||
* @param size
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public PageResult<LiteratureDTO> pageQuery(int page, int size) {
|
||||
// 查询总数量
|
||||
int total = literatureMapper.countLiterature();
|
||||
// 分页查询
|
||||
int offset = (page - 1) * size;
|
||||
List<LiteratureInfo> infoList = literatureMapper.selectLiteratureByPage(offset, size);
|
||||
|
||||
// PO->DTO
|
||||
List<LiteratureDTO> dtoList = infoList.stream().map(info -> {
|
||||
LiteratureDTO dto = new LiteratureDTO();
|
||||
BeanUtils.copyProperties(info, dto);
|
||||
return dto;
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
return new PageResult<>(total, dtoList);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user