1. What is MyBatis
MyBatis first open source project from the Apache Foundation ibatis,2010 This project has the Apache Software Foundation migrated to Google Code, and renamed MyBatis;
MyBatis is an excellent persistence layer framework that supports common query SQL queries, stored procedures, and advanced mapping queries
MyBatis encapsulates the manual setup of almost all JDBC code and parameters and the retrieval of the result set;
MyBatis uses simple XML or annotations to configure and define mappings, mapping Java's POJOs (Plain old Java Objects) to records in a database.
2.MyBatis Architecture
The MyBatis architecture consists mainly of the following key parts
1). Load configuration file
There are two forms of configuration, one is an XML configuration file and the other is a Java code annotation.
MyBatis loads SQL configuration information into a Mappedstatement object (including incoming parameter mapping configuration, executed SQL statement, result mapping configuration), and stores it in memory
2). SQL parsing
When the API interface layer receives a call request, it receives the ID of the incoming SQL and the incoming object (which can be a map, JavaBean, or base type).
MyBatis will find the corresponding mappendstatent based on the SQL ID, and then parse the mappedstatement based on the incoming parameter object.
Parse to get the final SQL statement and parameters to execute
3). SQL execution
The resulting SQL and parameters are taken to the database for execution, resulting in the operation of the database.
4). Result mapping
The results of the operational database are converted according to the mapped configuration, and can be converted to Hashmap,javabean or basic types, and the final result is returned.
3.MyBatis configuration file
The XML configuration file for the Mybastis framework contains the following two types
1). Sqlmapconfig.xml (1)
master configuration file for specifying database connection parameters and frame parameters
2). Sqlmap.xml (N)
mapping definition files for defining SQL statements and mapping information
4. Introduction to the Framework API
When using the MyBatis framework, the following APIs are primarily involved
Sqlsessionfactionbuilder
This object is responsible for building Sqlsessionfactory instances based on the MyBatis configuration file Sqlmapconfig.xml
Sqlsessionfactory
Each MyBatis application is centered on a Sqlsessionfactory object. The object is responsible for creating an Sqlsession object instance
Sqlsession
This object contains all the methods for executing SQL operations that are used to execute a mapped SQL statement
5. Get the Sqlsession object
String conf = "Sqlmapconfig.xml";
Reader reader = resources.getresourceasreader (conf);
Create a Sessionfactory object
Sqlsessionfactorybulider SFB = new Sqlsessionfactorybulider ()
Sqlsessionfactory SF = sfb.build (reader);
Create session
sqlsession session = Sf.opensession ();
6. Using Mybastis to realize paged query
When using the Sqlsession selectlist () method, specify a rowbounds pager parameter to query the specified range of records
Rowbounds (Offset,limit) constructor
Rowbounds bounds = new Rowbounds (offset,limit);
OFFSET specifies the starting line of the fetch record, starting at 0
Limit specifies the number of fetch records
SelectList () How to use
Sqlsession.selectlist (SQL ID, parameter, rowbounds object)
Tips:
MyBatis paging is memory-based paging, with the principle that all records are queried, and then the JDBC-based absolute () and next () methods are used to locate partial records.
Therefore, in the case of large amounts of data, it is not recommended to use the MyBatis self-paging feature.
Requires the developer to specify the SQL statement for paged queries or to extend the use of mybatis.
7. Using the Mapper Mapper
The Mapper mapper is the interface for developers to create binding mapping statements, and an instance of the Mapper interface can be obtained from the sqlsession.
sqlsession session = Sqlsessionfactory.opensession ();
try{
Deptmapper mapper= Session.getmapper (deptmapper.class);
Do work
}finally{
Session.close ();
}
Tips:
The method name in the Mapper interface is consistent with the ID of SQL in Sqlmap.xml.
8.ResultMap Mapping definition
When Sqlmap.xml definition <select> operation, if the query result field name and Java Pojo attribute are inconsistent, you need to explicitly specify the mapping relationship using the <resultMap> element.
For example:
<select id= "FindAll1" resultmap= "Deptmap" >
Select Deptno,dname,loc from DEPT
</select>
<resultmap id= "Deptmap" type= "com.test.entity.Dept" >
<result property= "No" column= "DEPTNO"/>
<result property= "name" column= "Dname"/>
<result property= "loc" column= "loc"/>
</resultMap>
Introduction to the MyBatis framework