再識mybatis(servlet)

創建工程zpnhtml

創建html:

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

  <!-- - 按照第二個字段模糊查詢 -->

  <div>   

  按照第二個字段模糊查詢  <input id="ytb" name="ytb">  <input type="button" value="find" onclick="getYt()">

    </div>

  <!-- - 顯示錶的數據 -->

  <div>  

    <table>   

     <thead>

     <tr> <td> FLDA</td> <td> FLDB</td>  

     <td> FLDC</td>   <td> FLDD</td>   <td> OP</td>    </tr>

     </thead>     

     <tbody id="dis">     

     </tbody>    

    </table>  

   </div>

</body>

</html>

 

創建action

package com.zpnhtml.action;

 

import java.io.IOException;

import java.io.PrintWriter;

 

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

@WebServlet(urlPatterns = "/ytController")

public class YtController extends HttpServlet {

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

doPost(req, resp);

}

 

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

String method = req.getParameter("method");

if (method == null) {

method = "";

}

switch (method) {

case "insert":

break;

case "delete":

break;

case "update":

break;

case "login":

break;

default:

getYt(req, resp);

break;

}

}

private void getYt(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

String ytb = req.getParameter("ytb");

if (ytb == null) {

ytb = "";

}

PrintWriter out=resp.getWriter();

out.print("select * from yt where  ytb  like  '%"+ytb+"%'");

}

 

 

}

啓動服務器

繼續實現查詢

每個人將昨天mybatis相關配置加入工程

Jar包

Src結構

mybatis-config.xml

數據庫訪問

package com.zpnhtml.dao;

 

import org.apache.ibatis.session.SqlSession;

 

public class ZpnDao {

 

private SqlSession getConn() {

SqlSession conn=null;

try {

conn= MyBatisDAOUtil.getSqlSessionFactory().openSession();

} catch (Exception e) {

System.err.println(e.getMessage());

}

return conn;

}

 

}

寫接口(昨天用"com.mybatis1.mapper.YtMapper.insert"

編輯YtMapper.xml

從action調用數據庫

編輯Yt.java

啓動服務器,查看結果

看到結果繼續。。。對接html和action

在html引用jquery庫

在首頁異步請求ytController,看是否對接成功

指定返回結果是json對象

解析JSON對象

實現模糊查詢

調整getData()

運行效果

https://pan.baidu.com/s/1cOpvwG8HTuaQcZgIF77iTA

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章