SqlSessionFactory工具类

本文阅读 1 分钟
首页 代码,Java 正文

img

🍁博客主页:👉不会压弯的小飞侠
✨欢迎关注:👉点赞👍收藏⭐留言✒
✨系列专栏:MyBatis
✨如果觉得博主的文章还不错的话,请三连支持一下博主。
🔥欢迎大佬指正,一起 学习!一起加油!
在这里插入图片描述

img

⭐⭐⭐结合Javaweb用户登录注册功能实现学习: Javaweb用户登录注册功能

SqlSessionFactory 工厂只创建一次,不需要重复创建。 代码优化:

//2.1获取SqlSessionFactory对象
String resouce = "mybatis.config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

解决了低码的重复。

🔥SqlSessionFactoryUtils

创建在com.jkj.utils包下:

package com.jkj.utils;

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.InputStream;

public class SqlSessionFactoryUtils { 
    private static SqlSessionFactory sqlSessionFactory;
    static{ 
        String resource = "mybatis.config.xml";
        InputStream inputStream = null;
        try { 
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) { 
            e.printStackTrace();
        }
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    public static SqlSessionFactory getSqlSessionFactory(){ 
        return sqlSessionFactory;
    }

}

🔥LoginServlet.java

package com.jkj.web;

import com.jkj.mapper.UserMapper;
import com.jkj.pojo.User;
import com.jkj.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

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.io.InputStream;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet { 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
        //获取用户名和密码
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        /*//2.调用MyBatis完成查询 //2.1获取SqlSessionFactory对象 String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); */
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
        //2.2获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //2.3获取Mapper
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        //2.4调用方法
        User user = userMapper.select(username, password);
        //2.5释放资源
        sqlSession.close();
        //获取字符输出流,并设置content type
        resp.setContentType("text/html;charset=utf-8");

        //判断user是否为空
        if (user !=null){ 
            resp.getWriter().write("登录成功!");
        }
        else { 
            resp.getWriter().write("登录失败!");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
       this.doGet(req, resp);
    }
}
本文为互联网自动采集或经作者授权后发布,本文观点不代表立场,若侵权下架请联系我们删帖处理!文章出自:https://blog.csdn.net/qq_43514330/article/details/125315258
-- 展开阅读全文 --
安全面试之XSS(跨站脚本攻击)
« 上一篇 07-24

发表评论

成为第一个评论的人

热门文章

标签TAG

最近回复