河北省科技政策查询系统(二)
首先它的多项查询就和之前写的论文查询系统很相似
想要看详细的可以在我博客园直接搜 论文查询系统
这是第一版没有树形结构查询的功能
在做树形结构查询的时候,我搜了一下基本都是递归查询,还有mybatis,综合一下网上的说法,我选用的mybatis关联多表,最终能达到想要的基本效果就行
这部分发的主要是关于多项查询的
index.jsp
<%-- Created by IntelliJ IDEA. User: 榕宝 Date: 2022/10/25 Time: 14:01 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>河北省科技政策查询系统</title> <script th:src="@{/layuimini/js/lay-module/echarts/echarts.js}"></script> <script th:src="@{/layuimini/js/lay-module/echarts/wordcloud.js}"></script> <link rel="stylesheet" type="text/css" href="https://www.layuicdn.com/layui/css/layui.css" /> <script src="https://www.layuicdn.com/layui/layui.js"></script> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="renderer" content="webkit"> <link href="css/bootstrap.min.css?v=3.3.6" rel="stylesheet"> <link href="css/font-awesome.min.css?v=4.4.0" rel="stylesheet"> <link href="css/animate.css" rel="stylesheet"> <link href="css/style.css?v=4.1.0" rel="stylesheet"> <style> .biaoti{ font-family:"幼圆";/*设置字体*/ font-size:30px;/*设置字体大小*/ font-weight:900;/*调整字体粗细*/ color:#136dab; } </style> </head> <body class="fixed-sidebar full-height-layout gray-bg" style="overflow:hidden"> <div style="width:100%;height:90px; background-color:white ;"> <div style="width: 330px;height: 80px; margin:auto;"> <p> </p> <div style="width: 70px;height: 70px;background: url(LOGO.png); background-size:cover ;text-align: center;float:left"></div> <div style="float:left"> <p class="biaoti"> 科技政策查询系统</p> </div></div> </div> <div id="wrapper" style="width:100%;height:100%;margin: auto; "> <!--左侧导航开始--> <nav class="navbar-default navbar-static-side" role="navigation"> <div class="sidebar-collapse" > <ul class="nav" id="side-menu"> <li> <a class="J_menuItem" href="layouts.html"><i class="fa fa-columns"></i> <span class="nav-label">政策查询</span></a> </li> <li> <a href="#"><i class="fa fa-flask"></i> <span class="nav-label">政策分类</span><span class="fa arrow"></span></a> <ul class="nav nav-second-level"> <li><a class="J_menuItem" href="table2.jsp?id=100">综合(87)</a> </li> <li><a class="J_menuItem" href="table2.jsp?id=200">科研机构改革(17)</a> </li> <li><a class="J_menuItem" href="table2.jsp?id=300">科研计划管理(93)</a> </li> <li><a class="J_menuItem" href="table2.jsp?id=400">科研经费与财务(39)</a> </li> <li> <a href="#">基础研究与科研基地(108) <span class="fa arrow"></span></a> <ul class="nav nav-third-level"> <li><a class="J_menuItem" href="table2.jsp?id=501">基础研究(29)</a> </li> <li><a class="J_menuItem" href="table2.jsp?id=502">平台基地(79)</a> </li> </ul> </li> <li> <a href="#">企业技术进步与高新科技(137) <span class="fa arrow"></span></a> <ul class="nav nav-third-level"> <li><a class="J_menuItem" href="table2.jsp?id=601">企业(85)</a> </li> <li><a class="J_menuItem" href="table2.jsp?id=602">产业(27)</a> </li> <li><a class="J_menuItem" href="table2.jsp?id=603">创新载体(25)</a> </li> </ul> </li> <li><a class="J_menuItem" href="table2.jsp?id=700">农村科技与社会发展(29)</a> </li> <li><a class="J_menuItem" href="table2.jsp?id=800">科技人才(39)</a> </li> <li><a class="J_menuItem" href="table2.jsp?id=900">科技中介服务(17)</a> </li> <li><a class="J_menuItem" href="table2.jsp?id=1000">科技条件与标准(2)</a> </li> <li><a class="J_menuItem" href="table2.jsp?id=1100">科技金融与税收(30)</a> </li> <li><a class="J_menuItem" href="table2.jsp?id=1200">科技成果与知识产权(84)</a> </li> <li><a class="J_menuItem" href="table2.jsp?id=1300">科技奖励(18)</a> </li> <li><a class="J_menuItem" href="table2.jsp?id=1400">科学技术普及(10)</a> </li> </ul> </li> <li> <a class="J_menuItem" href="layouts.html"><i class="fa fa-desktop"></i> <span class="nav-label">退出系统</span></a> </li> </ul> </div> </nav> <!--左侧导航结束--> <!--右侧部分开始--> <div id="page-wrapper" class="gray-bg dashbard-1"> <div class="row content-tabs"> <button class="roll-nav roll-left J_tabLeft"><i class="fa fa-backward"></i> </button> <nav class="page-tabs J_menuTabs"> <div class="page-tabs-content"> <a href="javascript:;" class="active J_menuTab" data-id="index_v1.html">首页</a> </div> </nav> <button class="roll-nav roll-right J_tabRight"><i class="fa fa-forward"></i> </button> <div class="btn-group roll-nav roll-right"> <button class="dropdown J_tabClose" data-toggle="dropdown">关闭操作<span class="caret"></span> </button> <ul role="menu" class="dropdown-menu dropdown-menu-right"> <li class="J_tabShowActive"><a>定位当前选项卡</a> </li> <li class="divider"></li> <li class="J_tabCloseAll"><a>关闭全部选项卡</a> </li> <li class="J_tabCloseOther"><a>关闭其他选项卡</a> </li> </ul> </div> <a href="login.html" class="roll-nav roll-right J_tabExit"><i class="fa fa fa-sign-out"></i> 退出</a> </div> <div class="row J_mainContent" id="content-main"> <iframe class="J_iframe" name="iframe0" width="100%" height="100%" src="../index_v1.html?v=4.0" frameborder="0" data-id="index_v1.html" seamless></iframe> </div> <div class="footer"> <div class="pull-right">© 2014-2015 <a href="http://www.zi-han.net/" target="_blank">zihan's blog</a> </div> </div> </div> <!--右侧部分结束--> <!--右侧边栏开始--> <!-- 全局js --> <script src="js/jquery.min.js?v=2.1.4"></script> <script src="js/bootstrap.min.js?v=3.3.6"></script> <script src="js/plugins/metisMenu/jquery.metisMenu.js"></script> <script src="js/plugins/slimscroll/jquery.slimscroll.min.js"></script> <script src="js/plugins/layer/layer.min.js"></script> <!-- 自定义js --> <script src="js/hplus.js?v=4.1.0"></script> <script type="text/javascript" src="js/contabs.js"></script> <!-- 第三方插件 --> <script src="js/plugins/pace/pace.min.js"></script> </div> </body> </html>
layouts.html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> <script th:src="@{/layuimini/js/lay-module/echarts/echarts.js}"></script> <script th:src="@{/layuimini/js/lay-module/echarts/wordcloud.js}"></script> <link rel="stylesheet" type="text/css" href="https://www.layuicdn.com/layui/css/layui.css" /> <script src="https://www.layuicdn.com/layui/layui.js"></script> </head> <body> <div style="width:100%;height:65px;float:left; position:relative; display: table-cell;vertical-align: middle; background-color: rgba(232,232,232,0.76) "> <form class="layui-form"action="table1.jsp" method="post" target="hideIframe1" > <p> </p> <div class="layui-form-item"> <div class="layui-inline"> <label class="layui-form-label"> 政策名称: </label> <div class="layui-input-inline"> <input type="text" name="name" class="layui-input"> </div> </div> <div class="layui-inline"> <label class="layui-form-label"> 政策文号: </label> <div class="layui-input-inline"> <input type="text" name="document" class="layui-input"> </div> </div> <div class="layui-inline"> <label class="layui-form-label"> 发文机构: </label> <div class="layui-input-inline"> <input type="text" name="organ" class="layui-input"> </div> </div> <div class="layui-inline"> <div class="layui-input-inline"> <button type="submit" class="layui-btn" lay-submit lay-filter="demo1"style="background-color: #1571b2">查询</button> </div> </div> </div> </form> </div> <div class="container3"> <iframe id="myIframe1" name="hideIframe1" style="" src="table1.jsp" frameborder="0" width="100%" height="850px"></iframe> </div> </body> </html>
table1.jsp
<%-- Created by IntelliJ IDEA. User: 榕宝 Date: 2022/10/25 Time: 15:14 To change this template use File | Settings | File Templates. --%> <%@ page import="Bean.bean" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="java.util.List" %> <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> <script th:src="@{/layuimini/js/lay-module/echarts/echarts.js}"></script> <script th:src="@{/layuimini/js/lay-module/echarts/wordcloud.js}"></script> <link rel="stylesheet" type="text/css" href="https://www.layuicdn.com/layui/css/layui.css" /> <script src="https://www.layuicdn.com/layui/layui.js"></script> </head> <jsp:useBean id="util" class="Dao.dao"/> <% request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String name=request.getParameter("name"); String document=request.getParameter("document"); String organ=request.getParameter("organ"); %> <%if (name == null||document==null||organ==null){ List<bean> list = util.searchAll(); %> <table class="layui-table"style="table-layout: fixed;word-wrap:break-word;" > <thead> <tr> <td style="width: 300px">政策名称</td> <td style="width: 150px">发文机构</td> <td style="width: 70px">颁布日期</td> <td style="width: 80px">政策分类</td> <td style="width: 100px">操作</td> </tr> </thead> <tbody> <%for (bean b : list) {%> <tr> <td style="width: 300px"><%=b.getName()%></td> <td style="width: 150px"><%=b.getOrgan()%></td> <td style="width: 70px"><%=b.getPubdata()%></td> <td style="width: 80px"><%=b.getType()%></td> <td style="width: 100px"><a href="xiangqing.jsp?id=<%=b.getId()%>"><button type="submit" class="layui-btn"style="background-color: #1571b2" >查看详情</button></a> </td> </tr> <%} %> </tbody> </table> <%}else{ List<bean> list = util.search(name,document,organ); %> <div class="container3" id="container3"> <table class="layui-table"style="table-layout: fixed;word-wrap:break-word;" > <thead> <tr> <td style="width: 200px">政策名称</td> <td style="width: 150px">发文机构</td> <td style="width: 100px">颁布日期</td> <td style="width: 150px">政策分类</td> <td style="width: 100px">操作</td> </tr> </thead> <tbody> <%for (bean b : list) {%> <tr> <td style="width: 200px"><%=b.getName()%></td> <td style="width: 150px"><%=b.getOrgan()%></td> <td style="width: 100px"><%=b.getPubdata()%></td> <td style="width: 150px"><%=b.getType()%></td> <td style="width: 100px"><a href="xiangqing.jsp?id=<%=b.getId()%>"><button type="submit" class="layui-btn"style="background-color: #1571b2">查看详情</button></a> </td> </tr> <%} %> <p><a href="javascript:history.go(-1);"><button class="layui-btn"style="background-color: #1571b2">返回</button></a> </p> </tbody> </table> </div> <%}%> </html>
Dao.dao.java
package Dao; import Bean.Menu; import Bean.bean; import Bean.policy; import DBUtil.dbutil; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.Reader; import java.sql.*; import java.util.ArrayList; import java.util.List; public class dao { public List<bean> searchAll( ){ List<bean> list = new ArrayList<bean>(); try { Connection conn = dbutil.getConn(); Statement state = null; String sql="select * from policy"; PreparedStatement pstmt = conn.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); System.out.println("搜索全部政策"); while(rs.next()){ bean lu = new bean(); lu.setId(rs.getString("id")); lu.setType(rs.getString("type")); lu.setOrgan(rs.getString("organ")); lu.setPubdata(rs.getString("pubdata")); lu.setName(rs.getString("name")); list.add(lu); } rs.close(); pstmt.close(); conn.close(); }catch(SQLException e) { System.out.println("发生错误"); e.printStackTrace(); } return list; } public List<bean> search(String name,String document,String organ){ List<bean> list = new ArrayList<bean>(); try { Connection conn = dbutil.getConn(); Statement state = null; String sql = "select * from policy where name REGEXP '"+name+"' and document REGEXP '"+document+"' and organ REGEXP '"+organ+"'"; PreparedStatement pstmt = conn.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); System.out.println("多项条件搜索运行中"); while(rs.next()){ bean lu = new bean(); lu.setId(rs.getString("id")); lu.setType(rs.getString("type")); lu.setOrgan(rs.getString("organ")); lu.setPubdata(rs.getString("pubdata")); lu.setName(rs.getString("name")); list.add(lu); } rs.close(); pstmt.close(); conn.close(); }catch(SQLException e) { System.out.println("发生错误"); e.printStackTrace(); } return list; } public static bean getOne(String id){ bean lu = new bean(); try { Connection conn = dbutil.getConn(); Statement state = null; String sql="select * from policy where id=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1,id); ResultSet rs = pstmt.executeQuery(); while(rs.next()){ lu.setId(rs.getString("id")); lu.setType(rs.getString("type")); lu.setCategory(rs.getString("category")); lu.setOrgan(rs.getString("organ")); lu.setPubdata(rs.getString("pubdata")); lu.setName(rs.getString("name")); lu.setDocument(rs.getString("document")); lu.setRange(rs.getString("range")); lu.setPerdata(rs.getString("perdata")); lu.setViadata(rs.getString("viadata")); lu.setText(rs.getString("text")); } rs.close(); pstmt.close(); conn.close(); } catch(SQLException e) { e.printStackTrace(); } return lu; } }
Bean.bean.java
package Bean; public class bean { private String id; private String name;// private String type;// private String category; private String range; private String document; private String organ;// private String viadata; private String pubdata;// private String perdata; Menu menu; public String getId() { return id; } public void setId(String 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 getOrgan() { return organ; } public void setOrgan(String organ) { this.organ = organ; } public String getViadata() { return viadata; } public void setViadata(String viadata) { this.viadata = viadata; } public String getPubdata() { return pubdata; } public void setPubdata(String pubdata) { this.pubdata = pubdata; } public String getPerdata() { return perdata; } public void setPerdata(String perdata) { this.perdata = perdata; } }
Bean.Menu.java
package Bean; import java.util.List; public class Menu { private Integer id; private String type; private List<Menu> bean; public Menu(Integer id,Integer pid,String type) { this.id =id; this.type= type; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getType() { return type; } public void setType(String type) { this.type = type; } @Override public String toString() { return "Menu{" + "id=" + id + ", type='" + type + '\'' + ", bean=" + bean + '}'; } public List<Menu> getBean(){ return bean; } public void setBean(List<Menu> bean) { this.bean = bean; } }
DBUtil.dbutil.java
package DBUtil; import java.sql.*; public class dbutil { static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; //数据库驱动名 static final String url = "jdbc:mysql://localhost:3306/policy";//数据库地址 static final String user = "root"; static final String password = "123456"; //连接数据库 public static Connection getConn () { Connection conn = null; try { Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动 conn = DriverManager.getConnection(url, user, password); } catch (Exception e) { e.printStackTrace(); } return conn; } //关闭连接 public static void close (PreparedStatement preparedState, Connection conn) { if (preparedState != null) { try { preparedState.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close (ResultSet rs, PreparedStatement preparedState, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (preparedState != null) { try { preparedState.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 关闭连接 * @param state * @param conn */ public static void close (Statement 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, Statement 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(); } } } //测试是否连接成功 public static void main(String[] args) throws SQLException { Connection conn = getConn(); PreparedStatement preparedStatement = null; ResultSet rs = null; String sql ="select * from policy limit 10";//数据库名称 preparedStatement = conn.prepareStatement(sql); rs = preparedStatement.executeQuery(); if(rs.next()){ System.out.println("数据库不为空"); } else{ System.out.println("数据库为空"); } } }
关于树形结构查询的解决方法就在mybatis关联多表(一对多) - zrswheart - 博客园 (cnblogs.com)