• mybatis SqlSessionFactory工厂模式


    一、工厂方法模式实现sqlsession

    (从别人博客复制的图)

     1.Sqlsession接口

    在sqlsession接口中包含了所有可能执行的sql语句。而Defaultsqlsession是他的实现类,实现了其中的方法。

     1 public interface SqlSession extends Closeable {
     2 
     3   /**
     4    * Retrieve a single row mapped from the statement key
     5    * @param <T> the returned object type
     6    * @param statement
     7    * @return Mapped object
     8    */
     9   <T> T selectOne(String statement);
    10 /**
    11    * Retrieve a list of mapped objects from the statement key and parameter.
    12    * @param <E> the returned list element type
    13    * @param statement Unique identifier matching the statement to use.
    14    * @return List of mapped object
    15    */
    16   <E> List<E> selectList(String statement);

    2.DefaultSqlSession

     1 public class DefaultSqlSession implements SqlSession {
     2 
     3   private Configuration configuration;
     4   private Executor executor;
     5 
     6   private boolean dirty;
     7 
     8  public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
     9     try {
    10       MappedStatement ms = configuration.getMappedStatement(statement);
    11       List<E> result = executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);
    12       return result;
    13     } catch (Exception e) {
    14       throw ExceptionFactory.wrapException("Error querying database.  Cause: " + e, e);
    15     } finally {
    16       ErrorContext.instance().reset();
    17     }
    18   }

    3.SqlSessionFactory接口

    sqlsessionfactory类中有opsession方法,用来创建sqlsession。

    4、DefaultSqlSessionFactory类实现了SqlSessionFactory

  • 相关阅读:
    牛牛与LCM(最小公约数)
    小明的挖矿之旅_牛客网(思维题)
    2019/4/20周赛 F题 CodeForces
    哥德巴赫猜想_2019/7/11_牛客网
    智障操作-wireless AC 9462
    codeforces762A
    2019中山大学程序设计竞赛(重现赛)斐波那契/
    Java自学笔记(10):【面向对象 】 类和对象、构造方法、this关键字
    Java自学笔记(9):方法
    Java自学笔记(8):多维数组
  • 原文地址:https://www.cnblogs.com/neu-student/p/7514573.html
Copyright © 2020-2023  润新知