MyBatis快速入门——第五章、maven整合Mybatis&Servlet_tomcat(3)

简介: MyBatis快速入门——第五章、maven整合Mybatis&Servlet_tomcat

8、创建【com.item.mapper.ProductMapper.java】文件

这里用的是【interface】来修饰类


package com.item.mapper;
import com.item.model.Product;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface ProductMapper {
    /**
     * 查询所有的接口
     * @return
     */
    List<Product> GetInfo();
    /**
     * 删除
     * @param id
     * @return
     */
    int DeleteById(@Param("id") String id);
}

9、创建【com.item.common】下的【JDBC】常用工具类

编码如下:


package com.item.common;
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.Reader;
public class JDBC {
    public static SqlSessionFactory GetConn() {
        Reader reader = null;
        try {
            reader = Resources.getResourceAsReader("mybatis-config.xml");
            return new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}


10、创建【com.item.dao】下的【ProductDAO.java】文件

package com.item.dao;
import com.item.common.JDBC;
import com.item.mapper.ProductMapper;
import com.item.model.Product;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;
public class ProductDAO {
    /**
     * 获取所有查询信息
     * @return
     */
    public static List<Product> GetInfo(){
        SqlSessionFactory factory = JDBC.GetConn();
        SqlSession session = factory.openSession();
        ProductMapper db = session.getMapper(ProductMapper.class);
        List<Product> list = db.GetInfo();
        session.close();
        return list;
    }
    /**
     * 删除
     * @param id
     * @return
     */
    public static boolean DeleteById(String id){
        SqlSessionFactory factory = JDBC.GetConn();
        SqlSession session = factory.openSession();
        ProductMapper db = session.getMapper(ProductMapper.class);
        int rows = db.DeleteById(id);
        session.commit();
        session.close();
        return rows>0;
    }
}


11、完成【servlet】访问层的【GetInfoServlet.java】接口文件

package com.item.servlet;
import com.item.dao.ProductDAO;
import com.item.model.Product;
import sun.misc.CharacterEncoder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/GetInfo")
public class GetInfoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Product> list = ProductDAO.GetInfo();
        //发送至前台
        request.setAttribute("lists",list);
        request.getRequestDispatcher("GetInfo.jsp").forward(request,response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

可以添加【DeleteByIdServlet】接口

package com.item.servlet;
import com.item.dao.ProductDAO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/DeleteById")
public class DeleteByIdServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        ProductDAO.DeleteById(id);
        response.sendRedirect("GetInfo");
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

12、编辑视图文件【GetInfo.jsp】


image.png

<%@ page import="java.util.List" %>
<%@ page import="com.item.model.Product" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022/5/15 0015
  Time: 10:22
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>mybatis与servlet整合</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<% List<Product> lists = (List<Product>) request.getAttribute("lists");%>
<table class="table table-bordered table-hover">
    <tr class="info">
        <th>编号</th>
        <th>创建时间</th>
        <th>修改时间</th>
        <th>产品名称</th>
        <th>产品标题</th>
        <th>产品价格</th>
        <th>产品数量</th>
        <th>产品厂家</th>
        <th>产品颜色</th>
        <th>产品重量</th>
        <th>产品状态</th>
        <th>操作</th>
    </tr>
    <% for (Product p : lists) {
    %>
    <tr>
        <td><%=p.getId()%>
        </td>
        <td><%=p.getCreateDate()%>
        </td>
        <td><%=p.getModifyDate()%>
        </td>
        <td><%=p.getProductName()%>
        </td>
        <td><%=p.getProductTitle()%>
        </td>
        <td><%=p.getProductPrice()%>
        </td>
        <td><%=p.getProductCount()%>
        </td>
        <td><%=p.getProductType()%>
        </td>
        <td><%=p.getProductColor()%>
        </td>
        <td><%=p.getProductWeight()%>
        </td>
        <td><%=p.getProductStatus()%>
        </td>
        <td>
            <a href="/DeleteById?id=<%=p.getId()%>" onclick="return confirm('是否删除此行?')" class="btn btn-primary">删除</a>
        </td>
    </tr>
    <%
        }%>
</table>
</body>
</html>


查询效果如下:

image.png

相关文章
|
3月前
|
安全 Java 应用服务中间件
阿里技术官架构使用总结:Spring+MyBatis源码+Tomcat架构解析等
分享Java技术文以及学习经验也有一段时间了,实际上作为程序员,我们都清楚学习的重要性,毕竟时代在发展,互联网之下,稍有一些落后可能就会被淘汰掉,因此我们需要不断去审视自己,通过学习来让自己得到相应的提升。
|
2月前
|
前端开发 Java 数据库连接
Springboot-MyBatis配置-配置端口号与服务路径(idea社区版2023.1.4+apache-maven-3.9.3-bin)
Springboot-MyBatis配置-配置端口号与服务路径(idea社区版2023.1.4+apache-maven-3.9.3-bin)
33 0
|
3月前
|
Java 项目管理 数据库
基于springboot电子招投标系统(分前后台管理springboot+mybatis+maven+html+css)
基于springboot电子招投标系统(分前后台管理springboot+mybatis+maven+html+css)
127 0
|
3月前
|
Java 数据库 数据安全/隐私保护
基于SSM框架实现管科类考研自我管理系统(分前后台spring+springmvc+mybatis+maven+jsp+jquery)
基于SSM框架实现管科类考研自我管理系统(分前后台spring+springmvc+mybatis+maven+jsp+jquery)
|
3月前
|
Java 关系型数据库 MySQL
基于springboot心理树洞管理系统(分前后台springboot+mybatis+mysql+maven+vue+echarts)
基于springboot心理树洞管理系统(分前后台springboot+mybatis+mysql+maven+vue+echarts)
|
3月前
|
SQL Java 数据库连接
Mybatis之Mybatis简介、搭建Mybatis相关步骤(开发环境、maven、核心配置文件、mapper接口、映射文件、junit测试、log4j日志)
【1月更文挑战第2天】 MyBatis最初是Apache的一个开源项目iBatis, 2010年6月这个项目由Apache Software Foundation迁移到了Google Code。随着开发团队转投Google Code旗下,iBatis3.x正式更名为MyBatis。代码于2013年11月迁移到Github iBatis一词来源于“internet”和“abatis”的组合,是一个基于Java的持久层框架。iBatis提供的持久层框架包括SQL Maps和Data Access Objects(DAO)
203 3
Mybatis之Mybatis简介、搭建Mybatis相关步骤(开发环境、maven、核心配置文件、mapper接口、映射文件、junit测试、log4j日志)
|
3月前
|
Java BI 数据库
基于SSM框架实现面向小微企业的简历管理系统企业简历管理系统(分前后台spring+springmvc+mybatis+maven+jsp+css+echarts)
基于SSM框架实现面向小微企业的简历管理系统企业简历管理系统(分前后台spring+springmvc+mybatis+maven+jsp+css+echarts)
|
3月前
|
前端开发 Java 关系型数据库
基于ssm框架旅游网旅游社交平台前后台管理系统(spring+springmvc+mybatis+maven+tomcat+html)
基于ssm框架旅游网旅游社交平台前后台管理系统(spring+springmvc+mybatis+maven+tomcat+html)
|
2月前
|
SQL Java 数据库连接
【MyBatis-Plus】快速精通Mybatis-plus框架—快速入门
【MyBatis-Plus】快速精通Mybatis-plus框架—快速入门
52 0
|
3月前
|
Java 数据库连接 数据库
基于SpringBoot+maven+Mybatis+html慢性病报销系统(源码+数据库)
基于SpringBoot+maven+Mybatis+html慢性病报销系统(源码+数据库)

推荐镜像

更多