MyBatis-01 (Framework Overview)

Framework overview

Personal blog : www.xiaobeigua.icu


1.1 Commonly used structure of software development

1.1.1  Three-tier architecture

The three-tier architecture contains three layers:

Interface layer ( User Interface layer ) , business logic layer ( Business Logic Layer ) , data access layer ( Data access layer )

Three-tier responsibilities

  1. Interface layer (presentation layer, view layer): The main function is to accept user data and display the processing result of the request. Use web   pages to interact with users, mobile apps are also the presentation layer, users operate in the app , and business logic is processed on the server side.
  2. Business logic layer : receiving the data passed over, checking the data, calculating the business logic, and calling the data access layer to get the data.
  3. Data access layer : dealing with the database. Mainly realize the addition, deletion, modification, and checking of data. Submit the data stored in the database to the business layer, and save the data processed by the business layer to the database .

Three-tier interaction of processing requests:

User---> Interface layer ---> Business logic layer ---> Data access layer ---> DB database

As shown in the figure:

Why use three layers?

1. Clear structure, low coupling, clear division of labor at each level

2. High maintainability and high scalability

3. Conducive to standardization

4. Developers can only pay attention to the function realization of a certain layer in the whole structure

5. It is conducive to the reuse of logic at all levels

1.1.2 Common Framework

Common J2EE development framework:

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 the complicated processes of loading drivers, creating connections, creating statements, closing connections, and resources.

MyBatis configures various sql statements to be executed through xml or annotation, and generates the final executed sql statement through the mapping of java objects and sql dynamic parameters, and finally the mybatis framework executes sql and maps the results to java objects and return.

Spring framework:

The Spring framework was created to solve the complexity of software development. Spring uses basic JavaBeans to complete the previously very complex enterprise-level development. Spring solves the coupling between business objects and functional modules. It is not only used in javase and web, but most Java applications can benefit from Spring.

Spring is a lightweight inversion of control (IoC) and aspect-oriented (AOP) container.

SpringMVC framework

Spring MVC is a module added by SpringFrameWork 3.0, which provides the Spring Framework with the ability to build Web applications. Now the SpringMVC module provided by the Spring framework can be used to realize web application development, and Spring and Spring MVC frameworks can be used seamlessly in web projects.


1.2 What is a framework

​​​​​​​​​​​​​​1.2.1   Framework Definition

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

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

The framework is safe, reusable, and constantly upgraded software.

1.2.2 Problems solved by the framework

The most important problem to be solved by the framework is technology integration. In the J2EE framework, there are various technologies, different applications and systems use different technologies to solve problems. Need to choose different technologies from J2EE, and the complexity of the technology itself leads to greater risks. When an enterprise develops a software project, the main purpose is to solve business problems. Not only requires companies to be responsible for the technology itself, but also for solving business problems. This is something most companies cannot accomplish. The framework integrates related technologies, and enterprise development can be concentrated on business areas.

Another aspect can provide development efficiency.


1.3 JDBC programming

​​​​​​​1.3.1   Review of Programming with JDBC

public void findStudent() {        Connection conn = null; Statement stmt = null; ResultSet rs = null; try { //注册 mysql 驱动         Class.forName("com.mysql.jdbc.Driver"); //连接数据的基本信息 url ,username,password String url = "jdbc:mysql://localhost:3306/springdb"; String username = "root"; String password = "123456"; //创建连接对象         conn = DriverManager.getConnection(url, username, password); //保存查询结果         List<Student> stuList = new ArrayList<>(); //创建 Statement, 用来执行 sql 语句         stmt = conn.createStatement(); //执行查询,创建记录集,         rs = stmt.executeQuery("select * from student"); while (rs.next()) { Student stu = new Student(); stu.setId(rs.getInt("id")); stu.setName(rs.getString("name")); stu.setAge(rs.getInt("age")); //从数据库取出数据转为 Student 对象,封装到 List 集合             stuList.add(stu); }  } catch (Exception e) { e.printStackTrace(); } finally { try { //关闭资源             if (rs != null) ; { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } catch (Exception e) { e.printStackTrace(); } } }

1.3.2 Defects 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 together

1. 4 Overview of MyBatis Framework

MyBatis framework:

MyBatis was originally an open source project iBatis of apache. In 2010, this project was migrated from apache software foundation to google code, and was renamed MyBatis. Migrated to Github in November 2013.

The term iBATIS comes from the combination of "internet" and "abatis", and is a Java-based persistence layer framework. The persistence layer framework provided by iBATIS includes SQL Maps and Data Access Objects (DAOs)

Currently, the latest version is MyBatis 3.5.1, and its release date is April 8, 2019.

​​​​​​​1.4.1    Main problems solved by MyBatis

Reduce the complexity of using JDBC, no need to write repeated creation of Connetion, Statement; no need to write close resource code. Use java objects directly to represent the result data. Let developers focus on SQL processing. Other distractions are done by MyBatis.

MyBatis can accomplish:

  1. Register the driver of the database , such as Class.forName("com.mysql.jdbc.Driver"))
  2. Create Connection, Statement and ResultSet objects that must be used in JDBC

3. Get sql from xml , execute sql statement, convert ResultSet result into 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()