1. 程式人生 > 實用技巧 >初識 Mybatis 執行流程

初識 Mybatis 執行流程

各位大佬,小弟這是自我學習總結,如有錯誤,請指教。

Mybatis 執行流程

1.配置Mybatis核心配置檔案mybatis-config.xml,也可以是別的命名,只不過官方文件建議使用mybatis-config.xml,配置內容我就略過了,因為我想總結的是執行流程,而不是使用配置流程。

2.在java程式碼中最終到獲取SqlSession,但是在官方文件上SqlSession是由SqlSessionFactory建立獲取的,SqlSessionFactory是由SqlSessionFactoryBuilder建立獲取的,接下來要具體的說一下SqlSessionFactoryBuilder具體幹了什麼,SqlSessionFactory又幹了什麼,最終獲取到SqlSession

 private static SqlSessionFactory sqlSessionFactory;
    static {
        try {
            //1 .獲取SQLSessionFactory物件
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            //SqlSessionFactoryBuilder 解析配置檔案流
            sqlSessionFactory = new
SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } }

3.從上面程式碼一步一步的取認識它的底層,首先是獲取的mybatis-config.xml的路徑

4.使用Resources獲取載入mybatis-config.xml全域性配置檔案流,InputStream,輸出流嘛~~

5.new SqlSessionFactoryBuilder,例項化SqlSessionFactoryBuilder構造器

6.new SqlSessionFactoryBuilder().build(InputStream);看看build做了什麼

build使用XMLConfigBuilder來解析mybatis-config.xml,從方法parseConfiguration可以看到有11個和mybatis-config.xml的屬性一樣,從而得知他就是解析mybatis-config.xml還有就是Debug可以發現mybatis-config.xml最後解析成了Configuration這個類

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

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

    private void parseConfiguration(XNode root) {
        try {
            this.propertiesElement(root.evalNode("properties"));
            Properties settings = this.settingsAsProperties(root.evalNode("settings"));
            this.loadCustomVfs(settings);
            this.loadCustomLogImpl(settings);
            this.typeAliasesElement(root.evalNode("typeAliases"));
            this.pluginElement(root.evalNode("plugins"));
            this.objectFactoryElement(root.evalNode("objectFactory"));
            this.objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
            this.reflectorFactoryElement(root.evalNode("reflectorFactory"));
            this.settingsElement(settings);
            this.environmentsElement(root.evalNode("environments"));
            this.databaseIdProviderElement(root.evalNode("databaseIdProvider"));
            this.typeHandlerElement(root.evalNode("typeHandlers"));
            this.mapperElement(root.evalNode("mappers"));
        } catch (Exception var3) {
            throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + var3, var3);
        }
    }
View Code

解析成了Configuration,他把所有mybatis-config.xml配置的一些屬性都解析成了Configuration這個實體物件

7.SqlSessionFactorysqlSessionFactory =new SqlSessionFactoryBuilder().build(InputStream);例項化sqlSessionFactory,但是例項化後的sqlSessionFactory 肯定保包含Configuration

8.在建立 快取 transactional事物管理器,目前學到的知識這我還是比較難理解的~日後在補充吧

9.在建立executor核心執行器,先有的快取 事物

10.建立SqlSession

11.實現增刪改查

12.if(執行error){回滾事物},else {檢視是否執行成功}

13.if(執行不成){回滾事物},else {提交事物}

14.關閉sqlSession.close();

下面我轉發一個流程圖,上面我描述的不是很好,有的知識點還是不能夠深入的理解,日後在在這片文章上在做補充吧