再识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" οnclick="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

 

 

 启动服务器,查看结果

 

 

 

 看到结果继续。。。对接htmlaction

 

html引用jquery

 

 

 

 

 

 在首页异步请求ytController,看是否对接成功

 

 

 

 

 

 指定返回结果是json对象

 

 

 

 

 

 解析JSON对象

 

 

 

 

 

 

 实现模糊查询

 

 调整getData()

 

 

 

 

 

 

 运行效果

 

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

 

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

 

转载于:https://www.cnblogs.com/caststudy/p/11436120.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值