mybatis--Mapper Annotations

Source: Internet
Author: User
Tags comparison constructor

Since the original design, MyBatis is an XML-driven framework. The configuration information is XML-based, and the mapping statement is also defined in XML. And by the MyBatis 3, there are new options available. MyBatis 3 is built on a comprehensive and powerful Java configuration API. This configuration API is the basis for XML-based MyBatis configuration and is also the basis for a new annotation-based configuration. Annotations provide an easy way to implement simple mapping statements without introducing a lot of overhead.

Note Unfortunately, Java annotations limit their performance and flexibility. Even though most of the time spent on research, design and experimentation, the most powerful MyBatis mapping could not be built with annotations, that's not ridiculous. C # properties (as examples) do not have these limitations, so mybatis.net will have a richer choice than XML. In other words, the Java annotation-based configuration is inseparable from its characteristics.

the annotations have the following:


Annotations Target the corresponding XML Description
@CacheNamespace Class <cache> Configures the cache for a given namespace (such as a class). Properties: Implemetation,eviction, Flushinterval,size, and ReadWrite.
@CacheNamespaceRef Class <cacheRef> Use a cache of another namespace. Property: value, which should be a string value for a stewardess (that is, the fully qualified name of the class).
@ConstructorArgs Method <constructor> Collects a set of results that are passed to a winged Raiders object by constructing a method. Property: Value, which is an array of formal parameters.
@Arg Method <arg> <idArg> A separate constructor method parameter, which is part of the Constructorargs collection. Properties: Id,column,javatype,typehandler. The id attribute is a Boolean value that identifies the property used for comparison, similar to the <idarg>xml element.
@TypeDiscriminator Method <discriminator> A set of instance values is used to determine the performance of the result map. Properties: Column, Javatype, Jdbctype, typehandler,cases. The cases property is an array of instances.
@Case Method <case> The value of the singleton instance and its corresponding mapping. Properties: Value,type,results. The Results property is an array of results, so this annotation is similar to the actual resultmap, as specified by the following Results annotation.
@Results Method <resultMap> A list of result mappings that contains details of how a particular result column is mapped to a property or field. Property: Value, which is an array of Result annotations.
@Result Method <result> <id> A separate result mapping between columns and attributes or fields. Properties: Id,column, property, Javatype, Jdbctype, type Handler, One,many. The id attribute is a Boolean value that represents a property that should be used for comparison (and <id> similarity in XML mappings). The one property is a separate link, similar to <association>, while the many property is for collections, and <collection> is similar. They are named so as to avoid name collisions.
@One Method <association> Individual property value mappings for complex types. Properties: Select, the fully qualified name of the mapped statement (that is, the Mapper method), which can load an instance of the appropriate type. Note: Federated mappings are not supported in the annotations API. This is because of the limitations of the Java annotations and does not allow circular references.
@Many Method <collection> A mapping to a collection property of a complex type. Attributes:select, which is the fully qualified name of a mapped statement (i.e. mapper method) that can load a collectio N of instances of the appropriate types. NOTE you'll notice that join mapping are not supported via the Annotations API. This was due to the limitation in Java Annotations, which does not allow for circular references.
@MapKey Method Collection property mappings for complex types. Properties: Select, which is the fully qualified name of a mapping statement (that is, the Mapper method), that can load a set of instances of the appropriate type. Note: Federated mappings are not supported in Java annotations. This is because of the limitations of the Java annotations and does not allow circular references.
@Options Method Properties of the Map statement This annotation provides a broad range of access switches and configuration options that typically appear as attributes on the map statement. Instead of complicating each statement annotation, the options annotations provide a coherent and clear way to access them. Properties: Usecache=true, Flushcache=false, Resultsettype=forward_only, statementtype=prepared, FetchSize=-1,, timeout=-1 us Egeneratedkeys=false, keyproperty= "id". It is important to understand Java annotations, because there is no way to specify "null" as the value. Therefore, once you have used the Options annotation, the statement is governed by all default values. Be aware of what the default values are to avoid undesirable behavior.
@Insert @Update @Delete @Select Method <insert> <update> <delete> <select> Each of these annotations represents the actual SQL being executed. Each of them uses a string array (or a separate string). If you pass an array of strings, they are concatenated by each individual space separating them. This avoids the "lost space" problem when building SQL with Java code. However, if you like, you are also welcome to concatenate separate strings. Property: Value, which is a string array used to compose a separate SQL statement.
@InsertProvider @UpdateProvider @DeleteProvider @SelectProvider Method <insert> <update> <delete> <select> These optional SQL annotations allow you to specify a class name and a method at execution time to return running SQL that allows the creation of dynamic. Based on the execution of the mapping statement, MyBatis instantiates the class and executes the method specified by provider. This method can optionally accept the parameter object as its unique parameter, but must specify only that parameter or no parameters. Properties: Type,method. The Type property is the fully qualified name of the class. The method is the name of the methods in the class. Note: This section is followed by a discussion of the Selectbuilder class, which can help you build dynamic SQL in a clean, readable way.
@Param Parameter N/A If your mapper method requires multiple parameters, this annotation can be applied to the Mapper's method parameters to give each parameter a name. Otherwise, multiple parameters will be named in their order (excluding any rowbounds parameters), for example. #{param1}, #{param2}, and so on, this is the default. With @Param ("person"), the parameter should be named #{person}.
@SelectKey Method <selectkey> This annotation duplicates the <SELECTK Ey> functionality for methods annotated with @Insert or @InsertProvider. It is ignored for other methods. If you specify a @SelectKey annotation and then MyBatis'll ignore any generated key properties set via the @Options Annotat Ion, or configuration properties. Attributes:statement an array of strings which are the SQL statement to execute, keyproperty which are the property of the Parameter object that would be updated with the new value, before which must is either true or false to denote if the SQL s Tatement should be executed before or after the insert, resulttype which is the Java type of the Keyproperty, and Statemen Ttype=prepared.
@ResultMap Method N/A This annotation was used to provide the ID of A <resul tmap> element in an XML mapper to a @Select or @SelectProvider annotation. This allows annotated selects to reuse resultmaps that is defined in XML. This annotation would override any @Results or @ConstructorArgs annotation If both is specified on an annotated select.
@ResultType Method N/A This annotation was used when using a result handler. In this case, the return type was void so MyBatis must has a-to determine the type of object-construct for each row . If there is a XML result map, use the @ResultMap annotation. If The result type is specified in XML on the <select> element and then no other annotation is necessary. In the other cases, use this annotation. For example, if a @Select annotated method would use a result handler, the return type must is void and this annotation (or @ResultMap) is required. This annotation was ignored unless the method return type is void.

Mapper Annotation Examples

This example shows using the @SelectKey annotation to retrieve a value from a sequence before an insert:

@Insert ("Insert into Table3 (ID, name) VALUES (#{nameid}, #{name})"
@SelectKey (statement= "Call Next value for Testsequence ", keyproperty=" NameId ", Before=true, resulttype=int.class)
int insertTable3 (name name);

This example shows using the @SelectKey annotation to retrieve a identity value after an insert:

@Insert ("Insert into table2 (name) VALUES (#{name})")
@SelectKey (statement= "Call identity ()", keyproperty= "nameId ", Before=false, resulttype=int.class)
int insertTable2 (name name);


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.