Detailed explanation of mybatis annotation configuration of mybatis learning notes

Java API

Now that you know how to configure mybatis and create a mapping file, you are ready to improve your skills. Mybatis's Java API is where you reap your efforts. As you will see, compared with JDBC, mybatis greatly simplifies your code and keeps it concise, easy to understand and maintain. Mybatis 3 has introduced many important improvements to make SQL mapping better.

Mybatis 3 is built on a comprehensive and powerful Java configuration API. This configuration API is the basis of mybatis configuration based on XML and the new annotation configuration.

Annotations provide a simple way to implement simple mapping statements without introducing a lot of overhead.

The targets and labels corresponding to mybatis common annotations are shown in the table:

Meanings of mybatis common annotations:

@Cachenamespace (size = 512): defines that built-in caches are allowed in this namespace

@Options (usecache = true, flushcache = false, timeout = 10000): option switches for some queries

@Param ("Id"): globally qualified alias, which defines the position of query parameters in SQL statements. It is no longer the sequential subscript 0,1,2,3 But the corresponding name, which is defined here.

@Results is an array with @ result as the element, @ result represents the mapping relationship between a single attribute and a field, and id = true indicates that the ID field is the primary key. Mybatis will give necessary optimization during query. All @ results in the array form the mapping relationship of a single record, and @ results is a collection of single records. In addition, there is a very important annotation @ resultmap, which is similar to @ results

@Select ("query statement"), @ insert ("add statement"), @ update ("UPDATE statement") and @ delete ("delete statement") represent the operations of querying, adding, updating and deleting data.

Next, let's look at the use of annotations.

(1) General annotation usage (operation without custom map):

Example 1

Tip: you need to register the mapper before calling the method:

Or in mapper Configure < mapper class = "mapper interface path" > < / mapper > in XML

After registration, get the mapper interface and call normally

(2) If you need to customize the map, you can use the results annotation:

Example 2

If the result set structures returned by multiple queries are the same, you can use @ resultmap to define the return structure. Using this annotation, you will have to configure your resultmap in your mapping file, and @ resultmap (value = "name") is the resultmap ID in the mapping file. In this way, you need to register your configuration file in < mapper >, Use @ resultmap in the interface to reference the resultmap ID in the configuration file as follows:

Example 3

SelfMapper. xml

SelfMapper. java:

Complete case

Interface code

Case 1 query an object

Case 2: pass multiple parameters and query multiple objects

Case 3 adding objects

Case 4 using HashMap for association query

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>