Basic tutorial for getting started with Java mybatis framework

1、 Introduction to mybatis

Mybatis is a first-class persistence framework that supports custom SQL, stored procedures and advanced mapping. Mybatis almost eliminates all JDBC code, and there is basically no need to manually set parameters and obtain search results. Mybatis can be configured using simple XML format or annotations, and can map basic data elements, map interfaces and POJOs (ordinary Java objects) to records in the database.

2、 Mybatis workflow

(1) Load configuration and initialize

Trigger condition: load configuration file

The configuration comes from two places: one is the configuration file and the other is the annotation of Java code. The SQL configuration information is loaded into mappedstatement objects (including incoming parameter mapping configuration, executed SQL statement and result mapping configuration) and stored in memory.

(2) Receive call request

Trigger condition: call the API provided by mybatis

Incoming parameter: the ID of the SQL and the incoming parameter object

Processing: pass the request to the lower request processing layer for processing.

(3) Trigger condition for processing operation request: the API interface layer passes the request

Incoming parameter: the ID of the SQL and the incoming parameter object

Process:

(A) Find the corresponding mappedstatement object according to the ID of SQL. (B) Parse the mappedstatement object according to the incoming parameter object to get the SQL to be executed and execute the incoming parameters. (C) Get the database connection, pass in the parameters to the database for execution according to the final SQL statement and execution, and get the execution result. (D) According to the result mapping configuration in the mappedstatement object, the obtained execution result is transformed and the final processing result is obtained. (E) Release connection resources.

(4) Return processing result returns the final processing result

Basic idea of ORM tool

Whether hibernate or mybatis are used, you can tell that they have one thing in common:

Functional architecture

The functional architecture of mybatis is divided into three layers:

1. API interface layer: interface APIs provided for external use. Developers use these local APIs to manipulate the database. As soon as the interface layer receives the call request, it will call the data processing layer to complete the specific data processing.

2. Data processing layer: responsible for specific SQL search, SQL parsing, SQL execution and execution result mapping. Its main purpose is to complete a database operation according to the call request.

3. Basic support layer: it is responsible for the most basic functional support, including connection management, transaction management, configuration loading and cache processing. These are common things, and they are extracted as the most basic components. Provide the most basic support for the upper data processing layer.

Multiple driver packages to be added:

Here's a quick start:

The contents are as follows:

Entity class user

Mapping file usermapping xml

Resource file mybatis xml

Test class:

result:

The above is a basic tutorial on the introduction of Java mybatis framework. I hope it will be helpful to your study.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>