小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

mybatis源碼分析(1)

 jjls14 2014-07-17

  在使用mybatis框架時(shí),第一步就需要產(chǎn)生SqlSessionFactory類的實(shí)例(相當(dāng)于是產(chǎn)生連接池),通過調(diào)用SqlSessionFactoryBuilder類的實(shí)例的build方法來完成。下面具體對(duì)這一過程涉及的源碼進(jìn)行分析。

     首先用Eclipse工具查看SqlSessionFactoryBuilder類的Outline視圖:

 

從上圖中可以看出,SqlSessionFactoryBuilder類負(fù)責(zé)構(gòu)建SqlSessionFactory,并且提供了多個(gè)build的重載方法。但其實(shí)很多都是在調(diào)同一簽名的方法,例如:

 public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) ,只是由于方法參數(shù)environment和 propertiese都可以為null,

所以為了提供調(diào)用的便利性,才提供了下面的三個(gè)方法:

  

public SqlSessionFactory build(InputStream inputStream)
public SqlSessionFactory build(InputStream inputStream, String environment) 
public SqlSessionFactory build(InputStream inputStream, Properties properties)

按照上述思路去除重復(fù)的,真正的重載方法只有如下三種:

public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) 
public SqlSessionFactory build(Reader reader, String environment, Properties properties)
public SqlSessionFactory build(Configuration config)

可以看出,配置信息可以以三種形式提供給SqlSessionFactorybuild方法,分別是InputStream(字節(jié)流)、Reader(字符流)、Configuration(類),由于字節(jié)流與字符流都屬于讀取配置文件的方式,所以從配置信息的來源就很容易想到構(gòu)建一個(gè)SqlSessionFactory有兩種方式,大致代碼如下:

 

(1) 讀取xml文件構(gòu)造方式

 

String resource = "org/mybatis/example/mybatis-config.xml";
  InputStream inputStream = Resources.getResourceAsStream(resource);
  SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream) ;

 

(2) 編程構(gòu)造方式

DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(BlogMapper.class);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration) ;

 

 

 

下面先來分析XML文件構(gòu)造方式的build方法的源碼:

 

復(fù)制代碼
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.
      }
    }
  }
復(fù)制代碼

 

通過上面這幾行代碼,就能看出基于XML文件的這種構(gòu)造方式,通過從XML中讀取信息的工作之后,也是構(gòu)造出Configuration對(duì)象之后再繼續(xù)進(jìn)行SqlSessionFactory的構(gòu)建工作的,只是多了些XML的解析工作,所以我們只需單刀直入,直按分析編程構(gòu)造方式的代碼就可以了,或者是直接分析 build(parser.parse())這句代碼(參數(shù)產(chǎn)生過程先跳過)

編程構(gòu)造方式的build方法源碼如下(基于xml的構(gòu)造方式的build(parser.parse())最終也是調(diào)了這個(gè)代碼): 

  

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

 

其實(shí)這么看來SqlSessionFactory在mybatis的默認(rèn)實(shí)現(xiàn)類為org.apache.ibatis.session.defaults.DefaultSqlSessionFactory , 其構(gòu)造過程主要是注入了Configuration的實(shí)例對(duì)象,Configuration的實(shí)例對(duì)象即可通過解析xml配置文件產(chǎn)生,也可能通過代碼直接構(gòu)造。以上代碼使用了一個(gè)設(shè)計(jì)模式:建設(shè)者模式(Builder)SqlSessionFactoryBuilder扮演具體的建造者,Configuration類則負(fù)責(zé)建造的細(xì)節(jié)工作,SqlSession則是建造出來的產(chǎn)品。

以下是類圖和建造者模式的基本形態(tài)圖,讀者自行對(duì)照閱讀。

    

 

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多