[Mybatis-multi-parameter processing] mybatis passes multiple parameters (not using @param annotation and using @param annotation)

Method 1: Sequential parameter transmission

1. Passing multiple parameters without @param annotation
Note: What you get with jdk1.7 is: [1, 0, param1, param2] What
you get with 1.8 is: [arg1, arg0, param1, param2]

The numbers in # {} represent the order of your incoming parameters.
This method is not recommended, the SQL layer expression is not intuitive, and once the sequence is adjusted, it is easy to make mistakes.
Give a chestnut:
Dao layer

List<User> demo(int userid, String name);
  •  

Corresponding XML writing
jdk1.7

<select id="demo" resultMap="User">
		select *
			from user where user_id=#{0} and name= #{1}
	</select>

After jdk1.8 The
first way to write

<select id="demo" resultMap="User">
		select *
			from user where user_id=#{arg0} and name= #{arg1}
	</select>

The second way of writing

<select id="demo" resultMap="User">
		select *
			from user where user_id=#{param0} and name= #{param1}
	</select>

Method 2: @Param 注解 传 parameter method

public User selectUser(@Param("userName") String name, int @Param("deptId") deptId);

<select id="selectUser" resultMap="UserResultMap">
    select * from user
    where user_name = #{userName} and dept_id = #{deptId}
</select>

The name in # {} corresponds to the name modified in the annotation @Param bracket.

This method is relatively intuitive when there are not many parameters, and it is recommended.

Method 3: Map pass method

public User selectUser(Map<String, Object> params);

<select id="selectUser" parameterType="java.util.Map" resultMap="UserResultMap">
    select * from user
    where user_name = #{userName} and dept_id = #{deptId}
</select>

The name in # {} corresponds to the key name in Map.

This method is suitable for passing multiple parameters, and the parameters can be flexibly passed.

Method 4: Java Bean pass parameter method

public User selectUser(Map<String, Object> params);

<select id="selectUser" parameterType="com.test.User" resultMap="UserResultMap">
    select * from user
    where user_name = #{userName} and dept_id = #{deptId}
</select>

The names in # {} correspond to the member attributes in the User class.

This method is very intuitive, but you need to build an entity class, it is not easy to expand, you need to add attributes, depending on the situation.

28 original articles published · Like 3 · Visits 40,000+

Guess you like

Origin blog.csdn.net/qq_34291570/article/details/105520132