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】
<%@ 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>
查询效果如下: