Java Mybatis framework getting started
1. Introduction to Mybatis
MyBatis is a first-class persistence framework that supports custom SQL, stored procedures, and advanced ing. MyBatis eliminates almost all JDBC code, and does not need to manually set parameters or obtain retrieval results. MyBatis can be configured with simple XML format or annotations, and can Map basic data elements, Map interfaces, and POJOs common java objects) to records in the database.
2. MyBatis Workflow
(1) load configuration and initialize
Trigger condition: load the configuration file
Configuration comes from two sources: configuration files and Java code annotations, load SQL configuration information into MappedStatement objects, including input parameter ing configuration, executed SQL statements, and result ing configuration, and store them in the memory.
(2) receive call requests
Trigger condition: Call the API provided by Mybatis
Input parameter: the ID of the SQL statement and the input parameter object.
Process: send the request to the lower-layer Request Processing Layer for processing.
(3) processing operation request trigger condition: the API interface layer transmits the request
Input parameter: the ID of the SQL statement and the input parameter object.
Processing Process:
(A) Find the corresponding MappedStatement object based on the SQL ID.
(B) parse the MappedStatement object based on the input parameter object to obtain the final SQL statement to be executed and execute the input parameter.
(C) obtain the database connection, input parameters to the database for execution based on the obtained final SQL statement and execution, and obtain the execution result.
(D) convert the execution result based on the result ing configuration in the MappedStatement object and obtain the final processing result.
(E) release the connection resource.
(4) return the processing result and return the final processing result.
Basic Idea of orm tools
Either hibernate or mybatis, you can have one thing in common:
-
Obtain sessionfactory from the configuration file (usually in the XML configuration file.
-
Sessions generated by sessionfactory
-
Add, delete, modify, and query data and commit transactions in the session.
-
Close the session after it is used up.
-
There is a mapping configuration file between the java object and the database, which is usually an xml file.
Functional Architecture
The functional architecture of Mybatis is divided into three layers:
Multiple driver packages to be added:
Below is a quick start:
The directory is as follows:
Entity User
Package com. oumyye. model;
Public class User {
Private String id;
Private String name;
Private int age;
Public String getId (){
Return id;
}
Public void setId (String id ){
This. id = id;
}
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public int getAge (){
Return age;
}
Public void setAge (int age ){
This. age = age;
}
@ Override
Public String toString (){
Return "User [id =" + id + ", name =" + name + ", age =" + age + "]";
}
}
Usering file UserMapping. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE mapper PUBLIC "-// mybatis.org//DTD Mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd ">
<Mapper namespace = "com. oumyye. mapping. UserMapping">
<! -- Compile the query SQL statement in the select tag and set the id attribute of the select tag to getUser. The id attribute value must be unique and cannot be repeated.
Use the parameterType attribute to specify the parameter type used for query, and the resultType attribute to specify the type of the returned result set.
ResultType = "com. oumyye. model. User" indicates that the query result is encapsulated into a User Class Object and returned
The User class is the entity class corresponding to the users table.
-->
<! --
Obtain a user object by id query.
-->
<Select id = "getUser" parameterType = "String"
ResultType = "com. oumyye. model. User">
Select * from user where id =#{ id}
</Select>
</Mapper>
Resource file mybatis. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE configuration PUBLIC "-// mybatis.org//DTD Config 3.0 //" http://mybatis.org/dtd/mybatis-3-config.dtd ">
<Configuration>
<Environments default = "development">
<Environment id = "development">
<TransactionManager type = "JDBC"/>
<! -- Configure database connection information -->
<DataSource type = "POOLED">
<Property name = "driver" value = "com. mysql. jdbc. Driver"/>
<Property name = "url" value = "jdbc: mysql: // localhost: 3306/mybatis"/>
<Property name = "username" value = "root"/>
<Property name = "password" value = "root"/>
</DataSource>
</Environment>
</Environments>
<Mappers>
<Mapper resource = "com/oumyye/mapping/userMapping. xml"/>
</Mappers>
</Configuration>
Test class:
Package test;
Import java. io. InputStream;
Import org. apache. ibatis. session. SqlSession;
Import org. apache. ibatis. session. SqlSessionFactory;
Import org. apache. ibatis. session. SqlSessionFactoryBuilder;
Import org. junit. Test;
Import com. oumyye. model. User;
Public class Tests {
@ Test
Public void test (){
String resource = "mybatis. xml ";
// Use the class loader to load the configuration file of mybatis. It also loads the associated ing file)
InputStream is = Tests. class. getClassLoader (). getResourceAsStream (resource );
// Construct the sqlSession Factory
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder (). build (is );
SqlSession session = sessionFactory. openSession ();
/**
* Ing the SQL identifier string,
* Com. oumyye. mapping. UserMapping is the value of the namespace attribute of the ER er tag in the userMapper. xml file,
* GetUser is the id attribute value of the select tag. You can use the id attribute value of the select tag to find the SQL statement to be executed.
*/
String statement = "com. oumyye. mapping. UserMapping. getUser"; // The ID String mapped to SQL
// Execute the query and return the SQL statement of a unique user object
User user = session. selectOne (statement, "1123 ");
System. out. println (user. toString ());
}
}
Result: