Use Mybatis to pass multiple parameters (three solutions) and mybatis Solution

Source: Internet
Author: User

Use Mybatis to pass multiple parameters (three solutions) and mybatis Solution

There are three methods to transmit multiple parameters that I have come into contact.

Solution 1

DAO layer Function Method

Public User selectUser(String name,String area);

Corresponding er. xml

<select id="selectUser" resultMap="BaseResultMap">    select  *  from user_user_t   where user_name = #{0} and user_area=#{1}</select>

# {0} indicates that the first parameter in the dao layer is received, and # {1} indicates the second parameter in the dao layer. More parameters are added after they are consistent.

Solution 2

This method uses Map to transmit multiple parameters.

Dao layer Function Method

Public User selectUser(Map paramMap);
Corresponding er. 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 specific parameter values"); paramMap. put ("userArea", "corresponds to specific parameter values"); User user = xxx. selectUser (paramMap );}

I personally think this method is not intuitive enough. When you see an interface method, you cannot directly know what parameters you want to transmit.

Solution 3 Function Method of Dao Layer

Public User selectUser(@param(“userName”)Stringname,@param(“userArea”)String area);
Corresponding er. xml

<select id=" selectUser" resultMap="BaseResultMap">   select  *  from user_user_t   where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR}</select>

I personally think this method is better, so developers can see what parameters should be passed in the dao layer method, which is more intuitive. I personally recommend this method.














How many parameters does mybatis pass?

Mybatis has never been used, and Ibatis has been used for a while.
The error is that you passed in.
Map. put ("productType", );
The productType parameter cannot be found.

If the parameter is not passed in, you can consider adding a dynamic Where condition to getScrollPage.
References: x

How does MyBatis pass multiple parameters?

In MyBatis, you can use the following method to pass multiple parameters: 1. use java. util. the Code is as follows: public List getAllUsersByUserName (String username, int start, int limit) {Map params = new HashMap (3); params. put ("username", username); params. put ("start", start); params. put ("limit", limit); return userMapper. getAllUsersByUserName (params);} corresponding XXMapper. the xml file is as follows: SELECT u. * FROM User u WHERE u. username LIKE # {username} '%' LIMIT # {start}, # {limit} 2. the Code is as follows: public List getUsersByUserName (String username) {User user User = new user (); User. setUsername (username); return userMapper. getUserByUsername (user);} corresponding XXMapper. the xml file is as follows: SELECT u. * FROM User u WHERE u. username = # {username} OK. Let's introduce these two methods !!!

Related Article

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.