四个步骤:
1. | 配置 Servlet ,使其能响应请求。 |
2. | 编写 响应请求的类:ActionServlet.java |
3. | 编写接口 OverspeedDao.java |
4. | 编写 MyBatis 的映射文件:overspeedMapper.xml |
配置 servlet
web.xml
1 <servlet> 2 <description>This is the description of my J2EE component</description> 3 <display-name>This is the display name of my J2EE component</display-name> 4 <servlet-name>OverspeedServlet</servlet-name> 5 <servlet-class>overspeed.ActionServlet</servlet-class> 6 </servlet> 7 8 <servlet-mapping> 9 <servlet-name>OverspeedServlet</servlet-name> 10 <url-pattern>/OverspeedServlet</url-pattern> 11 </servlet-mapping>
srcoverspeedActionServlet.java
1 package overspeed; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.util.List; 6 7 import javax.servlet.RequestDispatcher; 8 import javax.servlet.ServletException; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 import javax.servlet.http.HttpSession; 13 14 import net.sf.json.JSONObject; 15 16 import com.sytz.db.TzkkDb; 17 import com.sytz.util.CZY; 18 import com.sytz.util.SYS; 19 20 public class ActionServlet extends HttpServlet { 21 22 23 static OverspeedDao overspeedDao = new OverspeedDao(); 24 static Bean bean = new Bean(); 25 26 27 /* 28 * 违法列表 29 */ 30 private static List<Bean> violationNumberList ; 31 32 33 34 TzkkDb tzkkdb=new TzkkDb(); 35 /** 36 * Constructor of the object. 37 */ 38 public ActionServlet() { 39 super(); 40 } 41 42 /** 43 * Destruction of the servlet. <br> 44 */ 45 public void destroy() { 46 super.destroy(); // Just puts "destroy" string in log 47 // Put your code here 48 } 49 50 /** 51 * The doGet method of the servlet. <br> 52 * 53 * This method is called when a form has its tag value method equals to get. 54 * 55 * @param request the request send by the client to the server 56 * @param response the response send by the server to the client 57 * @throws ServletException if an error occurred 58 * @throws IOException if an error occurred 59 */ 60 @SuppressWarnings("unused") 61 public void doGet(HttpServletRequest request, HttpServletResponse response) 62 throws ServletException, IOException { 63 64 JSONObject json = new JSONObject(); 65 json.accumulate("Method", "GET"); 66 out.println( json.toString() ); 67 68 out.flush(); 69 out.close(); 70 } 71 72 /** 73 * The doPost method of the servlet. <br> 74 * 75 * This method is called when a form has its tag value method equals to post. 76 * 77 * @param request the request send by the client to the server 78 * @param response the response send by the server to the client 79 * @throws ServletException if an error occurred 80 * @throws IOException if an error occurred 81 */ 82 public void doPost(HttpServletRequest request, HttpServletResponse response) 83 throws ServletException, IOException { 84 85 86 HttpSession session = request.getSession(); 87 // String overspeedLevel = request.getParameter("overspeedLevel"); // 超速级别 88 String overspeedNumber = request.getParameter("overspeedNumber"); // 超速编号 89 // String overspeedDescription = request.getParameter("overspeedDescription"); // 超速描述 90 String vehicleType = request.getParameter("vehicleType"); // 车辆类型 CLLX 91 92 93 // if( overspeedLevel.equals("1") ){ 94 // overspeedDescription = "20%以下"; 95 // overspeedNumber = "01"; 96 // }else if(overspeedLevel.equals("2") ){ 97 // overspeedDescription = "20%以上50%以下"; 98 // overspeedNumber = "02"; 99 // }else if(overspeedLevel.equals("3") ){ 100 // overspeedDescription = "50%以上100%以下"; 101 // overspeedNumber = "03,05"; // 也可能是 “50%以上” 102 // }else { 103 // overspeedDescription = "100%以上"; 104 // overspeedNumber = "04,05"; // 也可能是 “50%以上” 105 // }; 106 107 bean.setOverspeedNumber(overspeedNumber); 108 // bean.setOverspeedDescription( overspeedDescription ); 109 110 111 String violationNumber = "" ; 112 113 // bean.setOverspeedLevel( overspeedLevel ); 114 bean.setVehicleType( vehicleType ); 115 116 // 访问数据库 117 violationNumberList = overspeedDao.selectList( bean ); 118 119 120 System.out.println( vehicleType ); 121 System.out.println( overspeedNumber ); 122 // 循环列表 123 for( Bean b : violationNumberList ) { 124 violationNumber = b.getViolationNumber(); 125 System.out.println( violationNumber ); 126 } 127 128 response.setContentType("text/html"); 129 PrintWriter out = response.getWriter(); 130 131 JSONObject json = new JSONObject(); 132 133 json.accumulate("success", true); 134 json.accumulate("Method", "POST"); 135 json.accumulate("vehicleType", vehicleType ); 136 json.accumulate("overspeedNumber", overspeedNumber ); 137 json.accumulate("violationNumber", violationNumber ); 138 out.println( json.toString() ); 139 140 out.flush(); 141 out.close(); 142 143 } 144 145 /** 146 * Initialization of the servlet. <br> 147 * 148 * @throws ServletException if an error occurs 149 */ 150 public void init() throws ServletException { 151 // Put your code here 152 } 153 154 }
OverspeedDao.java
1 package overspeed; 2 3 import java.util.List; 4 5 import org.apache.ibatis.session.SqlSession; 6 7 import system.utils.MyBatisUtils; 8 9 public class OverspeedDao { 10 11 12 13 /* 14 * 读取 1 条记录 15 */ 16 @SuppressWarnings("unchecked") 17 public List<Bean> selectList(Bean bean){ 18 19 // 开 Session 20 SqlSession session = MyBatisUtils.openSession(); 21 List<Bean> list = null ; 22 23 try { 24 // 读取 1 条记录 25 list = session.selectList( "overspeedMapper.selectList", bean ); 26 } catch ( Exception e) { 27 e.printStackTrace(); 28 }finally { 29 // 关闭 session 30 session.close(); 31 } 32 33 return list ; 34 35 } 36 37 38 }
overspeedMapper.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 6 <mapper namespace="overspeedMapper"> 7 8 <!-- + selectList + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --> 9 <select id="selectList" parameterType="overspeed.Bean" 10 resultType="overspeed.Bean" useCache="false"> 11 12 <![CDATA[ 13 14 SELECT 15 T1.WFXWDM as violationNumber 16 FROM BOCOITMS.ITMS_DICT_CLLX t 17 LEFT JOIN BOCOITMS.ITMS_VES_WFCSMAPPING t1 ON t.CLDL = T1.CLDL 18 WHERE t.CLLX = #{vehicleType} 19 AND T1.CSBLDM in ( ${overspeedNumber} ) 20 AND ROWNUM = 1 21 ]]> 22 23 </select> 24 25 26 27 </mapper>