MyBatis passing parameters to Mapping.xml

Source: Internet
Author: User

The first scenario, passed by ordinal

The function method of DAO layer

Public User Selectuser (String name,string area);

The corresponding Mapper.xml

<select id= "Selectuser" resultmap= "Baseresultmap" >    Select  * from  user_user_t   where user_name = #{0} and user_area=#{1}</select>

Where #{0} represents the first parameter in the DAO layer, #{1} represents the second parameter in the DAO layer, and more parameters are consistently added.

The second scenario, passing through map

This method uses map to pass multiple parameters.

The function method of DAO layer

Public User Selectuser (Map parammap);

The corresponding Mapper.xml

<select id= "Selectuser" resultmap= "Baseresultmap" >   Select  * from  user_user_t   where user_name = #{username,jdbctype=varchar} and user_area=#{userarea,jdbctype=varchar}</select>

Service Layer Invocation

Private User Xxxselectuser () {Map parammap=new hashMap ();p arammap.put ("UserName", "corresponding to specific parameter values");p arammap.put ("Userarea "," corresponding to the specific parameter value "); User user=xxx. Selectuser (PARAMMAP);}

Personally, this method is not intuitive enough to see the interface method can not directly know what parameters to pass.

The third scenario, passed by a single parameter name with @param, is recommended

The function method of DAO layer

Public User Selectuser (@param ("UserName") Stringname, @param ("Userarea") of String area);

The corresponding Mapper.xml

<select id= "Selectuser" resultmap= "Baseresultmap" >   Select  * from  user_user_t   where user_name = #{username,jdbctype=varchar} and user_area=#{userarea,jdbctype=varchar}</select>

The fourth scheme, passed through the parameter object with @param

The function method of DAO layer

Public User Selectuser (@param ("user") user user, @param ("Userarea") of String area);

The corresponding Mapper.xml

<select id= "Selectuser" resultmap= "Baseresultmap" >   Select  * from  user_user_t   where user_name = #{user.username, Jdbctype=varchar} and User_area=#{userarea,jdbctype=varchar} </select>


In each of these ways, you do not need to write the ParameterType property in XML

MyBatis passing parameters to Mapping.xml

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.