Analysis of mybatis service implementation in spring boot

Time:2020-9-29

First of all, we know that what mybatis does is to encapsulate and optimize JDBC operations, which aims to make it easier for developers to operate databases
In mybaits framework, there are two ways to map SQL statements: one is directly written in the < mapper > element in the XML file, and the other is through annotation,

1、 Mapping through XML:

Analysis of mybatis service implementation in spring boot
Analysis of mybatis service implementation in spring boot
Analysis of mybatis service implementation in spring boot
Note that the ID value in the XML file should be consistent with the method name~~~~

2、 By way of annotation:

First of all, let’s take note of the above code:
@Mapper: using the annotation on the interface, the interface modified by it will create an implementation class by mybaits, which will be managed by spring.
Through this annotation, we can not implement the method ourselves, but only need to declare the abstraction of the method in the interface. To implement SQL, we only need to add the annotation of the SQL to be executed on the abstract method.
Analysis of mybatis service implementation in spring boot
In this way, our SQL statement is associated with our method. Finally, the implementation class object of this interface is added to the test class by means of dependency injectionAnalysis of mybatis service implementation in spring boot
In the figure above, the goodsdao variable is injected with a goodsdao type object, which is completely produced by the framework itself.

Business realization

Either way to complete SQL business is based on mybatis
The main role of Dao layer interface is to define the methods we need. Through this method, we can write our SQL statements through annotations, and then implement them by implementation classes. We can analyze how the process of business implementation is realized

  • In fact, all the interactions between us and the database are implemented based on the methods defined by the goodsdao interface. What really keeps in touch with the database is the implementation class of the goodsdao interface. The relationship among them is almost like this:
  • Analysis of mybatis service implementation in spring boot

**

Business layer interface and implementation class

The business layer is located on top of our Dao layer. The diagram is as follows:
Analysis of mybatis service implementation in spring boot
In fact, a medium is added, which is like a middleman, which straddles between Dao layer and test class. However, this middleman is very conscientious. It is willing to do many things for you, such as decoupling. Adding business layer can effectively reduce the coupling between codes. When we implement dependency injection in test classes, we often choose to accept a pair of goodservice type In this way, even if we change the implementation class, we don’t have to care whether the object under test will change. In this way, we can separate each part of the code. When a certain part changes, we only need to pay attention to the changed part itself, and many of the businesses we need to implement can also be written in this layer In this way, the structure of the code will be very clear.
The business layer and Dao layer look very similar in structure. They both define interfaces to implement class objects. The difference is that the implementation class objects of Dao layer are created by the bottom layer of the framework, while the class objects in the business layer are defined by ourselves. In the test class, we inject the business layer implementation class objects through @ Autowired annotation. The implementation class objects need to be described with @ service:
Analysis of mybatis service implementation in spring boot
Analysis of mybatis service implementation in spring boot