This article is a mirror article of machine translation, please click here to jump to the original article.

View: 13726|Reply: 0

[Source] mybatis notes are annotated using the Mapper interface

[Copy link]
Posted on 1/16/2019 1:00:53 PM | | | |
1. Mapping methods supported by mybatis

The mapping methods supported by mybatis include mapper.xml files based on xml and mapper interface classes based on java.
There are four main interface method annotations: @Insert, @Delete, @Update, and @Select

2. How to map using interface annotations

The following experiments are all based on t_user tables and are structured as follows:

2.1 Increase @Insert

There are basically three schemes for this, namely manual designation (application layer), self-adding primary key (data layer single table), and selecting primary key (data layer multiple table).

1. Manually specify the primary key at the application layer

The manual method does not treat the primary key differently, and will give the primary key a value when the application layer generates the object before insertion, which is no different from the normal field when inserted.

In the example above, mybatis doesn't know which field is the primary key, and id is the primary key field, but it is not treated differently.

Note that the #{username} method is to use User as the current context, so that when accessing User's attributes, you can directly write the attribute name.

2. The table adds the primary key

The self-incrementing primary key corresponds to the primary key backfill in the XML configuration, a simple example:

Use Option to correspond to the properties of the select tag set in XML, userGeneratordKeys to indicate that you want to use a self-incrementing primary key, and keyProperty to specify the field name of the primary key field.

The self-incrementing primary key uses the self-incrementing features of the underlying database.

3. Select the primary key

Select the primary key to generate a value from the data layer and use this value as the value of the primary key.


2.2 Delete @Delete

When deleting, just write the condition in the value of the @Delete annotation, and return an int type as the number of records that have been successfully deleted.


2.3 Change @Update

When modifying, just write the SQL statement in the value of the @Update, and return an int type to indicate the number of modified record lines.


2.4 Check @Select

Querying is a bit more complicated, because the query involves how to set the found fields to the object, and there are usually three ways:

1. Manually specify an alias in the SQL statement to match

When writing SQL statements, manually assign an alias to each field to match the object's attributes, which is suitable for situations where the table field name and the object attribute name are very different and there are not many table fields.


2. Use mybatis' automatic underline hump transformation

mybatis has an option called mapUnderscoreToCamelCase that applies when the field name in the table is the same as the object's attribute name, but the difference in underscore and hump writing.

After configuring mapUnderscoreToCamelCase, mybatis will try to convert the underscore to a hump when setting the data found in the ResultSet to the object, and then stitch the set in front to set the attributes.

Turn on the conversion:




Then query:

Looking at the printed result, birth_day attributes are populated into the object:



3. Use ResultMap

If the field name of the table and the attribute name of the object are not much the same, and there are many fields in the table, ResultMap should be used for adaptation.


@Results corresponds to the ResultMap in XML, and you can specify an id for it, which can be used to reference it elsewhere, such as to reference the above Results:

Use @ResultMap to reference an existing ResultMap, which can be defined in Java using @Results annotations or in XML using resultMap tags.

2.5 Sample Code

User.java


UserMapper.java

3. Summary

Advantages of using interface annotations:

1. It is more convenient and quick to write mapping statements

Disadvantages of using interface annotations:

1. It is suitable for relatively simple configurations, and the interface cannot be done when it is too complicated.

2. Unable to use dynamic SQL, a bit tasteless.






Previous:Demon Spirit Summon V4.1.8 is used in a low-key manner
Next:Kirin Article Originality Detection Tool v2.0 (remove author contact information)
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com