It should have been a summary of MyBatis long ago (Autumn Recruitment of 2021)

Hello everyone, my name is 方圆
MyBatis!


1. What is MyBatis?

MyBatis is 持久层框架, it supports SQL. Avoid almost all JDBC code and manual setting of parameters 简化了开发. MyBatis can be configured with XML files and annotations.

2. What is ORM?

Adult, then that is to say, object-relational mapping, it is 关系型数据库数据the 简单Java类(Pojo)technology of mapping relationship.

3. Problems in traditional JDBC development

  1. Frequent creation and release of database connection objects is required during use, which affects system performance. If a data connection pool is used, JDBC needs to implement the connection pool itself.
  2. When the SQL statement needs to be modified, the Java code needs to be modified, which is not easy to maintain.
  3. The use of preparedStatement to pass parameters to the occupied position symbol is hard-coded, because the where condition of the SQL statement is not necessarily, which is not easy to maintain
  4. The result set is troublesome to process, and it is more convenient to map to Java objects
  • Based on the above shortcomings of JDBC, let's think about the advantages of MyBatis
  • You can 配置文件中配置数据库连接池use the connection pool to manage database connections
  • SQL语句与Java代码是分离的
  • MyBatis automatically maps java objects to database data according to ORM

4. The operating principle of MyBatis

  1. Read Mybatis configuration file (mybatis-config.xml), configured operating environment information, such as database information
  2. Load the mapping file (mapper.xml), the SQL statement for operating the database is stored in the mapping file, pay attention to namespacethe correct writing
  3. Create SqlSessionFactory (singleton mode)
  4. Create SqlSession through SqlSessionFactory
  5. Through SqlSession, call getMapper方法, load the mapper interface, perform database operations, in the execution of the database operation, but also mention the Executor executor, it will dynamically generate the SQL statement to be executed according to the parameters passed by the SqlSession
  6. Call session.commit()commit transaction
  7. Call session.close()close dialog

Insert picture description here

5. Why need precompilation

Pre-compilation can optimize the execution of SQL, the database does not need to compile SQL statements, can be directly executed, and can prevent SQL injection. The underlying call is still the PreparedStatement of JDBC, using #{} to realize the placeholder, so that you can avoid wrong input leading to changes in the SQL statement logic, and ${} is a reference for preventing SQL injection Insert picture description here
: SQL injection details


Guess you like

Origin blog.csdn.net/qq_46225886/article/details/107379729