MyBatis solutions for passing multiple parameters (three kinds) _java

Source: Internet
Author: User

Small knitting for everyone to share three solutions to solve the problem of MyBatis pass multiple parameters, the specific description as follows:

First Scenario

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_ar Ea=#{1}
</select>

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

The second option

This method uses map to transmit 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 Call

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

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

The third type of programme

The function method of DAO layer

Public User Selectuser (The @param ("UserName") Stringname, @param ("Userarea") 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}

Personally think this method is better, can let the developer see DAO layer method to know what kind of parameters to pass, more intuitive, personal recommend this kind of scheme.

The above is a small series to introduce the MyBatis to pass multiple parameters of the solution, I hope to help you!

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.