Get to know mybatis and its basic configuration and Implementation

1. Introduction to mybatis

Mybatis is an excellent persistence layer framework, which 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, and map interfaces and Java POJOs (plain old Java objects) to records in the database.

2. Main components and functions of mybatis

From the perspective of mybatis code implementation, the main core components of mybatis are as follows:

The whole flow chart is roughly as follows:

3. Simple steps to build mybatis

1. Mybatis configuration

  myBatis-config. XML, as the global configuration file of mybatis, this file configures the running environment and other information of mybatis, including data source, transaction management and database environment configuration.

Simple examples are as follows:

  mapper. The XML file is the SQL mapping file, in which the SQL statements for operating the database are configured. This file requires sqlmapconfig Load in XML.

A simple example is as follows:

Mapper interface:

Mapper. XML file

2. Construct sqlsessionfactory, i.e. session factory, through configuration information such as mybatis environment

3. Sqlsession is created by the session factory, that is, session. Database operation needs to be carried out through sqlsession.

4. The bottom layer of mybatis customizes the operation database of the executor interface. The executor interface has two implementations, one is the basic executor and the other is the cache executor.

5. The mapped statement is also a low-level encapsulation object of mybatis, which encapsulates the configuration information and SQL mapping information of mybatis. mapper. An SQL in the XML file corresponds to a mapped statement object, and the ID of the SQL is the ID of the mapped statement.

6. The mapped statement defines the input parameters of SQL execution, including HashMap, basic type and POJO. The executor uses the mapped statement

Statement maps the input Java objects into SQL before executing SQL. Input parameter mapping is to set parameters for Preparedstatement in JDBC programming.

7. The mapped statement defines the output results of SQL execution, including HashMap, basic type and POJO. The executor maps the output results to Java objects after executing SQL through the mapped statement. The output result mapping process is equivalent to the result parsing process in JDBC programming.

Let's take a look at the basic configuration and implementation of mybatis

1. Prepare mybatis jar package: mybatis-3.2 8.jar

Prepare the database jar package (I use MySQL): mysql-connector-java-3.1 12-bin. jar

2. Prepare the main configuration file of mybatis: mybatis config xml

The project architecture is shown in the figure, in which JDBC Propertis is the database connection configuration file

summary

The above is all the contents of Xiaobian's preliminary understanding of mybatis. I hope it will help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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
分享
二维码
< <上一篇
下一篇>>