跨域+模糊分页查询
This commit is contained in:
7
pom.xml
7
pom.xml
@@ -67,6 +67,13 @@
|
||||
<version>4.3.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.pagehelper</groupId>
|
||||
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
||||
<version>1.3.0</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- JWT依赖(生成/解析令牌) -->
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
|
||||
15
src/main/java/com/genepioneer/common/result/PageResult.java
Normal file
15
src/main/java/com/genepioneer/common/result/PageResult.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package com.genepioneer.common.result;
|
||||
|
||||
import lombok.Data;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class PageResult<T> {
|
||||
private long total;
|
||||
private List<T> records;
|
||||
|
||||
public PageResult(long total, List<T> records) {
|
||||
this.total = total;
|
||||
this.records = records;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ 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.pojo.dto.LiteraturePageQueryDTO;
|
||||
import com.genepioneer.service.LiteratureService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
@@ -47,13 +48,10 @@ public class LiteratureController {
|
||||
}
|
||||
|
||||
//分页查询
|
||||
@GetMapping("/list/{page}/{size}")
|
||||
@GetMapping("/page")
|
||||
@Operation(summary = "分页查询文献")
|
||||
public Result<PageResult<LiteratureDTO>> listLiterature(
|
||||
@PathVariable int page,
|
||||
@PathVariable int size
|
||||
) {
|
||||
PageResult<LiteratureDTO> result = literatureService.pageQuery(page, size);
|
||||
public Result<PageResult<LiteratureDTO>> page(LiteraturePageQueryDTO queryDTO) {
|
||||
PageResult<LiteratureDTO> result = literatureService.pageQuery(queryDTO);
|
||||
return Result.success(result);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.genepioneer.mapper;
|
||||
|
||||
import com.genepioneer.pojo.dto.LiteraturePageQueryDTO;
|
||||
import com.genepioneer.pojo.entity.LiteratureInfo;
|
||||
import com.github.pagehelper.Page;
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
@@ -21,11 +23,5 @@ public interface LiteratureMapper {
|
||||
@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);
|
||||
Page<LiteratureInfo> pageQuery(LiteraturePageQueryDTO queryDTO);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.genepioneer.pojo.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Data
|
||||
public class LiteraturePageQueryDTO {
|
||||
private Integer page;
|
||||
private Integer pageSize;
|
||||
private String articleTitle;
|
||||
private Integer year;
|
||||
private String journalName;
|
||||
private String literatureType;
|
||||
private String sampleSource;
|
||||
private BigDecimal impactFactorMin;
|
||||
private BigDecimal impactFactorMax;
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package com.genepioneer.service;
|
||||
|
||||
import com.genepioneer.common.result.PageResult;
|
||||
import com.genepioneer.pojo.dto.LiteratureDTO;
|
||||
import com.genepioneer.pojo.dto.LiteraturePageQueryDTO;
|
||||
|
||||
public interface LiteratureService {
|
||||
// 新增文献
|
||||
@@ -11,6 +12,6 @@ public interface LiteratureService {
|
||||
void deleteLiterature(Long id);
|
||||
|
||||
// 文献分页查询
|
||||
PageResult<LiteratureDTO> pageQuery(int page, int size);
|
||||
PageResult<LiteratureDTO> pageQuery(LiteraturePageQueryDTO queryDTO);
|
||||
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
package com.genepioneer.service.impl;
|
||||
|
||||
import com.github.pagehelper.Page;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.genepioneer.common.result.PageResult;
|
||||
import com.genepioneer.pojo.dto.LiteratureDTO;
|
||||
import com.genepioneer.pojo.dto.LiteraturePageQueryDTO;
|
||||
import com.genepioneer.pojo.entity.LiteratureInfo;
|
||||
import com.genepioneer.mapper.LiteratureMapper;
|
||||
import com.genepioneer.service.LiteratureService;
|
||||
@@ -44,25 +46,26 @@ public class LiteratureServiceImpl implements LiteratureService {
|
||||
|
||||
/**
|
||||
* 文献分页查询
|
||||
* @param page
|
||||
* @param size
|
||||
* @param queryDTO
|
||||
* @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);
|
||||
public PageResult<LiteratureDTO> pageQuery(LiteraturePageQueryDTO queryDTO) {
|
||||
|
||||
// PO->DTO
|
||||
List<LiteratureDTO> dtoList = infoList.stream().map(info -> {
|
||||
// 1) PageHelper 开始分页
|
||||
PageHelper.startPage(queryDTO.getPage(), queryDTO.getPageSize());
|
||||
|
||||
// 2) 执行 mapper 查询(会被 PageHelper 自动加 limit)
|
||||
Page<LiteratureInfo> page = literatureMapper.pageQuery(queryDTO);
|
||||
|
||||
// 3) 取 total + records
|
||||
long total = page.getTotal();
|
||||
List<LiteratureDTO> records = page.getResult().stream().map(info -> {
|
||||
LiteratureDTO dto = new LiteratureDTO();
|
||||
BeanUtils.copyProperties(info, dto);
|
||||
return dto;
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
return new PageResult<>(total, dtoList);
|
||||
return new PageResult<>(total, records);
|
||||
}
|
||||
}
|
||||
29
src/main/resources/mapper/LiteratureMapper.xml
Normal file
29
src/main/resources/mapper/LiteratureMapper.xml
Normal file
@@ -0,0 +1,29 @@
|
||||
<?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.genepioneer.mapper.LiteratureMapper">
|
||||
|
||||
<select id="pageQuery" resultType="com.genepioneer.pojo.entity.LiteratureInfo">
|
||||
select *
|
||||
from literature_info
|
||||
<where>
|
||||
<if test="year != null">
|
||||
and year = #{year}
|
||||
</if>
|
||||
<if test="articleTitle != null and articleTitle != ''">
|
||||
and article_title like concat('%', #{articleTitle}, '%')
|
||||
</if>
|
||||
<if test="journalName != null and journalName != ''">
|
||||
and journal_name like concat('%', #{journalName}, '%')
|
||||
</if>
|
||||
<if test="literatureType != null and literatureType != ''">
|
||||
and literature_type = #{literatureType}
|
||||
</if>
|
||||
<if test="sampleSource != null and sampleSource != ''">
|
||||
and sample_source like concat('%', #{sampleSource}, '%')
|
||||
</if>
|
||||
</where>
|
||||
order by id desc
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user