"MyBatis Learning 18" Use annotation configuration mapper

Source: Internet
Author: User

This blog address: http://blog.csdn.net/soonfly/article/details/67640653 (reproduced please specify the source)

MyBatis supports the use of annotations to configure mapped SQL statements so that the mapper XML file can be omitted. first, the mapping statement

1. Insert
For example, in an introductory instance:

public int Insert (user user) throws Exception;
1 1
<insert id= "Insert"  parametertype= "Twm.mybatisdemo.pojo.User" usegeneratedkeys= "true" keyproperty= "id" >
    <!--returns the primary key of the inserted data back to the user object--
    insert INTO user (Username,address,email) VALUES (#{username},#{address },#{email})
</insert>
1 2 3 4 1 2 3 4

Change to annotations to configure the mappings:

@Insert ("Insert into User (Username,address,email) VALUES (#{username},#{address},#{email})")
@Options ( Usegeneratedkeys = true, Keyproperty = "id") public 
int Insert (user user) throws Exception;
1 2 3 1 2 3

Here you use the @insert annotation to define an INSERT mapping statement.
and using the Usergeneratedkeys and Keyproperty attributes of the @options annotation, let the database auto_increment the generated primary key value, assign to the Keyproperty tag's property ID

There is also a way to get the primary key (Oracle uses select SEQ.) Nextval from DUAL, and order is set to before)

<insert id= "Insert"  parametertype= "Twm.mybatisdemo.pojo.User" >
    <!--returns the primary key of the inserted data back to the User object--
    <selectkey keyproperty= "id" order= "after" resulttype= "Java.lang.Integer" >
           select last_insert_id ()
    </selectKey>
    INSERT INTO user (Username,address,email) VALUES (#{username},#{address},#{email})
</insert>
1 2 3 4 5 6 7 1 2 3 4 5 6 7

The corresponding annotations are:

@Insert ("Insert into User (Username,address,email) VALUES (#{username},#{address},#{email})")
@SelectKey ( Statement= "Select last_insert_id ()", keyproperty= "id", Resulttype=int.class, before=true) public
int Insert (User User) throws Exception;
1 2 3 1 2 3

2. Select

@Select ("SELECT * from user where id=#{id}") the public
user Selectbyid (int id) throws Exception;
1 2 1 2

Returns a user object, so a toomanyresultsexception exception occurs if the SELECT statement returns multiple rows of records.

3. Update

@Update ("Update user set Username=#{username},address=#{address},email=#{email} where Id=#{id}") Public
int Update (user user) throws Exception;
1 2 1 2

4. Delete

@Delete ("Delete from user where Id=#{id}") the public
int Delete (int id) throws Exception;
1 2 1 2 Second, the result mapping

In the XML configuration file, the tags that map query results and JavaBean properties are <resultMap>. corresponding to the @results annotation

@Select ("SELECT * from user")
@Results ({@Result (id = true, column = ' id ', property = ' id '),
        @Result (column = "Use Rname ", property =" user_name "),
        @Result (column = ' City ', property = ' city ')}) public
list<user> SelectAll () throws Exception;
1 2 3 4 5 1 2 3 4 5

There is no way to reuse @Results annotations. For example, our public User Selectbyid (int id) throws exception also use the same @results annotation, but still need to re-write an identical @results

@Select ("SELECT * from user where id=#{id}")
@Results ({@Result (id = true, column = "id", property = "id"),
        @Resul T (column = "username", property = "user_name"),
        @Result (column = ' City ', property = ' city ')}) public
user SELECTB Yid (int id) throws Exception;
1 2 3 4 5 1 2 3 4 5

If you want to use a reusable mapper, use the @resultmap annotation. The annotation relies on an XML configuration file.
Create a new Usermapper.xml file under the same directory as the interface file and define a resultmap named UserMap.

<mapper namespace= "Twm.mybatisdemo.mapper.UserMapper" >
    <!--custom return result set-
    <resultmap id= " UserMap "type=" Twm.mybatisdemo.pojo.User ">
        <id column=" id "property=" id "jdbctype=" INTEGER "/>
        <result property= "user_name" column= "username" ></result>
        <result property= "City" column= "City" ></result>
    </resultMap>
</mapper>
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8

In Usermapper.java, use @resultmap to refer to Resultmap named UserMap for reuse.

 @Select ("select * from user where id=#{id}") 

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.