Mybatis (2) parameter (Parameters) transfer, mybatisparameters

Source: Internet
Author: User

Mybatis (2) parameter (Parameters) transfer, mybatisparameters
Pass the Mybatis parameter (Parameters) 1. Single Parameter

Values of the basic type, object type, and set type are acceptable. In this case, MyBatis can directly use this parameter without any processing.

 

<! -- Query a record in the data table by id and encapsulate the User object --> <select id = "selectById" resultType = "com. softjx. model. user "> select t_id as id, t_username as username, t_password password from t_user where t_id =#{ id}; </select> <! -- Query a record in the data table by id. parameterType is used to specify the parameter type. The parameter variable name can be defined at will, not controlled by attributes in javabean --> <select id = "selectById1" parameterType = "java. lang. integer "resultType =" com. softjx. model. user "> select t_id as id, t_username as username, t_password password from t_user where t_id =#{ id1}; </select>

 

2. POJO

When these parameters belong to our business POJO, we pass them directly.

 

<! -- Add user --> <insert id = "insertUser"> insert into t_user (t_username, t_password) values (# {username}, # {password}) </insert> <! -- Add a user --> <! -- The parameter can be omitted. In any case, the variable name is the attribute name in javabean --> <insert id = "insertUser1" parameterType = "com. softjx. model. user "> insert into t_user (t_username, t_password) values (# {username}, # {password}) </insert>

 

3. Map

We can also encapsulate multiple parameters as map and pass them directly.

 

Public int insertUser2 (Map <String, Object> map); <! -- Add a user --> <! -- The parameter is map, and the variable name is random. During the call, the map key must be consistent with the variable name, and the parameter can also be omitted --> <insert id = "insertUser2" parameterType = "java. util. map "> insert into t_user (t_username, t_password) values (# {a },# {B}) </insert> @ Test public void TestInserUser2 () {System. out. println ("add a user"); try {UserMapper mapper = session. getMapper (UserMapper. class); System. out. println (mapper); Map <String, Object> map = new HashMap <String, Object> (); map. put ("a", "lisi"); map. put ("B", "222222"); int count = mapper. insertUser2 (map); session. commit (); // you must submit it here. Otherwise, the data cannot be imported into the database. // session. rollback (); System. out. println (count);} finally {session. close ();}}

 

4. Multiple Parameters

Any multiple parameters will be repackaged into a Map by MyBatis. The key of Map is param1, param2, or 0, 1, and the value is the value of the parameter.

 

// Add the user public int insertUser3 (String name, String pass );

 

 

 

 

<! -- Add a user --> <! -- Contains multiple parameters: parameter name # {param1}, # {param2}, or # {0}, #{1 }, parameter Name --> <insert id = "insertUser3" parameterType = "java. util. map "> insert into t_user (t_username, t_password) values (# {param1}, # {param2}) </insert> <insert id =" insertUser3 "parameterType =" java. util. map "> insert into t_user (t_username, t_password) values (# {0 },# {1}) </insert>

 

@ Test public void TestInserUser3 () {System. out. println ("add a user"); try {UserMapper mapper = session. getMapper (UserMapper. class); System. out. println (mapper); int count = mapper. insertUser3 ("aaa", "bbb"); session. commit (); // you must submit it here. Otherwise, the data cannot be imported into the database. // session. rollback (); System. out. println (count);} finally {session. close ();}}

 

5. Name Parameters

If @ Param is used for a parameter, MyBatis encapsulates these parameters into map, and the key is the name we specify.

// Add the public int insertUser4 (@ Param ("name") String name, @ Param ("pass") String pass );
<! -- Add a user --> <! -- With multiple parameters, the @ Param annotation is used, and the parameter name is directly defined using the name # {name}, # {pass} Or, # {param1}, #{param2 }, # {0}, # {1} --> <insert id = "insertUser4" parameterType = "java. util. map "> insert into t_user (t_username, t_password) values (# {name },# {pass}) </insert>
@ Testpublic void TestInserUser4 () {System. out. println ("add a user"); try {UserMapper mapper = session. getMapper (UserMapper. class); System. out. println (mapper); int count = mapper. insertUser4 ("song", "555"); session. commit (); // you must submit it here. Otherwise, the data cannot be imported into the database. // session. rollback (); System. out. println (count);} finally {session. close ();}}

 

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.