Explain the use example of Java based on mybatis
Mybatis, formerly known as ibatis, was originally an open source project of Apache. In 2010, the project was migrated from Apache Software Foundation to Google Code and renamed mybatis. Mybatis is an excellent persistence layer framework that supports common SQL queries, stored procedures and advanced mapping. Mybatis eliminates almost all the manual setting of JDBC code and parameters and the retrieval of result sets. Mybatis uses simple XML or annotations for configuration and original mapping, and maps the interface and Java POJOs (plan old Java objects) into records in the database.
The functional architecture of mybatis is divided into three layers (pictures borrowed from Baidu Encyclopedia):
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.
Here is a simple mybatis using demo. Overall structure
POM dependency
You need to reference two jar packages, one is mybatis and the other is MySQL connector Java. If it is a maven project, you can add dependencies in POM as follows.
Data preparation
Create point data in MySQL for testing:
configuration file
Two types of configuration files are required. One is mybatis config XML. In the example, it is a very simple configuration. There are many instructions for the detailed configuration on the Internet.
The other is the data provider mapping file: in the example, usermapper xml。 This file is usually found under Src / main / resource or under the subdirectory mybatis, which is located in mybatis config XML is specified by the resource of the mappers / mapper node.
The corresponding mapping file is test The usermapper interface under the mybatis namespace only defines the interface for accessing the data table:
You need a POJO: user java
test
A class called sqlsession is used to operate the database through mybatis. This class is generated through sqlsessionfactory. It is generally recommended to maintain a sqlsessionfactory globally.
TestMyBatis. java
The test code is as follows:
TestMyBatis. java
matters needing attention
1. Mybatis will log with log4j, but turning on debug mode seems to have a great impact on performance.
2. The query cache of mybatis has a great impact on performance, and there is a big gap between enabling and not enabling
Note: the line cache must be added to the mapper file, otherwise it will not take effect.
Example code download: code download