MyBatis Learning (iv) XML file for SQL mapping of XML configuration file

Source: Internet
Author: User

Common elements of SQL mapping files:

  

1.select

A query statement is one of the most commonly used statements in MyBatis.

The select element that executes a simple query is very simple:

<select id= "Selectuser" parametertype= "int" resulttype= "HashMap" > select * FROM person WHERE id = #{id}</selec T>

This statement is called Selectuser, takes an int type argument, and returns an object of type HashMap.

#{id} tells MyBatis to create a preprocessing parameter that is equivalent to "?" in JDBC.

The following describes the properties of the Select element:

  

   

3.insert, UPDATE, delete

Describes the properties of these three elements:

Example:

<insert id= "Insertuser" parametertype= "Com.dj.domain.User" >insert INTO User (Id,username,password,email, Bio) VALUES (#{id},#{username},#{password},#{email}) </insert><update id= "UpdateUser" parametertype= " Com.dj.domain.User ">update User setusername = #{username},password = #{password},email = #{email},bio = #{bio} WHERE id = #{id}</update><delete id= "deleteuser" parametertype= "int" >delete from User where id = #{id}< ;/delete>  

  

As mentioned earlier, the INSERT statement has a bit more, and it has some properties and child elements to handle the generation of the primary key. First, if your database supports fields that automatically generate primary keys (such as MySQL and SQL Server databases), you can set usegeneratedkeys= "true" and set Keyproperty to the target properties you have already done. For example, if the Author table above already uses an automatically generated column type for the ID, the statement can be modified to:

<insert id= "Insertuser" parametertype= "Com.dj.domain.User"        usegeneratekeys= "true" keyproperty= "id" >  Insert into User (Id,username,password,email,bio) VALUES (#{id},#{username},#{password},#{email}) </insert> 

MyBatis there is another way to generate a primary key for a database that does not support auto-generated types, such as Oracle, or for JDBC drivers that might not support auto-generating primary keys.

    <insert id= "Insertuser" parametertype= "Com.dj.domain.User" >        <selectkey keyproperty= "id" resulttype= " int "order=" before ">            select Sequence_t_user.nextval as ID from dual        </selectKey>    Insert Into User (Id,username,password,email,bio)    values (#{id},#{username},#{password},#{email})    </insert > 

The Select element runs first, the user's ID is set, and the INSERT statement is called.

The Selectkey element is described as follows:
  

ParameterType can be set to basic data types and complex types, such as a user type.

4.resultMap

Resultmap is the most important and powerful element of MyBatis. Its role is to tell MyBatis to convert the data taken from the result set to the object that the developer needs.

In the first case, you can use RESULTMAP to handle the columns of the data you are querying and the properties of the objects that need to be returned in an inconsistent way.

In the second case, when a multi-table query is made, the returned object is associated to another object, and the simple mapping does not solve the problem, you must use the Resultmap element to complete the correlation mapping.

Next we have a simple test for resultmap: To Be Continued ... Update as soon as possible.

MyBatis Learning (iv) XML file for SQL mapping of XML configuration file

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.