分页查询,树形查询后端Spring

PolicyController

package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Policy;
import com.example.demo.service.PolicyService;
import com.example.demo.service.PolicyTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/policy")
public class PolicyController {
    @Autowired
    private PolicyService policyService;
    
    @Autowired
    private PolicyTypeService policyTypeService;

    @GetMapping("/search")
    public Page<Policy> search(
            @RequestParam(defaultValue = "") String keyword,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        
        Page<Policy> page = new Page<>(current, size);
        LambdaQueryWrapper<Policy> wrapper = new LambdaQueryWrapper<>();
        
        // 模糊匹配名称、关键词或政策文本
        wrapper.like(Policy::getName, keyword)
                .or()
                .like(Policy::getKeyword, keyword)
                .or()
                .like(Policy::getText, keyword);
        
        return policyService.page(page, wrapper);
    }

    @GetMapping("/{id}")
    public ResponseEntity<?> getById(@PathVariable Long id) {
        Policy policy = policyService.getById(id);
        if (policy == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(policy);
    }

    @GetMapping("/types")
    public ResponseEntity<?> getTypeTree() {
        return ResponseEntity.ok(policyTypeService.getTypeTree());
    }

    @GetMapping("/search/byType")
    public Page<Policy> searchByType(
            @RequestParam String type,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        
        Page<Policy> page = new Page<>(current, size);
        LambdaQueryWrapper<Policy> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Policy::getType, type);
        
        return policyService.page(page, wrapper);
    }

    @GetMapping("/search/byTypes")
    public Page<Policy> searchByTypes(
            @RequestParam String types,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        
        List<String> typeList = Arrays.asList(types.split(","));
        
        Page<Policy> page = new Page<>(current, size);
        LambdaQueryWrapper<Policy> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Policy::getType, typeList);
        
        return policyService.page(page, wrapper);
    }
}

Policy

package com.example.demo.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;

import java.util.Date;

@Data
public class Policy {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String name;
    private String type;
    private String category;
    @TableField("`range`")
    private String range;
    private String document;
    private String form;
    private String organ;
    private Date viadata;
    private Date pubdata;
    private Date perdata;
    @TableField("`field`")
    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;
    @TableField("`rank`")
    private String rank;
    private String policykey;
    private String newrank;
    @TableField("`year`")
    private String year;
    private String newkey;
    private String secondtheme;
    private Integer allsum;

    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;
    }
}

PolicyType

package com.example.demo.entity;

import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;

@Data
public class PolicyType {
    @TableId
    private String typeId;
    private String type;
    private String parentId;

    public String getTypeId() {
        return typeId;
    }

    public void setTypeId(String typeId) {
        this.typeId = typeId;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }
}

PolicyMapper

package com.example.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.Policy;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface PolicyMapper extends BaseMapper<Policy> {
}

PolicyTypeMapper

package com.example.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.PolicyType;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface PolicyTypeMapper extends BaseMapper<PolicyType> {
}

PolicyServiceImpl

package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Policy;
import com.example.demo.mapper.PolicyMapper;
import com.example.demo.service.PolicyService;
import org.springframework.stereotype.Service;

@Service
public class PolicyServiceImpl extends ServiceImpl<PolicyMapper, Policy> implements PolicyService {
}

PolicyTypeServiceImpl

package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.PolicyType;
import com.example.demo.mapper.PolicyTypeMapper;
import com.example.demo.service.PolicyTypeService;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class PolicyTypeServiceImpl extends ServiceImpl<PolicyTypeMapper, PolicyType> implements PolicyTypeService {
    
    @Override
    public List<Map<String, Object>> getTypeTree() {
        List<PolicyType> allTypes = this.list();
        return buildTree(allTypes, null);
    }

    private List<Map<String, Object>> buildTree(List<PolicyType> allTypes, String parentId) {
        List<Map<String, Object>> tree = new ArrayList<>();
        
        for (PolicyType type : allTypes) {
            if ((parentId == null && type.getParentId() == null) || 
                (parentId != null && parentId.equals(type.getParentId()))) {
                Map<String, Object> node = new HashMap<>();
                node.put("id", type.getTypeId());
                node.put("label", type.getType());
                
                List<Map<String, Object>> children = buildTree(allTypes, type.getTypeId());
                if (!children.isEmpty()) {
                    node.put("children", children);
                }
                
                tree.add(node);
            }
        }
        
        return tree;
    }
}

PolicyService

package com.example.demo.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo.entity.Policy;

public interface PolicyService extends IService<Policy> {
}

PolicyTypeService

package com.example.demo.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo.entity.Policy;

public interface PolicyService extends IService<Policy> {
}

DemoApplication

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}
posted @ 2025-03-30 15:14  QixunQiu  阅读(32)  评论(0)    收藏  举报