3.25

package com.example;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtil {

public static Connection getConnection() throws Exception {
    Class.forName("com.mysql.cj.jdbc.Driver");
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/policy?serverTimezone=UTC&useSSL=true", "root", "123456789");
    return conn;
}

public static void close(PreparedStatement state, Connection conn) {
    if (state != null) {
        try {
            state.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    if (conn != null) {
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

public static void close(ResultSet rs, PreparedStatement state, Connection conn) {
    if (rs != null) {
        try {
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    if (state != null) {
        try {
            state.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    if (conn != null) {
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

}
package com.example.demos7.controller;

import com.example.demos7.model.Policy;
import com.example.demos7.repository.PolicyRepository;
import com.example.demos7.service.PolicyCategoryService;
import com.example.demos7.service.PolicyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@Controller
public class PolicyController {

@Autowired
private PolicyService policyService;

@Autowired
private PolicyCategoryService categoryService;

@Autowired
private PolicyRepository policyRepository;

// 主界面:展示分类树和政策列表
@GetMapping("/")
public String index(Model model,
                    @RequestParam(required = false) String categoryId,
                    @RequestParam(defaultValue = "0") int page) {
    model.addAttribute("categories", categoryService.buildCategoryTree());
    Page<Policy> policies = policyService.getPoliciesByCategory(categoryId, page);
    model.addAttribute("policies", policies);
    return "index";
}

// 高级检索界面
@GetMapping("/advanced-search")
public String advancedSearch(Model model) {
    model.addAttribute("categories", categoryService.buildCategoryTree());
    return "advanced-search";
}

// 处理高级检索请求
@PostMapping("/search")
public String search(@RequestParam String name,
                     @RequestParam String content,
                     @RequestParam String organ,
                     @RequestParam String categoryId,
                     @RequestParam String policyNumber,
                     @RequestParam(defaultValue = "0") int page,
                     Model model) {
    Page<Policy> policies = policyService.advancedSearch(
            name, content, organ, categoryId, policyNumber, page
    );
    model.addAttribute("policies", policies);
    return "index";
}

// 政策详情页
@GetMapping("/policy/{id}")
public String getPolicyDetail(@PathVariable Long id, Model model) {
    Optional<Policy> policyOptional = policyRepository.findById(id);
    if (policyOptional.isPresent()) {
        model.addAttribute("policy", policyOptional.get());
        return "policy-detail";
    } else {
        return "redirect:/"; // 若政策不存在,重定向到首页
    }
}

}
package com.example.demos7.dto;

import java.util.ArrayList;
import java.util.List;

public class TreeNode {
private String id; // 分类ID(如0100)
private String label; // 分类名称(如"综合")
private List children = new ArrayList<>(); // 子节点

// 构造方法
public TreeNode() {}

// Getter & Setter
public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getLabel() {
    return label;
}

public void setLabel(String label) {
    this.label = label;
}

public List<TreeNode> getChildren() {
    return children;
}

public void setChildren(List<TreeNode> children) {
    this.children = children;
}

}
package com.example.demos7.model;

import jakarta.persistence.*;

import java.util.Date;

@Entity
@Table(name = "policy")
public class Policy {

@Id
@Column(name = "id")
private Long id;

@Column(name = "name")
private String name;

@Column(name = "type")
private String categoryId;

@Column(name = "organ")
private String issuingAuthority;

@Column(name = "document")
private String policyNumber;

@Lob
@Column(name = "text")
private String content;

@Column(name = "pubdata")
private Date pubdata;

// 其他字段(根据数据库表结构补充)
@Column(name = "viadata")
private Date viaDate;

@Column(name = "state")
private String state;

// 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 getCategoryId() { return categoryId; }
public void setCategoryId(String categoryId) { this.categoryId = categoryId; }

public String getIssuingAuthority() { return issuingAuthority; }
public void setIssuingAuthority(String issuingAuthority) { this.issuingAuthority = issuingAuthority; }

public String getPolicyNumber() { return policyNumber; }
public void setPolicyNumber(String policyNumber) { this.policyNumber = policyNumber; }

public String getContent() { return content; }
public void setContent(String content) { this.content = content; }

public Date getPubdata() { return pubdata; }
public void setPubdata(Date pubdata) { this.pubdata = pubdata; }

public Date getViaDate() { return viaDate; }
public void setViaDate(Date viaDate) { this.viaDate = viaDate; }

public String getState() { return state; }
public void setState(String state) { this.state = state; }

}

posted @ 2025-03-25 21:51  李蕊lr  阅读(4)  评论(0)    收藏  举报