再识mybatis
创建工程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
https://pan.baidu.com/s/1cOpvwG8HTuaQcZgIF77iTA