Detailed Explanation of SpringBoot and Integrated Component Annotations (2) - Mybatis

4. Commonly used annotations in the development of Mybatis full annotations

1、@Mapper

Acting on the dao interface, the declaration is a mapper class

2. @Insert, @Delete, @Update, and @Select

These four annotations respectively represent the SQL statements to be executed, acting on the methods of the dao interface. They take an array of strings (or a single string) as arguments. If an array of strings is passed, the strings will be padded with a space and then concatenated into a single complete string. This effectively avoids the "missing spaces" problem when constructing SQL statements in Java code. However, you can also manually concatenate the strings ahead of time. The attributes are: value, the filled value is an array of strings used to form a single SQL statement.

3、@InsertProvider 、@UpdateProvider 、@DeleteProvider 以及@SelectProvider

Allows building dynamic SQL. These alternative SQL annotations allow you to specify class names and methods that return SQL statements to execute at runtime. (Since MyBatis 3.4.6, you can use CharSequence instead of String to return the type return value.) When executing the mapping statement, MyBatis will instantiate the class and execute the method. The class and method are the values ​​​​filled with annotations. You can use the objects that have been passed to the mapping method as parameters, "Mapper interface type" and "Mapper method" will pass ProviderContext (only supported in MyBatis 3.4.5 and above) as parameter values. (MyBatis 3.4 and above versions support multi-parameter input) The attributes are: type, method. The type attribute needs to be filled with a class. method needs to fill in the method name defined by this class. Note that the next subsections discuss classes that can help you build dynamic SQL more easily.

4、@Results

For methods, the list of result mappings contains details of how a particular result column is mapped to an attribute or field. The attributes are: value, id. The value attribute is an array of Result annotations. The id attribute is the name of the resulting map.

5、@Result

Individual result mappings between columns and attributes or fields. The attributes are: id, column, javaType, jdbcType, typeHandler, one, many. The id attribute is a boolean value that identifies the attribute that should be used for comparison (similar to <id> in XML maps). The one attribute is a single association, similar to <association>, while the many attribute is for collections, similar to <collection>. They are named this way to avoid name conflicts.

6、@One

Individual property value maps for complex types. The attributes are: select, the fully qualified name of the mapped statement (that is, the mapper method) that can load an instance of the appropriate type. fetchType overrides the global configuration parameter lazyLoadingEnabled. Note that union mappings are not supported in the annotations API. This is because of the limitations of Java annotations, which do not allow circular references.

7、@Many

Maps to collection properties of complex types. The attributes are: select, the fully qualified name of the mapped statement (that is, the mapper method), which can load a collection of instances of the appropriate type, and fetchType will override the global configuration parameter lazyLoadingEnabled. Note that union mappings are not supported in the annotations API. This is because of the limitations of Java annotations, which do not allow circular references

8、@MapKey

This is an annotation used on methods that return a Map. It can convert the List storing objects into a Map whose key value is a certain attribute of the object. Attributes include: value, which is the attribute name of the object to be filled in as the key value of the Map.

9、@Param

If your mapping method has multiple formal parameters, this annotation can be used on the parameters of the mapping method to give them custom names. If no custom name is given, multiple parameters (excluding the RowBounds parameter) are prefixed with "param" followed by their parameter positions as parameter aliases. For example #{param1}, #{param2}, this is the default value. If the annotation is @Param("person"), then the parameter will be named #{person}.

10、@SelectKey

The function of this annotation is exactly the same as the <selectKey> tag, and it is used on methods annotated by @Insert or @InsertProvider or @Update or @UpdateProvider. If the @SelectKey annotation is made on a method that is not annotated by the above four annotations, it will be considered invalid. If you specify the @SelectKey annotation, MyBatis will ignore the generated primary key or configuration properties set by the @Options annotation. The properties are: statement fills in the SQL string array that will be executed, keyProperty fills in the value of the property of the parameter object that will be updated, before fills in true or false to indicate whether the SQL statement should be executed before or after the insert statement. resultType fills in the Java type of keyProperty and fills in statementType with any value of STATEMENT, PREPARED or CALLABLE in Statement, PreparedStatement and CallableStatement. The default value is PREPARED.

11、@ResultMap

This annotation provides @Select or @SelectProvider with the id of the <resultMap> in the XML map. This enables annotated selects to reuse ResultMaps defined in XML. If there are @Results or @ConstructorArgs in the same select annotation, then these two annotations will be covered by this annotation.

12、@ResultType

This annotation is used when a result handler is used. In this case, the return type is void, so Mybatis must have a way to determine the type of object used to construct each row of data. If you have an XML result map, use the @ResultMap annotation. If the result type is specified in the XML <select> node, no other annotations are needed. Otherwise use this annotation. For example, if @Select is annotated on a method that will use a result handler, then the return type must be void and this annotation (or @ResultMap ) is mandatory. This annotation only takes effect when the method return type is void.

13、@Flush

If this annotation is used, methods defined in the Mapper interface can call the SqlSession#flushStatements() method.

Guess you like

Origin blog.csdn.net/qq_42133100/article/details/89098388