6.8
个人作业 科技查询
package com.example.news1.controller;
import com.example.news1.mapper.Mapper;
import com.example.news1.pojo.Policy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@CrossOrigin(origins = "*")
@RestController
public class Controller {
@Autowired
Mapper mapper;
@GetMapping("/selectall")
List<Policy> selectall() {
return mapper.selectAll();
}
@GetMapping("/selectbyname")
List<Policy> selectByname(String name) {
return mapper.selectByname(name);
}
// @PostMapping("/selectby")
// public List<Policy> selectby(@RequestBody Map<String, String> query) {
// String name = query.get("name");
// String organ = query.get("organ");
// String document = query.get("document");
// String text = query.get("text");
// System.out.println(organ);
// // 调用 mapper.selectby 方法进行综合查询
// return mapper.selectby(name, organ, document, text);
// }
@GetMapping("/selectByPage")
public PageResult<Policy> selectByPage(@RequestParam(defaultValue = "0") int pageNum,
@RequestParam(defaultValue = "10") int pageSize) {
int offset = pageNum * pageSize;
List<Policy> policies = mapper.selectByPage(offset, pageSize);
int total = mapper.countAll(); // 假设有一个countAll方法来获取总数
return new PageResult<>(policies, total);
}
// 分页查询(带条件)
@PostMapping("/selectByConditionAndPage")
public PageResult<Policy> selectByConditionAndPage(@RequestBody Map<String, String> query,
@RequestParam(defaultValue = "0") int pageNum,
@RequestParam(defaultValue = "10") int pageSize) {
int offset = pageNum * pageSize;
String name = query.getOrDefault("name", "");
String organ = query.getOrDefault("organ", "");
String document = query.getOrDefault("document", "");
String text = query.getOrDefault("text", "");
String namec = query.getOrDefault("namec", "");
String documentc = query.getOrDefault("document", "");
String textc = query.getOrDefault("textc", "");
List<Policy> policies;
if ("精确".equals(namec)) {
policies = mapper.selectByname(name);
} else {
// 执行模糊查询的逻辑,比如调用相应的方法来执行模糊查询
policies = mapper.selectByname(name);
}
List<Policy> organPolicies = mapper.selectByConditionAndPagec("", organ, document, text, offset, pageSize);
policies.addAll(organPolicies); // 合并结果
int total = mapper.countByCondition(name, organ, document, text); // 使用计数方法
return new PageResult<>(policies, total);
}
}
package com.example.news1.controller;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@NoArgsConstructor
public class PageResult<T> {
private List<T> content;
private int total;
public PageResult(List<T> content, int total) {
this.content = content;
this.total = total;
}
public List<T> getContent() {
return content;
}
public void setContent(List<T> content) {
this.content = content;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
// getter and setter
}
package com.example.news1.mapper;
import com.example.news1.pojo.Policy;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@org.apache.ibatis.annotations.Mapper
public interface Mapper {
List<Policy> selectAll();
List<Policy> selectByname(String name);
List<Policy> selectByPage(@Param("offset") int offset, @Param("pageSize") int pageSize);
// 增加一个方法来获取满足条件的记录总数
int countByCondition(@Param("name") String name,
@Param("organ") String organ,
@Param("document") String document,
@Param("text") String text);
// 修改原有的selectby方法,利用动态SQL支持条件查询
List<Policy> selectByCondition(@Param("name") String name,
@Param("organ") String organ,
@Param("document") String document,
@Param("text") String text);
// 分页查询(带条件),结合之前的countByCondition方法和分页逻辑
List<Policy> selectByConditionAndPagec(@Param("name") String name,
@Param("organ") String organ,
@Param("document") String document,
@Param("text") String text,
@Param("offset") int offset,
@Param("pageSize") int pageSize);
List<Policy> selectByConditionAndPage(@Param("name") String name,
@Param("organ") String organ,
@Param("document") String document,
@Param("text") String text,
@Param("offset") int offset,
@Param("pageSize") int pageSize);
@Select("SELECT COUNT(*) FROM policy")
int countAll();
}
package com.example.news1.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class PageInfo<T> {
private int pageNum; // 当前页码
private int pageSize; // 每页数量
private long total; // 总记录数
private List<T> list; // 数据列表
// 构造方法、getter和setter省略...
}
package com.example.news1.pojo;
import java.util.Date;
public class Policy {
private Long id;
private String name;
private String type;
private String category;
private String range;
private String document;
private String form;
private String organ;
private Date viadata;
private Date pubdata;
private Date perdata;
private String field;
private String theme;
private String keyword;
private String superior;
private String precursor;
private String succeed;
private String state;
private String text;
private String pdf;
private String redundancy;
private String rank;
private String policykey;
private String newrank;
private String year;
private String newkey;
private String secondtheme;
private Integer allsum;
// Getters and Setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getRange() {
return range;
}
public void setRange(String range) {
this.range = range;
}
public String getDocument() {
return document;
}
public void setDocument(String document) {
this.document = document;
}
public String getForm() {
return form;
}
public void setForm(String form) {
this.form = form;
}
public String getOrgan() {
return organ;
}
public void setOrgan(String organ) {
this.organ = organ;
}
public Date getViadata() {
return viadata;
}
public void setViadata(Date viadata) {
this.viadata = viadata;
}
public Date getPubdata() {
return pubdata;
}
public void setPubdata(Date pubdata) {
this.pubdata = pubdata;
}
public Date getPerdata() {
return perdata;
}
public void setPerdata(Date perdata) {
this.perdata = perdata;
}
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
public String getTheme() {
return theme;
}
public void setTheme(String theme) {
this.theme = theme;
}
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
public String getSuperior() {
return superior;
}
public void setSuperior(String superior) {
this.superior = superior;
}
public String getPrecursor() {
return precursor;
}
public void setPrecursor(String precursor) {
this.precursor = precursor;
}
public String getSucceed() {
return succeed;
}
public void setSucceed(String succeed) {
this.succeed = succeed;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getPdf() {
return pdf;
}
public void setPdf(String pdf) {
this.pdf = pdf;
}
public String getRedundancy() {
return redundancy;
}
public void setRedundancy(String redundancy) {
this.redundancy = redundancy;
}
public String getRank() {
return rank;
}
public void setRank(String rank) {
this.rank = rank;
}
public String getPolicykey() {
return policykey;
}
public void setPolicykey(String policykey) {
this.policykey = policykey;
}
public String getNewrank() {
return newrank;
}
public void setNewrank(String newrank) {
this.newrank = newrank;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getNewkey() {
return newkey;
}
public void setNewkey(String newkey) {
this.newkey = newkey;
}
public String getSecondtheme() {
return secondtheme;
}
public void setSecondtheme(String secondtheme) {
this.secondtheme = secondtheme;
}
public Integer getAllsum() {
return allsum;
}
public void setAllsum(Integer allsum) {
this.allsum = allsum;
}
}
<?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.example.news1.mapper.Mapper">
<!-- 全部查询 -->
<select id="selectAll" resultType="com.example.news1.pojo.Policy">
SELECT * FROM policy
</select>
<select id="selectByname" resultType="com.example.news1.pojo.Policy">
SELECT * FROM policy where name LIKE concat('%', #{name}, '%')
</select>
<!-- 分页查询 -->
<select id="selectByPage" resultMap="policyResultMap">
SELECT * FROM policy
LIMIT #{pageSize} OFFSET #{offset}
</select>
<!-- 条件计数查询 -->
<select id="countByCondition" resultType="int">
SELECT COUNT(*) FROM policy
<where>
<if test="name != null">AND name LIKE CONCAT('%', #{name}, '%')</if>
<if test="organ != null">AND organ LIKE CONCAT('%', #{organ}, '%')</if>
<if test="document != null">AND document LIKE CONCAT('%', #{document}, '%')</if>
<if test="text != null">AND text LIKE CONCAT('%', #{text}, '%')</if>
</where>
</select>
<!-- 条件查询 -->
<select id="selectByCondition" resultMap="policyResultMap">
SELECT * FROM policy
<where>
<if test="name != null">AND name LIKE CONCAT('%', #{name}, '%')</if>
<if test="organ != null">AND organ LIKE CONCAT('%', #{organ}, '%')</if>
<if test="document != null">AND document LIKE CONCAT('%', #{document}, '%')</if>
<if test="text != null">AND text LIKE CONCAT('%', #{text}, '%')</if>
</where>
</select>
<!-- 分页+条件查询 -->
<select id="selectByConditionAndPage" resultMap="policyResultMap">
SELECT * FROM policy
<where>
<if test="name != null">AND name LIKE CONCAT('%', #{name}, '%')</if>
<if test="organ != null">AND organ LIKE CONCAT('%', #{organ}, '%')</if>
<if test="document != null">AND document LIKE CONCAT('%', #{document}, '%')</if>
<if test="text != null">AND text LIKE CONCAT('%', #{text}, '%')</if>
</where>
LIMIT #{pageSize} OFFSET #{offset}
</select>
<select id="selectByConditionAndPagec" resultMap="policyResultMap">
SELECT * FROM policy
<where>
<if test="name != null">AND name= CONCAT('%', #{name}, '%')</if>
<if test="organ != null">AND organ LIKE CONCAT('%', #{organ}, '%')</if>
<if test="document != null">AND document LIKE CONCAT('%', #{document}, '%')</if>
<if test="text != null">AND text LIKE CONCAT('%', #{text}, '%')</if>
</where>
LIMIT #{pageSize} OFFSET #{offset}
</select>
<!-- resultMap定义,如果在其他地方已有定义,则无需重复 -->
<resultMap id="policyResultMap" type="com.example.news1.pojo.Policy">
<id property="id" column="policy_id"/>
<!-- 字符串类型属性映射 -->
<result property="name" column="name"/>
<result property="type" column="type"/>
<result property="category" column="category"/>
<result property="range" column="range"/>
<result property="document" column="document"/>
<result property="form" column="form"/>
<result property="organ" column="organ"/>
<result property="field" column="field"/>
<result property="theme" column="theme"/>
<result property="keyword" column="keyword"/>
<result property="superior" column="superior"/>
<result property="precursor" column="precursor"/>
<result property="succeed" column="succeed"/>
<result property="state" column="state"/>
<result property="text" column="text"/>
<result property="pdf" column="pdf"/>
<result property="redundancy" column="redundancy"/>
<result property="rank" column="rank"/>
<result property="policykey" column="policykey"/>
<result property="newrank" column="newrank"/>
<result property="year" column="release_ear"/>
<result property="newkey" column="newkey"/>
<result property="secondtheme" column="secondtheme"/>
<!-- 日期类型属性映射 -->
<result property="viadata" column="viadata" javaType="java.util.Date" />
<result property="pubdata" column="pubdata" javaType="java.util.Date"/>
<result property="perdata" column="perdata" javaType="java.util.Date"/>
<!-- 整型属性映射 -->
<result property="allsum" column="allsum"/>
</resultMap>
</mapper>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义