总目录 (最下面 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" %>
首页
添加用户
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" %>
添加用户
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" %>
Title
操作成功!!!
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" %>
$Title$
添加用户 查询用户
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" %>
查询用户
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 pageInfo = usersService.findUsers(page,users);
req.setAttribute("pageInfo",pageInfo);
req.setAttribute("users",users);
req.getRequestDispatcher("showUsers.jsp").forward(req,resp);
}
}
3.2.4 修改业务层
PageInfo findUsers(int page,Users users);
/**
* 查询用户
*
*/
@Override
public PageInfo 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 list = usersMapper.selectByExample(usersExample);
PageInfo 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" %>
显示查询结果
用户 ID
用户姓名
用户性别
${users.userid}
${users.username}
${users.usersex}
上一页
${st.count}
${st.count}
下一页