MyBatis - 传递参数 7 种方式

简介: MyBatis - 传递参数 7 种方式

image.pngimage.pngimage.pngimage.png


方法0:单参数传递

public User selectUser(int deptId);
<select id="selectUser" resultMap="UserResultMap">
    select * from user where dept_id = #{param}
</select>

注:单参数传递名字可以和形参不一样。

方法1:顺序传参法(Default)

public User selectUser(String name, int deptId);
<select id="selectUser" resultMap="UserResultMap">
    select * from user
    where user_name = #{0} and dept_id = #{1}
</select>

#{}里面的数字代表你传入参数的顺序。

多参数传递时,Mybatis默认将传过来的参数用【arg0】【arg1】【……】或者【param1】【param2】【……】来替换。

Ps:在MyBatis3.4.4版不能直接使用#{0}要使用 #{arg0}。这种方法不建议使用,sql层表达不直观,且一旦顺序调整容易出错。

方法2:@Param注解传参法

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>

#{}里面的名称对应的是注解@Param括号里面修饰的名称。

这种方法在参数不多的情况还是比较直观的,推荐使用。

方法3:Map传参法

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>

#{}里面的名称对应的是Map里面的key名称。

这种方法适合传递多个参数,且参数易变能灵活传递的情况。

方法4:Java Bean 传参法

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

#{}里面的名称对应的是User类里面的成员属性。

这种方法很直观,但需要建一个实体类,扩展不容易,需要加属性,看情况使用。

方法5:@Param + 默认传参

public User selectUser(@Param("userName") String name, int deptId);
<select id="selectUser" resultMap="UserResultMap">
    select * from user
    where user_name = #{userName} and dept_id = #{param2}
</select>

@Param + 默认传参混合传参时,没有被标记@Param的形参,一一对应序号【argN(从0开始)】或【paramN(从1开始)】。

Ps:在MyBatis3.4.4版不能直接使用#{0}要使用 #{arg0}。

方法6:集合类型传参

  1. Collection
public User selectUser(Collection listId);
<select id="selectUser" resultMap="UserResultMap">
    select * from user where dept_id = #{collection[0]}
</select>
  1. List
public User selectUser(List listId);
<select id="selectUser" resultMap="UserResultMap">
    select * from user where dept_id = #{collection[0]}
</select>
public User selectUser(List listId);
<select id="selectUser" resultMap="UserResultMap">
    select * from user where dept_id = #{list[0]}
</select>
  1. Array
public User selectUser(int[] ids);
<select id="selectUser" resultMap="UserResultMap">
    select * from user where dept_id = #{array[0]}
</select>

Ps:Collection 在版本3.3以及之后,List 在版本3.2以及之后。

方法7:@Param + 数组

public User selectUser(@Param("param") int[] ids);
<select id="selectUser" resultMap="UserResultMap">
    select * from user where dept_id = #{test[0]}
</select>

1.parameterType 写不写?

  1. 答:parameterType写的话,则限制了输入的参数类型。当然也可以不用写,因为mybatis能自动识别,但返回值类型不能不写,因为mybatis需要将获得结果封装到相应的类中,查询的字段与类的属性需要一致(不一致的需要显示的配置)。
  2. 待更新...
目录
相关文章
|
7月前
|
SQL Java 数据库连接
在mybatis中,使用map传递参数和进行模糊查询的方法
在mybatis中,使用map传递参数和进行模糊查询的方法
200 0
|
4月前
|
SQL Java 数据库连接
Mybatis之核心配置文件详解、默认类型别名、Mybatis获取参数值的两种方式
【1月更文挑战第3天】 一、核心配置文件详解 二、默认的类型别名 三、MyBatis的增删改查 四、MyBatis获取参数值的两种方式 1、单个字面量类型的参数 2、多个字面量类型的参数 3、map集合类型的参数 4、实体类类型的参数 5、使用@Param标识参数
63 2
Mybatis之核心配置文件详解、默认类型别名、Mybatis获取参数值的两种方式
|
3天前
|
XML Java 数据库连接
MyBatis 解决上篇的参数绑定问题以及XML方式交互
MyBatis 解决上篇的参数绑定问题以及XML方式交互
6 0
|
5月前
|
SQL Java 数据库连接
Mybatis如何通过泛型来动态调整返回参数
Mybatis如何通过泛型来动态调整返回参数
96 0
|
2月前
|
SQL Java 数据库连接
Mybatis拦截器实现带参数SQL语句打印
Mybatis拦截器实现带参数SQL语句打印
|
4月前
|
SQL 缓存 Java
JAVAEE框架技术之8-myBatis ORM框架技术参数和动态SQL语句
JAVAEE框架技术之8-myBatis ORM框架技术参数和动态SQL语句
65 0
JAVAEE框架技术之8-myBatis ORM框架技术参数和动态SQL语句
|
4月前
|
Java 数据库连接 测试技术
一文彻底搞懂Mybatis系列(八)之Mybatis参数处理
一文彻底搞懂Mybatis系列(八)之Mybatis参数处理
150 0
|
5月前
|
SQL IDE Java
MyBatis【问题 01】mapper传入array\collection\list类型的参数时报BindingException:Parameter ‘xx‘ not found问题复现及解决
MyBatis【问题 01】mapper传入array\collection\list类型的参数时报BindingException:Parameter ‘xx‘ not found问题复现及解决
63 0
|
5月前
|
SQL Java 数据库连接
干翻Mybatis源码系列之第十一篇:Mybatis拦截器获取被拦截对象的方法和参数
干翻Mybatis源码系列之第十一篇:Mybatis拦截器获取被拦截对象的方法和参数