Mybatis framework in Java


Introduction to mybatis

(1) Mybatis was originally an open source project ibatis of Apache. In 2010, the project was migrated from Apache Software Foundation to Google Code and renamed mybatis. Moved to GitHub in November 2013.
(2) Mybatis is an excellent persistence layer framework. It encapsulates the process of JDBC operating the database, so that developers only need to pay attention to SQL itself, and do not need to spend energy to deal with complicated JDBC process codes such as registering drivers, creating connections, creating statements, manually setting parameters, result set retrieval and so on.
(3) Mybatis configures various statements (statement and preparedstatemnt) to be executed through XML or annotation, and generates the final executed SQL statement through the mapping between Java object and SQL in the statement. Finally, mybatis framework executes SQL, maps the result into Java object and returns it.
In short, mybatis encapsulates the process of JDBC accessing the database, simplifies the JDBC code, and solves the trouble of JDBC encapsulating the result set into Java objects.

Architecture diagram of mybatis

Mybatis framework in Java
(1) Mybatis-config.xml is the core configuration file of mybatis. Sqlsessionfactory, that is, sqlsession factory, can be generated through its configuration.
(2) Sqlsession objects can be generated based on sqlsessionfactory.
(3) Sqlsession is an object that can send SQL to execute and return results. It is similar to the connection object in JDBC. It is also a crucial object in mybatis.
(4) Executor is the underlying object of sqlsession, which is used to execute SQL statements.
(5) Mapperstatement object is also the underlying object of sqlsession. It is used to receive input mappings (parameters in SQL statements) and make output mappings (that is, map the results of SQL queries to corresponding results).

Why use mybatis

Because there are many disadvantages in using traditional JDBC to access the database, such as cumbersome code, too many duplicate codes, too many objects to operate, releasing resources, too troublesome processing of results, etc. Almost all these problems have been solved in the mybatis framework!!

Basic workflow of mybatis

(1) Read the configuration file, which contains database connection information, mapper mapping file or mapper package path
(2) With this information, you can create sqlsessionfactory. The life cycle of sqlsessionfactory is program level, which is established when the program runs and dies when the program ends.
(3) Sqlsessionfactory creates a sqlsession to execute SQL statements. Sqlsession is a procedure level. It is created in a method and should be closed at the end of the method.
(4) When the user uses the method configured in the mapper.xml file, mybatis first parses the SQL dynamic tag into the form of the corresponding database SQL statement, encapsulates it into the mapperstatement object, and then injects the SQL into the database through the executor for execution and returns the results.
(5) Wrap the returned results into Java objects through mapping.

Programming steps for mybatis

(1) Create sqlsessionfactory;
(2) Create a sqlsession object through sqlsessionfactory;
(3) Execute database operations through sqlsession;
(4) Commit the transaction through session. Commit();
(5) Close the transaction using session. Close().


Mybatis framework is easy to start and master. SQL is written directly in XML, which is convenient for unified management and optimization, reduces the coupling between SQL and program code, and also provides XML tags to support dynamic SQL writing.

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]