crud/main/java/com/utils/SqlSessionFactoryUtils.java

37 lines
1.0 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.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;
/*工具类:SqlSessionFactory数据库会话工厂工具*/
public class SqlSessionFactoryUtils {
//定义一个SqlSessionFactory的对象
public static SqlSessionFactory sqlSessionFactory = null;
//定义一个静态方法获得SqlSessionFactory
public static SqlSessionFactory getSqlSessionFactory() {
//步骤1读取mybatis 主配置文件。获取数据库相关的配置信息
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream("mybatis-config.xml");
//步骤2通过配置文件流input构建出工厂类
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
return sqlSessionFactory;
}
}