(A) mybatsi processes, principles

1.mybatis development process

1.1 add MyBatis dependent jar

 

 

1.2 to develop a mapping entity class

 

 

1.3 Develop a SQL mapping file

Created under src / main / resource table corresponding to the current mapping file for SQL statements SQL statements

 

 

1.4 Development MyBatis core profile

   Creating MyBatis-config.xml in src / main / resources as the core profile

1.5 Development MyBatis basic call flow

 

2.mybatis works

to sum up:

1. mybatis workflow

  1. Save the frame configuration file to the configuration objects
  2. The configuration as an attribute to our current DefaultSqlSessionFactory instance of the object (DefaultSqlSessionFactory is SqlSessionFactory implementation class), we wanted to this database configuration information is loaded into sqlSessionFactory, create a sqlSessionFactory factory.
  3. After creating an object will generate sqlSession good sqlSessionFactory factory.

4. Provides properties in objects created SqlSession

        1) Configuration objects

        2) dirty: true sql statement after the implementation of the transaction can be submitted

                  false sql statement is executed to send an error transaction rollback

        3) Executor object performs:

                  Create a Statement object in the creation process of assigning content to rely on MapperStatement objects with placeholders to bind sql

         5. SqlSession.commit (): The decision to commit and rollback case dirty properties

       6.      SqlSession.close(); 

2.      Mybatis如何将sql语句id与sql语句进行定位

在这个MapperAnnotationBuilder中会判断是否有定义命名空间,如果没有或者定义出错就会报出错误

private void loadXmlResource() {
    if (!this.configuration.isResourceLoaded("namespace:" + this.type.getName())) {
        String xmlResource = this.type.getName().replace('.', '/') + ".xml";
        InputStream inputStream = null;

        try {
            inputStream = Resources.getResourceAsStream(this.type.getClassLoader(), xmlResource);
        } catch (IOException var4) {
            ;
        }

        if (inputStream != null) {
            XMLMapperBuilder xmlParser = new XMLMapperBuilder(inputStream, this.assistant.getConfiguration(), xmlResource, this.configuration.getSqlFragments(), this.type.getName());
            xmlParser.parse();
        }
    }

}

 

 

3.      mybatis如何将数据绑定到sql命令

public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {// parameter是传递进来的对象参数
    Statement stmt = null;//JDBC的Statement对象
    int var6;
    try {
        Configuration configuration = ms.getConfiguration();
        StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, (ResultHandler)null, (BoundSql)null);
        stmt = this.prepareStatement(handler, ms.getStatementLog());
        //将对象通过JDBC的prepareStatement实现类封装成完整的SQL语句

var6 = handler.update(stmt);
    } finally {
        this.closeStatement(stmt);
    }

    return var6;
}

 

4.      mybatis如何输送sql

在这个XMLConfigBuilder中读取配置的标签的内容

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.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);
    }
}

 

 

5.  SqlSession中的dirty属性赋值的目的

 

this.dirty = true;//赋值的目的:sqlSession的一个属性
MappedStatement ms = this.configuration.getMappedStatement(statement);//找到ID对应的sql语句

ErrorContext.instance().resource(ms.getResource()).activity("executing an update").object(ms.getId());//找到对应的mapper文件并且传递个ID匹配对应的sql语句

今天就先简单介绍下Mybatis的开发流程和与原理的个人总结,下一篇介绍的是对mybatis的配置文件标签的使用。

有问题邮箱:[email protected]

 

Guess you like

Origin www.cnblogs.com/fqh0324/p/11037601.html