Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Basic concepts and principles of MyBaits

Shulou Source: shulou.com Published: 2022-06-03 02:59:37 09月25日 Update

What is MyBatis?

MyBatis is an excellent persistence layer framework that supports customized SQL, stored procedures, and advanced mapping. MyBatis avoids almost all JDBC code and manually setting parameters and getting result sets. MyBatis can use simple XML or annotations to configure and map native information, mapping interfaces and Java's POJOs (Plain Old Java Objects, ordinary Java objects) to records in the database.

Structure flow corresponding to MyBatis source code

SqlMapConfig.xml, as the global configuration file of mybatis, configures the running environment of mybatis and other information. The mapper.xml file, the sql mapping file, is configured with sql statements to manipulate the database, and this file needs to be loaded in SqlMapConfig.xml.

Mapper.xml

Select * from user_info where id = # {id}

two。 SqlSessionFactory (that is, session factory) is constructed from configuration information such as the mybatis environment.

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

3. The sqlSession is created by the session factory, and the operation of the database needs to be done through sqlSession.

SqlSession session = sqlSessionFactory.openSession ()

The bottom layer of 4.mybatis defines the Executor executor interface to operate the database. The Executor interface has two implementations, one is the basic executor and the other is the cache executor.

5.MappedStatement is also an underlying encapsulation object of mybatis, which wraps mybatis configuration information and sql mapping information. A sql in the mapper.xml file corresponds to a MappedStatement object, and the id of sql is the id of MappedStatement.

6.MappedStatement defines the input parameters of sql execution, including HashMap, basic types, and pojo,Executor maps the input java object to sql before performing sql through MappedStatement. The input parameter mapping is to set parameters for preparedStatement in JDBC programming.

7.MappedStatement defines the output result of sql execution, including HashMap, basic type, and pojo,Executor maps the output result to java object after performing sql through MappedStatement. The mapping process of output result is equivalent to the parsing process of the result in JDBC programming.

Tags: File configuration information object result parameter data database interface procedure actuator input output factory underlying environment type programming excellent ordinary Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Apple MariaDB Redmi Shulou Information