MyBatis快速入门——第六章、MyBatis分页demo测试
目录
1、映射mapper.xml
2、映射接口ProductMapper
3、ProductDAO.java数据层
4、GetInfoServlet.java的servlet文件
5、JSP页面
6、访问路径
7、访问效果
1、映射mapper.xml
代码语言:javascript复制<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC ".//mybaits.org/DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.item.mapper.ProductMapper">
<select id="GetInfo" resultType="Product">
select * from product limit #{pageNow},#{pageSize}
</select>
<delete id="DeleteById" parameterType="java.lang.Integer">
delete from product where id="${id}"
</delete>
</mapper>
2、映射接口ProductMapper
代码语言:javascript复制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("pageNow") int pageNow,@Param("pageSize")int pageSize);
/**
* 删除
* @param id
* @return
*/
int DeleteById(@Param("id") String id);
}
3、ProductDAO.java数据层
代码语言:javascript复制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(int pageNow,int pageSize){
SqlSessionFactory factory = JDBC.GetConn();
SqlSession session = factory.openSession();
ProductMapper db = session.getMapper(ProductMapper.class);
List<Product> list = db.GetInfo(pageNow,pageSize);
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;
}
}
4、GetInfoServlet.java的servlet文件
代码语言:javascript复制package com.item.servlet;
import com.item.dao.ProductDAO;
import com.item.model.Product;
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 {
String pageNow = request.getParameter("pageNow");
String pageSize = request.getParameter("pageSize");
int int_pageNow = Integer.parseInt(pageNow.isEmpty() ? "0" : pageNow);
int int_pageSize = Integer.parseInt(pageSize.isEmpty() ? "3" : pageSize);
System.out.println(int_pageNow "~" int_pageSize);
int_pageNow=(int_pageNow-1)*int_pageSize;
System.out.println(int_pageNow);
List<Product> list = ProductDAO.GetInfo(int_pageNow,int_pageSize);
//发送至前台
request.setAttribute("lists",list);
request.getRequestDispatcher("GetInfo.jsp").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
5、JSP页面
代码语言:javascript复制<%@ page import="java.util.List" %>
<%@ page import="com.item.model.Product" %><%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2022/5/16 0016
Time: 15:55
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>bootStrap——table</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<% List<Product> list = (List<Product>) request.getAttribute("lists"); %>
<div style="width: 90%;text-align: center;padding:50px;font-size: 3rem">
[<a href="/GetInfo?pageNow=1&pageSize=5">1,5</a>]
[<a href="/GetInfo?pageNow=1&pageSize=3">1,3</a>]
[<a href="/GetInfo?pageNow=1&pageSize=5">1,5</a>]
[<a href="/GetInfo?pageNow=2&pageSize=5">2,5</a>]
[<a href="/GetInfo?pageNow=2&pageSize=3">2,3</a>]
[<a href="/GetInfo?pageNow=3&pageSize=2">3,2</a>]
</div>
<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 : list) {
%>
<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>
<hr/>
</body>
</html>
6、访问路径
http://localhost:8088/GetInfo?pageNow=1&pageSize=5
7、访问效果
删除一个信息