总目录 (最下面 Mybatis分页 扩展内容)
1. 如下图所式文件夹需要在Mybatis逆向工程中生成,有时间再说说Mybatis逆向工程
2. 什么是 Open Session In View
2.1 Open Session In View 模式:
Open Session In View 是将一个数据库会话对象绑定到当前请求线程中,在请求期间一直
保持数据库会话对象处于 Open 状态,使数据库会话对象在请求的整个期间都可以使用。直
到产生响应后关闭当前的数据库会话对象。
2.2 创建 OpenSqlSessionInViewFilter
package com.tong.web.filter; import com.tong.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; /*** * 在filter中打开一个SqlSession */ @WebFilter("/*") public class OpenSqlSessionInViewFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { SqlSession sqlSession = MybatisUtils.getSqlSession(); try { filterChain.doFilter(servletRequest, servletResponse); sqlSession.commit(); }catch (Exception e) { e.printStackTrace(); sqlSession.rollback(); }finally{ MybatisUtils.closeSqlSession(); } } @Override public void destroy() { } }
3. 完成业务操作
3.1添加用户
3.1.1 修改 index.jsp
<%-- Created by IntelliJ IDEA. User: 童蕴锐 Date: 2021/7/29 Time: 20:28 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>首页</title> </head> <body> <a href="addUsers.jsp">添加用户</a> </body> </html>
3.1.2 创建 addUsers.jsp
<%-- Created by IntelliJ IDEA. User: 童蕴锐 Date: 2021/8/1 Time: 19:50 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>添加用户</title> </head> <body> <form action="usersServlet.do" method="post"> <input type="hidden" name="flag" value="addUsers"/> 用户姓名: <input type="text" name="username"/><br/> 用户性别: <input type="text" name="userage"/><br> <input type="submit" value="OK"/> </form> </body> </html>
3.1.3 创建 UsersServlet
package com.tong.web.servlet; import com.tong.pojo.Users; import com.tong.service.UsersService; import com.tong.service.impl.UsersServiceImpl; 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("/usersServlet.do") public class UsersServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String flag = req.getParameter("flag"); if("addUsers".equals(flag)){ this.addUsers(req,resp); } } /** * 处理添加用户请求 */ private void addUsers(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ Users users = this.createUsers(req); UsersService usersService = new UsersServiceImpl(); usersService.addUsers(users); resp.sendRedirect("ok.jsp"); } /** * 获取提交数据 */ private Users createUsers(HttpServletRequest req){ String username = req.getParameter("username"); String userage = req.getParameter("userage"); Users users = new Users(); users.setUsername(username); users.setUserage(Integer.valueOf(userage)); return users; } }
3.1.4 创建业务层
package com.tong.service; import com.tong.pojo.Users; /** * @author 童蕴锐 */ public interface UsersService { void addUsers(Users users); }
package com.tong.service.impl; import com.tong.mapper.UsersMapper; import com.tong.pojo.Users; import com.tong.utils.MybatisUtils; import com.tong.service.UsersService; import org.apache.ibatis.session.SqlSession; public class UsersServiceImpl implements UsersService { /*** * 添加用户 * @param users */ @Override public void addUsers(Users users) { SqlSession sqlSession = MybatisUtils.getSqlSession(); UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class); usersMapper.insertSelective(users); } }
3.1.5 创建成功页面
<%-- Created by IntelliJ IDEA. User: 童蕴锐 Date: 2021/8/1 Time: 20:36 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> 操作成功!!! </body> </html>
3.2查询用户
3.2.1 修改 index.jsp
<%-- Created by IntelliJ IDEA. User: 童蕴锐 Date: 2021/8/1 Time: 20:40 --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <a href="addUsers.jsp">添加用户</a> <a href="findUsers.jsp">查询用户</a> </body> </html>
3.2.2 创建 findUsers.jsp
<%-- Created by IntelliJ IDEA. User: 童蕴锐 Date: 2021/8/1 Time: 20:45 --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>查询用户</title> </head> <body> <form action="usersServlet.do" method="post"> <input type="hidden" name="flag" value="findUsers"/> 用户姓名: <input type="text" name="username"/><br/> 用户性别:<input type="text" name="usersex"/><br/> <input type="submit" value="OK"/> </form> </body> </html>
3.2.3 修改 UsersServlet
@WebServlet("/usersServlet.do") public class UsersServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException { String flag = req.getParameter("flag"); if("addUsers".equals(flag)){ this.addUsers(req,resp); }else if("findUsers".equals(flag)){ this.findUsers(req,resp); } } /** * 处理添加用户请求 */ private void addUsers(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ Users users = this.createUsers(req); UsersService usersService = new UsersServiceImpl(); usersService.addUsers(users); resp.sendRedirect("ok.jsp"); } /** * 获取提交数据 */ private Users createUsers(HttpServletRequest req){ String username = req.getParameter("username"); String usersex = req.getParameter("usersex"); Users users = new Users(); users.setUsername(username); users.setUsersex(usersex); return users; } /** * 查询用户 */ private void findUsers(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ Users users = this.createUsers(req); String pageIndex = req.getParameter("pageIndex"); int page = 1; if(pageIndex != null && pageIndex.length() > 0){ page = Integer.parseInt(pageIndex); } UsersService usersService = new UsersServiceImpl(); PageInfo<Users> pageInfo = usersService.findUsers(page,users); req.setAttribute("pageInfo",pageInfo); req.setAttribute("users",users); req.getRequestDispatcher("showUsers.jsp").forward(req,resp); } }
3.2.4 修改业务层
PageInfo<Users> findUsers(int page,Users users);
/** * 查询用户 * */ @Override public PageInfo<Users> findUsers(int page, Users users) { SqlSession sqlSession = MybatisUtils.getSqlSession(); UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class); UsersExample usersExample = this.createUsersExample(users); PageHelper.startPage(page,2); List<Users> list = usersMapper.selectByExample(usersExample); PageInfo<Users> pageInfo = new PageInfo<>(list); return pageInfo; } /** * 生成查询条件 */ private UsersExample createUsersExample(Users users){ UsersExample usersExample = new UsersExample(); UsersExample.Criteria criteria = usersExample.createCriteria(); if(users.getUsername() != null && users.getUsername().length() >0){ criteria.andUsernameEqualTo(users.getUsername()); } if(users.getUsersex() != null && users.getUsersex().length() > 0){ criteria.andUsersexEqualTo(users.getUsersex()); } return usersExample; }
扩 展 分 页
3.3.2 添加 taglib 指令标签
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3.3.3 创显示查询结果页面
<%-- Created by IntelliJ IDEA. User: 童蕴锐 Date: 2021/8/2 Time: 20:45 --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>显示查询结果</title> <script> function subForm(pageIndex){ document.getElementById("pageIndex").value=pageIndex; document.forms[0].submit(); } </script> </head> <body> <form action="usersServlet.do" method="post"> <input type="hidden" name="flag" value="findUsers"/> <input type="hidden" name="pageIndex" id="pageIndex"/> <input type="hidden" name="username" value="${users.username}"/> <input type="hidden" name="usersex" value="${users.usersex}"/> </form> <table align="center" border="1" width="40%"> <tr> <th>用户 ID</th> <th>用户姓名</th> <th>用户性别</th> </tr> <c:forEach items="${pageInfo.list}" var="users"> <tr> <td>${users.userid}</td> <td>${users.username}</td> <td>${users.usersex}</td> </tr> </c:forEach> <tr> <td colspan="3" align="center"> <c:if test="${pageInfo.pageNum > 1}"> <a href="#" onclick="subForm(${pageInfo.pageNum - 1})">上一页</a> </c:if> <c:forEach begin="1" end="${pageInfo.pages}" varStatus="st"> <c:choose> <c:when test="${pageInfo.pageNum eq st.count}"> <a style="color: red" href="#" onclick="subForm(${st.count})">${st.count}</a> </c:when> <c:otherwise> <a href="#" onclick="subForm(${st.count})">${st.count}</a> </c:otherwise> </c:choose> </c:forEach> <c:if test="${pageInfo.pageNum < pageInfo.pages}"> <a href="#" onclick="subForm(${pageInfo.pageNum + 1})">下一页</a> </c:if> </td> </tr> </table> </body> </html>