Mybatis development annotation quick start

Rapid popularization

1. What is mybatis

Mybatis is an excellent persistence layer framework that supports common SQL queries, stored procedures and advanced mapping.

Mybatis eliminates almost all the manual setting of JDBC code and parameters and the retrieval encapsulation of result sets. Mybatis can use simple XML or annotations for configuration and original mapping, and map the interface and Java POJO (plain old Java objects) into records in the database. Mybatis implements interface binding, which is more convenient to use.

The improvement of object relationship mapping is more efficient

Mybatis uses powerful ognl based expressions to eliminate other elements.

2. Functional architecture

3. Execution process

Detailed explanation of principle:

Mybatis application creates sqlsessionfactory according to the XML configuration file. According to the configuration, the configuration of sqlsessionfactory comes from two places: one is the configuration file and the other is the annotation of Java code to obtain an sqlsession. Sqlsession contains all the methods required to execute SQL. You can directly run the mapped SQL statement through the sqlsession instance to complete the addition, deletion, modification, query and transaction submission of data. Close the sqlsession after it is used up.

Let's take a look at the simple annotations of mybatis, which is the focus of this article

Key words:

@Insert: insert SQL, with the same syntax as XML insert SQL

@Select: query SQL. The syntax is exactly the same as XML select SQL

@Update: update SQL. The syntax is exactly the same as that of XML update SQL

@Delete: delete SQL. The syntax is exactly the same as XML delete SQL

@Param: input parameter

@Results: result set

@Result: result

1. Domain model:

2. Interface definition:

3、mybatis xml config:

In this way, we have completed the demo of mybatis using annotations. Do you think it's very simple~~

If you are familiar with mybatis XML, in most cases, you need to map the field name of the database table to class do. The mybatis annotation also provides the function of mapping, and the syntax is similar.

Of course, the above is just a simple SQL. Think about if we need to update the user information and want to update the specified attribute value. In other words, generate SQL dynamically like XML. Then we can't simply use the @ update annotation. Fortunately, the powerful mybatis also provides dynamic SQL assembly.

Dynamic SQL

The corresponding relationship is as follows

@Insert :@InsertProvider @Select :@SelectProvider @Update :@UpdateProvider @Delete :@DeleteProvider

The four provider annotations identify the use of dynamic SQL, using the syntax format:

How to construct dynamic SQL

The knowledge points mentioned in this article are relatively basic. For in-depth understanding, see the official website documentation or look at the source code.

Summary:

1. How to select XML and annotations? It varies from person to person. Each person's code has his own habits. XML and annotation have their own advantages and disadvantages. The disadvantages of XML: when the model attributes are changed, it is necessary to change from do to Dao to XML. It's painful to think about it. XML also has advantages. SQL fragments are easy to reuse and the syntax is approachable. Unlike annotation, it is necessary to build a class to construct a dynamic statement. Moreover, when a piece of SQL is referenced many times, the code appears redundant, At this time, we must use XML to extract common use. I make complaints about mybatis, and that note is not useless. No, mybatis is suitable for scenarios where model attributes change frequently, because it can combine reflection and regular matching to dynamically construct SQL (purely blind, personal ideas, it should be possible to implement it, and send it another day). It can be said that the annotation advantages of mybatis make up for the shortcomings of XML. The two complement each other~

The above is a quick start to mybatis development annotation introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>