今天改进了地铁系统
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <link href="demo.css" rel="stylesheet"> <body> <h1 style="text-align: center">地铁线路查询</h1> <div id="tabsHead"> <a id="tabs1" class="curtab" href="javascript:showTab('tabs1','tabContent1')">线路查询</a> <a id="tabs2" class="tabs" href="javascript:showTab('tabs2','tabContent2')">站点查询</a> <a id="tabs3" class="tabs" href="javascript:showTab('tabs3','tabContent3')">路径查询</a> </div> <div id="app"> <div id="tabDiv" style="width: 1000px"> <div id="tabContent1" style="display: block"> <form action="" method="get"> 输入要查询的线路号:<input v-model="subwayLine" name="line"> <input type="button" @click="selectLine()" value="查询"> </form> <table id="subway1" border="1" cellspacing="0" width="100%"> <tr> <th>序号</th> <th>车站名称</th> <th>相邻车站</th> </tr> <tr v-for="(subway,i) in subways1" align="center"> <td>{{i + 1}}</td> <td>{{subway.station}}</td> <td>{{subway.next1}} {{subway.next2}} {{subway.next3}} {{subway.next4}} {{subway.next5}} </td> </tr> </table> </div> <div id="tabContent2" style="display: none"> <form action="" method="get"> 输入要查询的站名:<input v-model="subwayStation" name="station"> <input type="button" @click="selectStation()" value="查询"> </form> <table id="subway2" border="1" cellspacing="0" width="100%"> <tr> <th>线路名称</th> </tr> <tr v-for="(subway,i) in subways2" align="center"> <td>{{subway.line}}</td> </tr> </table> </div> <div id="tabContent3" style="display: none"> <form action="" method="get"> 起始站:<input v-model="subwayStation" name="start"> 终点站:<input v-model="subwayStation2" name="end"> <input type="button" @click="selectAll()" value="查询"> </form> <table id="subway3" border="1" cellspacing="0" width="100%"> <tr> <th>最优路径</th> </tr> <tr align="center"> <td>{{path}}</td> </tr> </table> </div> </div> </div> </body> <script src="js/axios-0.18.0.js"></script> <script src="js/vue.js"></script> <script> new Vue({ el: "#app", data() { return { subwayLine: '', subwayStation:'', subwayStation2:'', subways1:[], subways2:[], path:'', } }, methods: { selectLine() { var _this = this; axios({ method: "get", url: "http://localhost:8080/subway/way/selectLine?id=" + _this.subwayLine, }).then(function (resp) { _this.subways1 = resp.data; }) }, selectStation() { var _this = this; axios({ method: "get", url: "http://localhost:8080/subway/way/selectStation?station=" + _this.subwayStation, }).then(function (resp) { _this.subways2 = resp.data; }) }, selectAll() { var _this = this; axios({ method: "get", url: "http://localhost:8080/subway/way/selectAll?start=" + _this.subwayStation+"&&end="+_this.subwayStation2, }).then(function (resp) { _this.path = resp.data; }) } } }) function showTab(tabHeadId,tabContentId) { //tab层 var tabDiv = document.getElementById("tabDiv"); //将tab层中所有的内容层设为不可见 //遍历tab层下的所有子节点 var taContents = tabDiv.childNodes; for(i=0; i<taContents.length; i++) { //将所有内容层都设为不可见 if(taContents[i].id!=null && taContents[i].id != 'tabsHead') { taContents[i].style.display = 'none'; } } //将要显示的层设为可见 document.getElementById(tabContentId).style.display = 'block'; //遍历tab头中所有的超链接 var tabHeads = document.getElementById('tabsHead').getElementsByTagName('a'); for(i=0; i<tabHeads.length; i++) { //将超链接的样式设为未选的tab头样式 tabHeads[i].className='tabs'; } //将当前超链接的样式设为已选tab头样式 document.getElementById(tabHeadId).className='curtab'; document.getElementById(tabHeadId).blur(); } </script> </html>
package com.nian.web.servlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.rmi.ServerException; import java.rmi.server.ServerCloneException; public class BaseServlet extends HttpServlet { //根据请求的最后一段路径来进行方法分发 @Override protected void service(HttpServletRequest req, HttpServletResponse resp) { //1.获取资源路径 String uri = req.getRequestURI();//subway/way/selectLine //获取最后一段路径,方法名 int index = uri.lastIndexOf('/'); String methodName = uri.substring(index + 1); //2.获取subway字节码对象 Class<? extends BaseServlet> cls = this.getClass(); //3.获取Method对象 try { Method method = cls.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class); method.invoke(this, req, resp); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }
package com.nian.pojo; public class subway { private Integer id; private String line; private String station; private String next1; private String next2; private String next3; private String next4; private String next5; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLine() { return line; } public void setLine(String line) { this.line = line; } public String getStation() { return station; } public void setStation(String station) { this.station = station; } public String getNext1() { return next1; } public void setNext1(String next1) { this.next1 = next1; } public String getNext2() { return next2; } public void setNext2(String next2) { this.next2 = next2; } public String getNext3() { return next3; } public void setNext3(String next3) { this.next3 = next3; } public String getNext4() { return next4; } public void setNext4(String next4) { this.next4 = next4; } public String getNext5() { return next5; } public void setNext5(String next5) { this.next5 = next5; } }
package com.nian.web.servlet; import com.alibaba.fastjson.JSON; import com.nian.pojo.subway; import com.nian.service.impl.subwayServiceImpl; import com.nian.service.subwayService; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.BufferedReader; import java.io.IOException; import java.util.List; @WebServlet("/way/*") public class subwayServlet extends BaseServlet { private subwayService service= new subwayServiceImpl(); public void selectLine(HttpServletRequest request,HttpServletResponse response) throws IOException { //1.接收数据 String line=request.getParameter("id"); //2.调用service查询 List<subway> subways=service.selectLine(line); String jsomString= JSON.toJSONString(subways); //写入数据 response.setContentType("text/json;charset=utf-8"); response.getWriter().write(jsomString); } public void selectStation(HttpServletRequest request,HttpServletResponse response) throws IOException { //1.接收数据 String station=request.getParameter("station"); //2.调用service查询 List<subway> subways=service.selectStation(station); String jsomString= JSON.toJSONString(subways); //写入数据 response.setContentType("text/json;charset=utf-8"); response.getWriter().write(jsomString); } public void selectAll(HttpServletRequest request,HttpServletResponse response) throws IOException { //1.接收数据 String start1=request.getParameter("start"); String end1=request.getParameter("end"); int start = service.selectId(start1); int end = service.selectId(end1); //2.调用service查询 List<subway> subways = service.selectAll(); int[][] ints = service.array1(subways); String[] strings = service.array2(subways); String result = service.djst(start, end, ints, strings); String jsomString= JSON.toJSONString(result); //写入数据 response.setContentType("text/json;charset=utf-8"); response.getWriter().write(jsomString); } }
package com.nian.util; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; public class SqlSessionFactoryUtils { private static SqlSessionFactory sqlSessionFactory; static { //静态代码块会随着类的加载而自动执行,且只执行一次 try { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } public static SqlSessionFactory getSqlSessionFactory(){ return sqlSessionFactory; } }
package com.nian.mapper; import com.nian.pojo.subway; import org.apache.ibatis.annotations.Select; import java.util.List; public interface Mapper { @Select("select * from subway where line=#{line}") List<subway> selectLine(String line); @Select("select * from subway where station=#{station}") List<subway> selectStation(String station); @Select("select * from subway") List<subway> selectAll(); @Select("select distinct id from subway where station=#{station}") int selectId(String station); }