The process of executing SQL in MyBatis can be divided into the following steps:
-
Parse the configuration file: At runtime, MyBatis will load and parse the configuration file (usually
mybatis-config.xml
) to obtain database connection information, mapping files, etc. -
Create SqlSessionFactory: MyBatis uses SqlSessionFactory to create SqlSession objects. SqlSessionFactory is a thread-safe object that should be created once when the application starts and reused throughout the application life cycle.
-
Create SqlSession: The application creates the SqlSession object through SqlSessionFactory. SqlSession is a lightweight, non-thread-safe object used to execute SQL statements and manage transactions.
-
Obtain the Mapper interface: The application obtains the implementation class of the Mapper interface through SqlSession. The Mapper interface defines the mapping relationship between SQL statements and Java methods. MyBatis will generate and execute SQL statements based on these mapping relationships.
-
Execute SQL statements: When the application calls the method of the Mapper interface, MyBatis will generate the corresponding SQL statement based on the configuration information in the mapping file, and pass the parameters to the database for execution. MyBatis supports a variety of SQL statements, such as query, insert, update, delete, etc.
-
Process the result set: When the database executes the SQL statement and returns the results, MyBatis will map the query results to Java objects. According to the configuration rules in the mapping file, MyBatis will map the columns of the database query results to the properties of the Java object, thereby facilitating the application's processing and use of data.
-
Submit the transaction: If the application turns on transaction management, MyBatis will submit the transaction after executing the SQL statement. If an exception occurs or the transaction is rolled back, MyBatis will undo the previous operation and close the database connection.
-
Close SqlSession: After the application completes the operation on the database, you need to manually close the SqlSession object. This frees up resources and avoids problems such as memory leaks.
@Test
public void testInsert() throws IOException {
// Get the input stream of the core configuration file
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
// ObtainSqlSessionFactoryBuilderobject
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// ObtainSqlSessionFactoryBuilderobject
SqlSessionFactory build = sqlSessionFactoryBuilder.build(is);
// Obtainsqlsession objectsqlSession,yesMybatisProvides objects for operating the database
SqlSession sqlSession = build.openSession(true);
// ObtainUserMapperproxy implementation class object
// The bottom layer will be created for meUserMapperThe implementation class,then return
// TODO Use proxy mode,createdUserMapperThe implementation class
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// TODO Execution principle,according toIdFind,Then find the object to which it is mapped,and then implement sqlSession.insert()
// int result = sqlSession.insert("com.mybatis.mapper.UserMapper.insertUserOne");
// User user = new User(null, "abc", "123456", 18);
int result = mapper.insertUserOne();
System.out.println(mapper instanceof UserMapper);
System.out.println("result = " + result);
// commit transaction
// If the transaction is not committed,Then it will show no effect,The transaction has not been submitted yet
// sqlSession.commit();
// closuresqlSession
sqlSession.close();
}