Seventy-six, MyBatis framework entry

MyBatis framework: MyBatis is an excellent java-based persistence layer framework, which encapsulates jdbc internally. Developers only need to pay attention to the SQL statement itself, and do not need to deal with loading drivers, creating connections, creating statements, and closing connections.

content

frame definition

The problem the framework solves

Disadvantages of using JDBC

Advantages of using MyBatis

MyBatis functional architecture

MyBatis framework process


frame definition

Framework (Framework) is the reusable design of the whole or part of the system, which is expressed as a set of abstract components and the method of interaction between component instances; another thinks that the framework is the application skeleton and template that can be customized by application developers.

Simply put, a framework is actually semi-finished software, a set of components for you to use to complete your own system. To frame a stage from another perspective, you perform on stage. Add the functions you want to complete on the basis of the framework.

Frameworks are secure, reusable, and continuously upgraded software.

The problem the framework solves

The most important problem to be solved by the framework is technology integration. In the J2EE framework, there are various technologies, different applications, and the system uses different technologies to solve problems. Different technologies need to be selected from J2EE, and the complexity of the technologies themselves leads to greater risks.

When an enterprise develops a software project, the main purpose is to solve a business problem. That requires enterprises to be responsible for the technology itself, but also to solve business problems. This is something that most businesses cannot accomplish. The framework integrates related technologies, and enterprise development can focus on business areas. Another aspect can provide development efficiency.

Disadvantages of using JDBC

  1. More code, low development efficiency
  2. Need to pay attention to Connection, Statement, ResultSet object creation and destruction
  3. The result of the ResultSet query needs to be encapsulated as a List by itself
  4. More repetitive code
  5. Business code and database operations are mixed

Advantages of using MyBatis

  1. Reduce the complexity of using JDBC
  2. No need to write repeated creation Connection, Statement 
  3. No need to write close resource code
  4. Use java objects directly to represent the result data
  5. Let developers focus on SQL processing. Other distractions are done by MyBatis

MyBatis functional architecture

We divide the functional architecture of Mybatis into three layers:

1. API interface layer: The interface API provided for external use, and developers manipulate the database through these local APIs. 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 processing, etc. Its main purpose is to complete a database operation according to the call request.

3. Basic support layer: Responsible for the most basic functional support, including connection management, transaction management, configuration loading, and cache processing. These are all common things, and they are extracted as the most basic components. Provide the most basic support for the upper data processing layer.

MyBatis framework process

MyBatis (formerly iBatis) is a persistence layer framework that supports common SQL queries, stored procedures and advanced mapping.

1. Register the database driver

Class.forName(“com.mysql.jdbc.Driver”)

2. Create Connection, Statement, ResultSet objects that must be used in JDBC

3. Get sql from xml, execute the sql statement, and convert the ResultSet result into a java object

List<Student> list = new ArrayLsit<>();
ResultSet rs = state.executeQuery(“select * from student”);
while(rs.next){
 Student student = new Student();
 student.setName(rs.getString(“name”));
 student.setAge(rs.getInt(“age”));
 list.add(student);
}

4. Close resources

ResultSet.close() , Statement.close() , Conenection.close()


 

Guess you like

Origin blog.csdn.net/m0_54925305/article/details/123290123