第七篇:SqlSessionFactory、SqlSession、Executor

本文详细解读了SqlSessionFactory的创建方式、SqlSession的核心操作,包括其线程安全与不安全特性,以及Executor的设计与实现。重点介绍了如何获取Executor并理解其在数据库交互中的作用。

这一篇开始编写sql执行的过程,首先要先说下这两个组件,也可以说是执行的源头开始

SqlSessionFactory

   看名字就可以确定这是用于获取 SqlSession 对象的工厂类,主要是说一下他的格式和创建方式,这是线程安全的,一般一个应用就只有一个factory对象     

格式 

public interface SqlSessionFactory {

  /**
   * 打开一个SqlSession接口
   * @return
   */
  SqlSession openSession();

  /**
   * 其他的都是重载方法
   * @param autoCommit
   * @return
   */
  SqlSession openSession(boolean autoCommit);
  SqlSession openSession(Connection connection);
  SqlSession openSession(TransactionIsolationLevel level);

  SqlSession openSession(ExecutorType execType);
  SqlSession openSession(ExecutorType execType, boolean autoCommit);
  SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level);
  SqlSession openSession(ExecutorType execType, Connection connection);

  /**
   * 获取配置类信息
   * @return
   */
  Configuration getConfiguration();

}

获取

public static SqlSessionFactory sqlSessionFactory(Properties properties) {
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new SqlSessionFactoryBuilder().build(inputStream, properties);
}


通过SqlSessionFactoryBuilder对象进行构建即可



如果是直接自己new  DefaultSqlSessionFactory对象的话,xml里面的信息不会被加载,等于配置信息都要通过代码进行设置,所以一般推荐使用SqlSessionFactoryBuilder进行创建

SqlSession

SqlSession对象,一个核心接口,包含了所有跟数据库的交互,比如select、update、delete与事务相关的操作,commit、rollback、获取数据库连接等,这个对象是线程不安全的,因为每次在跟数据库的时候都需要获取连接,而每个SqlSession对象获取连接的时候都是同一条,如果SqlSession对象在多线程环境运行的过程中,自然就会有一个连接被不同事务获取到进行执行的情况,那么也就会失去了事务的保证行了

定义 

public interface SqlSession extends Closeable {

  /**
   * Retrieve a single row mapped from the statement key
   * @param <T> the returned object type
   * @param statement
   * @return Mapped object
   */
  <T> T selectOne(String statement);

  /**
   * Retrieve a single row mapped from the statement key and parameter.
   * @param <T> the returned object type
   * @param statement Unique identifier matching the statement to use.
   * @param parameter A parameter object to pass to the statement.
   * @return Mapped object
   */
  <T> T selectOne(String statement, Object parameter);

  /**
   * Retrieve a list of mapped objects from the statement key and parameter.
   * @param <E> the returned list element type
   * @param statement Unique identifier matching the statement to use.
   * @return List of mapped object
   */
  <E> List<E> selectList(String statement);

  /**
   * Retrieve a list of mapped objects from the statement key and parameter.
   * @param <E> the returned list element type
   * @param statement Unique identifier matching the statement to use.
   * @param parameter A parameter object to pass to the statement.
   * @return List of mapped object
   */
  <E> List<E> selectList(Str
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值