MyBatis SQL statement mapping File (iii)----parameter passing

Source: Internet
Author: User

1. Single Basic type parameters (String,int, etc.)

A single primitive type parameter that parametertype the value of the corresponding statement to the same type as the parameter. Then use "#{parameter name}" to get

Java code

Parameter username of type string
public User finduser (String usernumber) { return (User) Sqlsessiontemplate.selectone ("Finduserbyusernumber", Usernumber); }

XML code

Corresponds to Paramtertype= "string"
<id= "Finduserbyusernumber" parametertype= "string" Resulttype= "user"> select * from User where usernumber = #{usernumber} </ Select >

2.Java Entity type parameters

Java entity type parameter: Sets the value of the ParameterType to the corresponding Java entity class, and then uses the property name of the #{class} to get

Java code

User entity class
publicvoid updateUser (user user) { sqlsessiontemplate.update ("UpdateUser", user); }

XML code

Point the ParameterType type to the specific address of the corresponding class (package name + class name), or to the alias (in the Mybatis.xml) file in Typealias
<id= "UpdateUser" parametertype= "User"> update user set Username=#{username},loginname=#{loginname},loginpassword=#{loginpassword},sex=#{sex}, Birthday=#{birthday} where Usernumber=#{usernumber} </update>

3. Multiple basic type parameters
In a specific business relationship, it is often necessary to pass multiple parameters, such as: Login-----> Username + password

 public   User Finduser (String loginname, String Loginpassword { //  TODO Auto-gene Rated method stub  map<string,string> map=new  hashmap<string, S        Tring> ();        Map.put ( "LoginName" , LoginName);        Map.put ( "Loginpassword" , Loginpassword);  return  (User) sqlsessiontemplate.selectone ("Loginuser" 
       <id= "Loginuser"  parametertype= "Map"  resulttype  = "User">            select * from User where loginname=#{loginname} and Loginpassword=#{loginpassword }        </Select>

One way to take advantage of Map knowledge multi-parameter transfer, there are other ways to achieve

 

MyBatis SQL statement mapping File (iii)----parameter passing

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.