SqlSessionFactory工具类

简介: SqlSessionFactory工具类

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

🍁SqlSessionFactory工具类

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);
    }
}
相关文章
|
5月前
|
Java 数据库连接 mybatis
使用Mybatis获取sqlSession对象老爆红的问题解决
使用Mybatis获取sqlSession对象老爆红的问题解决
|
6月前
|
SQL 缓存 Java
MyBatis原理分析之获取SqlSessionFactory
MyBatis原理分析之获取SqlSessionFactory
212 0
|
SQL 缓存 Java
MyBatis核心 - SqlSession如何通过Mapper接口生成Mapper对象
从 SqlSessionFactoryBuilder - SqlSessionFactory - SqlSession - Mapeper实例对象 的过程
162 0
|
6月前
|
缓存 Java 数据库连接
一文彻底搞懂Mybatis系列(十)之SqlSession、SqlSessionFactory和SqlSessionFactoryBuilder详解
一文彻底搞懂Mybatis系列(十)之SqlSession、SqlSessionFactory和SqlSessionFactoryBuilder详解
1040 1
|
Java
超实用的mybatis-plus工具类
超实用的mybatis-plus工具类
140 0
超实用的mybatis-plus工具类
|
6月前
|
设计模式 缓存 Java
MyBatis原理分析之获取SqlSession
MyBatis原理分析之获取SqlSession
213 0
|
XML JavaScript Java
MyBatis SqlSessionFactory的两种创建方式
MyBatis SqlSessionFactory的两种创建方式
109 0
|
XML SQL 设计模式
|
SQL Java 数据库连接
Mybatis Mapper Bean 生成源码分析二
Mybatis Mapper Bean 生成源码分析二
106 0
|
Java 数据库连接 mybatis
Mybatis Mapper Bean 生成源码分析一
Mybatis Mapper Bean 生成源码分析一
92 0