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