首页 > 数据库 >008.从 XML 中构建 SqlSessionFactory

008.从 XML 中构建 SqlSessionFactory

时间:2022-11-01 02:12:03浏览次数:43  
标签:XML xml SqlSessionFactory SqlSession sqlSession org import 008

1.在pom.xml文件中引入依赖

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

2.创建MybatisTest.java测试SessionFactory是否加载成功

package com.imooc.mybatis;

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 org.junit.Test;

import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;

public class MybatisTest
{
    @Test
    public void testSqlSessionFactory() throws IOException
    {
        //利用Reader加载classpath下的mybatis-config.xml核心配置文件
        Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
        //初始化SqlSessionFactory对象,同时解析mybatis-config.xml文件
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        System.out.println("SessionFactory加载成功");
        SqlSession sqlSession = null;
        try
        {
            //创建SqlSession对象,SqlSession是JDBC的扩展类,用于与数据库交互
            sqlSession = sqlSessionFactory.openSession();
            //创建数据库连接(测试用)
            Connection connection = sqlSession.getConnection();
            System.out.println(connection);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (sqlSession != null)
            {
                //如果type="POOLED",代表使用连接池,close则是将连接回收到连接池中
                //如果type="UNPOOLED",代表直连,close则会调用Connection.close()方法关闭连接
                sqlSession.close();
            }
        }
    }
}

 

标签:XML,xml,SqlSessionFactory,SqlSession,sqlSession,org,import,008
From: https://www.cnblogs.com/LLL0617/p/16846467.html

相关文章

  • day08-(xml&&tomcat)
    回顾:jdbc:java语言操作数据库jdbc是一套规范,oracle公司制定的驱动:jdbc的实现类,由数据库厂商提供使用步骤:1.导入jar包(驱动)2.注册驱......
  • 【题解】P4683 [IOI2008] Type Printer
    题目传送门:P4683[IOI2008]TypePrinter板子题贪心+字典树+dfs贪心:把最长的字符留在最后打或者最先打把每个字母插入字典树对trie树dfs一遍#include<cstdio>#inclu......
  • FOR XML PATH 函数用法
    https://www.cnblogs.com/yasuo2/p/6433697.html   一.FORXMLPATH简单介绍             那么还是首先来介绍一下FORXMLPATH,假设现在有一张兴趣爱......
  • Java知识【XML知识】
    1.xml1.1概述【理解】万维网联盟(W3C)万维网联盟(W3C)创建于1994年,又称W3C理事会。1994年10月在麻省理工学院计算机科学实验室成立。建立者:TimBerners-Lee(蒂姆·伯纳斯......
  • 0083-Go-range 遍历
    环境Time2022-08-23Go1.19前言说明参考:https://gobyexample.com/range目标使用Go语言的range遍历。切片求和packagemainimport"fmt"funcmain(){......
  • 0084-Go-函数
    环境Time2022-08-23Go1.19前言说明参考:https://gobyexample.com/functions目标使用Go语言的函数。定义函数funcplus(aint,bint)int{returna+......
  • 0085-Go-多返回值函数
    环境Time2022-08-23Go1.19前言说明参考:https://gobyexample.com/multiple-return-values目标使用Go语言的函数,返回两个值。直接返回packagemainimport"......
  • 0086-Go-可变参数函数
    环境Time2022-08-23Go1.19前言说明参考:https://gobyexample.com/variadic-functions目标使用Go语言的可变参数函数。可变参数函数packagemainimport"fm......
  • 0087-Go-闭包
    环境Time2022-08-23Go1.19前言说明参考:https://gobyexample.com/closures目标使用Go语言的闭包。示例packagemainimport"fmt"funcintSeq()func()i......
  • 0088-Go-递归
    环境Time2022-08-23Go1.19前言说明参考:https://gobyexample.com/closures目标使用Go语言的递归。递归函数packagemainimport"fmt"funcfact(nint)i......