mybatis--Mapper Annotation

Source: Internet
Author: User
Tags comparison constructor

Because originally designed, 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 foundation of an xml-based mybatis configuration and a new basis for annotation configuration. Annotations provide an easy way to implement simple mapping statements without introducing a large amount of overhead.

Note Unfortunately, Java annotations limit their performance and flexibility. Although a lot of time is spent on research, design and experimentation, the most powerful mybatis mappings cannot be built with annotations, which is not ridiculous. C # attributes (as an example) do not have these limitations, so mybatis.net will have a richer choice than XML. In other words, the configuration based on Java annotations cannot be separated from its characteristics.

the notes have the following:


Annotations Target the corresponding XML Description
@CacheNamespace Class <cache> Configures caching for a given namespace, such as a class. Properties: Implemetation,eviction, Flushinterval,size and ReadWrite.
@CacheNamespaceRef Class <cacheRef> Used in reference to the cache of another namespace. Attribute: value, which should be the string value of a stewardess (that is, the fully qualified name of the class).
@ConstructorArgs Method <constructor> Collects a set of results to pass to a whoever rescues object's construction method. Property: Value, which is an array of formal arguments.
@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 the comparison, similar to the <idarg>xml element.
@TypeDiscriminator Method <discriminator> A set of instance values is used to determine the performance of the result mapping. Properties: Column, Javatype, Jdbctype, typehandler,cases. The cases property is an array of instances.
@Case Method <case> The value of the individual instance and its corresponding mapping. Properties: Value,type,results. The Results property is an array of results, so this annotation is very similar to the actual resultmap, as specified by the Results annotation below.
@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 a column and a property or field. 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> similar in an XML map). The one property is a separate connection, similar to <association>, and the many property is similar to the set, and <collection>. They are named so as to avoid name collisions.
@One Method <association> Individual attribute value mappings for complex types. Properties: Select, the fully qualified name of the mapped statement (that is, the Mapper method), which can load instances of the appropriate type. Note: Federated mappings are not supported in the annotation 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 The property of a complex type. Attributes:select, which is the fully qualified name to a mapped statement (i.e. mapper method) that can load a collectio N of instances of the appropriate types. You'll notice that join mapping are not supported via the annotations API. This is due to the "limitation in Java annotations" does not allow for circular.
@MapKey Method Collection property mappings for complex types. Properties: Select is the fully qualified name of the 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 mapping statement This annotation provides a broad range of access Exchange and configuration options, which are usually presented as attributes on the mapping statement. Instead of complicating each statement annotation, the Options annotation provides 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 a value. Therefore, once you use the Options annotation, the statement is subject to all default values. Be aware of what default values are to avoid unexpected behavior.
@Insert @Update @Delete @Select Method <insert> <update> <delete> <select> Each of these annotations represents the actual SQL executed. Each of them uses a string array (or a separate string). If you pass an array of strings, they are concatenated by each separate space that separates them. This avoids the "loss of space" problem when building SQL with Java code. However, if you like, also welcome you to concatenate a separate string. Attribute: value, which is an array of strings used to compose separate SQL statements.
@InsertProvider @UpdateProvider @DeleteProvider @SelectProvider Method <insert> <update> <delete> <select> These optional SQL annotations allow you to specify a class name and a method to return to run when executed to allow the creation of dynamic SQL. Based on the execution of the mapping statement, MyBatis instantiates the class and then executes the method specified by provider. This method can optionally accept the parameter object as its unique parameter, but must specify only this parameter or no parameters. Properties: Type,method. The Type property is the fully qualified name of the class. Method is the name of the way 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, the multiple parameters will be named in their ordinal position (excluding any rowbounds parameters) such as. #{param1}, #{param2}, and so on, this is the default. Using the @Param ("person"), the parameter should be named #{person}.
@SelectKey method <selectkey> annotation duplicates the <SELECTK Ey> functionality for methods annotated with @Insert or @InsertProvider. It is ignored for the other methods. If you specify a @SelectKey annotation, 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'll 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 is allows annotated selects to reuse Resultmaps the are in XML. This annotation would override any @Results or @ConstructorArgs annotation if both are specified on a 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 have a way to determine the type of object to construct for each row . If there is a XML result map, use the @ResultMap annotation. If The result type was specified in XML on the <select> element, 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 sequence before a insert:

@Insert (Insert into Table3 (ID, name) VALUES (#{nameid}, #{name}))
@SelectKey (statement= "called 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 a 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.