SpringBoot中MyBatis传参的方式

版本

SpringBoot调用MyBatis访问数据库,依赖为org.mybatis.spring.boot,版本为2.1.2

两种调用方式

两种调用方式为:注解方式和SqlProvider方式。
两种方式所遵循的规则相同:

无论是否使用@Param,都可以支持多参数,令传入与引用的参数名相同即可。

因此建议:不使用@Param直接传参,且保持传入与引用的参数名一致。

注解方式

@Select({"select id, name, age, class from student"})
List<Student> getStudents();

SqlProvider方式

Mapper文件:

@SelectProvider(type = StudentSqlProvider.class, method = "getStudents")
List<Student> getStudents();

SqlProvider文件:

public String getStudents() {
    String sql = "select id, name, age, class from student";
    return sql;
}

注解方式传参

注解方式传参建议采用以下规则:

无论是否使用@Param,都可以支持多参数,令传入与引用的参数名相同即可。

1个参数

@Select({"select id, name, age, class from student where id = #{id}"})
Student getStudent(Integer id);

@Select({"select id, name, age, class from student where id = #{id}"})
Student getStudent(@Param("id") Integer id);

@Select({"select id, name, age, class from student where id = #{id}"})
Student getStudent(@Param("id") Integer studentId);

@Select({"select id, name, age, class from student where id = #{id}"})
Student getStudent(Integer aValue);

如上4种方式,效果相同。MyBatis都可顺利识别id参数值。
特别注意第4种方式,在不加@Param的情况下,即使传入的参数名与引用的参数名不匹配,却依然可以正确识别。但若加了@Param,指定的参数名与引用的参数名就必须相同:

@Select({"select id, name, age, class from student where id = #{id}"})
Student getStudent(@Param("aValue") Integer id);

如上,运行会报错。

2个参数

@Select({"select id, name, age from student where age = #{age} and class = #{class}"})
Student getStudent(Integer age, Integer class);

@Select({"select id, name, age from student where age = #{age} and class = #{class}"})
Student getStudent(@Param("age") Integer age, @Param("class") Integer class);

@Select({"select id, name, age from student where age = #{age} and class = #{class}"})
Student getStudent(@Param("age") Integer studentAge, @Param("class")  Integer studentClass);

@Select({"select id, name, age from student where age = #{age} and class = #{class}"})
Student getStudent(@Param("age") Integer studentAge, Integer class);

如上4种方式,效果相同。MyBatis都可顺利识别age和class参数值。
注意第4种方式,允许部分字段使用@Param来指定参数名,其他字段依然使用传入的名称
与1个参数不同的是,多参数情况下:

  • 若不使用@Param,传入与引用的参数名必须匹配。
  • 若使用@Param@Param指定的参数名与引用的参数名必须匹配。

若不匹配则运行报错。

更换参数顺序

@Select({"select id, name, age from student where age = #{age} and class = #{class}"})
Student getStudent(Integer class, Integer age);

@Select({"select id, name, age from student where age = #{age} and class = #{class}"})
Student getStudent(@Param("class") Integer class, @Param("age") Integer age);

@Select({"select id, name, age from student where age = #{age} and class = #{class}"})
Student getStudent(@Param("class")  Integer studentClass, @Param("age") Integer studentAge);

如上。sql语句没有变化,依然是age先判断,class后判断。但传入的参数顺序变了,第1个参数是class,第2个参数是age。但MyBatis顺利识别出了参数值。

SqlProvider方式传参

SqlProvider方式传参建议采用与注解方式传参相同的规则:

无论是否使用@Param,都可以支持多参数,令传入与引用的参数名相同即可。

唯一的区别是,SqlProvider方式传参可以使用Map接收:

// Mapper传入参数
@SelectProvider(type = StudentSqlProvider.class, method = "getStudent")
Student getStudent(Integer class, Integer age);

// 对应参数接收
public String getStudent(Integer age, Integer class) {
    String sql = "select id, name, age, class from student where 1=1";
    sql += " and class = " + class;
    sql += " and age = " + age;
    return sql;
}

// Map接收
public String getStudent(Map<String, Integer> map) {
    String sql = "select id, name, age, class from student where 1=1";
    sql += " and class = " + map.get("class");
    sql += " and age = " + map.get("age");
    return sql;
}

如上,两种接收方式都可顺利接收到classage2个参数。

  • 非Map接收,只需要参数名相同,不需要参数顺序相同。
  • Map接收,从Map中获取参数时要与传入参数的名称相同。

org.apache.ibatis.binding.BindingException

使用如上规则,同样的代码,有的正常运行,有的会报:

org.apache.ibatis.binding.BindingException

经验证,与IDEA有关,暂不能确定是版本问题还是环境导致。出问题的IDEA是2018版本,更换为2020版本问题解决。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值