Mybatis的SqlSessionFactoryBuilder构造器(一)

SqlSessionFactoryBuilder是一个构造SqlSessionFactory的构造器,使用它能建立会话工厂。它读取了类似SqlMapConfig.xml的配置文件,反序列化为流对象,然后在创建一个XmlConfigBuilder对象,该对象会解析流内容,返回一个Configuration对象。SqlSessionFactoryBuilder调用build(Configuration configuration)生成并返回SqlSessionFactory。

SqlSessionFactory是会话工厂,使用它建立SqlSession对象。

SqlSession是一个Sql会话,建立SqlSession后会建立一个通向数据库的连接,进而访问数据库的数据,或者修改删除数据表等操作。

之后被XmlConfigBuilder解析内容,配置文件的内容会写入Configuration对象。Configuration是非常重要的全局对象,保存了mybatis所有的配置属性。

复制代码
 4     public void init() throws IOException{
 5         //创建sqlSessionFactory
 6         //MyBatis配置文件
 7         String resource = "SqlMapConfig.xml";
 8         //得到配置文件流
 9         InputStream inputStream = Resources.getResourceAsStream(resource);
10         //创建会话工厂,传入MyBatis的配置信息
11         sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
12     }
复制代码

 

复制代码
  public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
    try {
      XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
      return build(parser.parse());
    } catch (Exception e) {
      throw ExceptionFactory.wrapException("Error building SqlSession.", e);
    } finally {
      ErrorContext.instance().reset();
      try {
        inputStream.close();
      } catch (IOException e) {
        // Intentionally ignore. Prefer previous error.
      }
    }
  }

复制代码
  public XMLConfigBuilder(Reader reader, String environment, Properties props) {
    this(new XPathParser(reader, true, props, new XMLMapperEntityResolver()), environment, props);
  }

创建XmlConfigBuilder对象使用了XpathParser对象生成java的Document对象。

  private XMLConfigBuilder(XPathParser parser, String environment, Properties props) {
    super(new Configuration());
    ErrorContext.instance().resource("SQL Mapper Configuration");
    this.configuration.setVariables(props);
    this.parsed = false;
    this.environment = environment;
    this.parser = parser;
  }

在XmlConfigBuilder的构造函数中传参parser,new一个Configuration对象。

  public Configuration parse() {
    if (parsed) {
      throw new BuilderException("Each XMLConfigBuilder can only be used once.");
    }
    parsed = true;
    parseConfiguration(parser.evalNode("/configuration"));
    return configuration;
  }

之后parse方法执行了转换document数据,生成Configuration的基础属性,并返回最终初始化结束的Configuration对象。

 public SqlSessionFactory build(Configuration config) {
    return new DefaultSqlSessionFactory(config);
  }

最后在build中使用new的创建方式创建DefaultSqlSessionFacotory,且作为返回值返回。

复制代码
public class MyBatis_mapper_test {
    private SqlSessionFactory sqlSessionFactory;
    @Before
    public void init() throws IOException{
        //创建sqlSessionFactory
        //MyBatis配置文件
        String resource = "SqlMapConfig.xml";
        //得到配置文件流
        InputStream inputStream = Resources.getResourceAsStream(resource);
        //创建会话工厂,传入MyBatis的配置信息
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }
    @Test
    public void testFindUserById() throws Exception{
        //获取sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //创建UserMapper对象,MyBatis自动生成mapper代理
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //调用userMapper的方法
        User user = userMapper.findUserById(10);
        //关闭资源
        sqlSession.close();
        //打印客户信息
        System.out.println(user);
    }
}
复制代码

开发者使用mybatis的SqlSessionFactoryBuilder作为访问mysql数据库的启动。

如果想看springboot整合mybatis如何使用,请移步另一篇文章。

 

posted @   IT知识生产小店铺  阅读(208)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示