SpringBoot中MyBatis传参的方式

2023-11-16

版本

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版本问题解决。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SpringBoot中MyBatis传参的方式 的相关文章

  • 不同帐户上的 Spring Boot、JmsListener 和 SQS 队列

    我正在尝试开发一个 Spring Boot 1 5 应用程序 该应用程序需要侦听来自两个不同 AWS 帐户的 SQS 队列 是否可以使用 JmsListener 注解创建监听器 我已检查权限是否正确 我可以使用 getQueueUrl 获取
  • Junit:如何测试从属性文件读取属性的方法

    嗨 我有课ReadProperty其中有一个方法ReadPropertyFile返回类型的Myclass从属性文件读取参数值并返回Myclass目的 我需要帮助来测试ReadPropertyFile方法与JUnit 如果可能的话使用模拟文件
  • 在内存中使用 byte[] 创建 zip 文件。 Zip 文件总是损坏

    我创建的 zip 文件有问题 我正在使用 Java 7 我尝试从字节数组创建一个 zip 文件 其中包含两个或多个 Excel 文件 应用程序始终完成 没有任何异常 所以 我以为一切都好 当我尝试打开 zip 文件后 Windows 7 出
  • 如何使用assertEquals 和 Epsilon 在 JUnit 中断言两个双精度数?

    不推荐使用双打的assertEquals 我发现应该使用带有Epsilon的形式 这是因为双打不可能100 严格 但无论如何我需要比较两个双打 预期结果和实际结果 但我不知道该怎么做 目前我的测试如下 Test public void te
  • 谷歌应用程序引擎会话

    什么是java应用程序引擎 默认会话超时 如果我们将会话超时设置为非常非常长的时间 会不会产生不良影响 因为谷歌应用程序引擎会话默认情况下仅存储在数据存储中 就像facebook一样 每次访问该页面时 会话仍然永远存在 默认会话超时设置为
  • Java 集合的并集或交集

    建立并集或交集的最简单方法是什么Set在 Java 中 我见过这个简单问题的一些奇怪的解决方案 例如手动迭代这两个集合 最简单的单行解决方案是这样的 set1 addAll set2 Union set1 retainAll set2 In
  • 将流转换为 IntStream

    我有一种感觉 我在这里错过了一些东西 我发现自己做了以下事情 private static int getHighestValue Map
  • 无法创建请求的服务[org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]-MySQL

    我是 Hibernate 的新手 我目前正在使用 Spring boot 框架并尝试通过 hibernate 创建数据库表 我知道以前也问过同样的问题 但我似乎无法根据我的环境找出如何修复错误 休眠配置文件
  • 在 junit 测试中获取 javax.lang.model.element.Element 类

    我想测试我的实用程序类 ElementUtils 但我不知道如何将类作为元素获取 在 AnnotationProcessors 中 我使用以下代码获取元素 Set
  • 内部类的构造函数引用在运行时失败并出现VerifyError

    我正在使用 lambda 为内部类构造函数创建供应商ctx gt new SpectatorSwitcher ctx IntelliJ建议我将其更改为SpectatorSwitcher new反而 SpectatorSwitcher 是我正
  • 如何在谷歌地图android上显示多个标记

    我想在谷歌地图android上显示带有多个标记的位置 问题是当我运行我的应用程序时 它只显示一个位置 标记 这是我的代码 public class koordinatTask extends AsyncTask
  • Eclipse 选项卡宽度不变

    我浏览了一些与此相关的帖子 但它们似乎并不能帮助我解决我的问题 我有一个项目 其中 java 文件以 2 个空格的宽度缩进 我想将所有内容更改为 4 空格宽度 我尝试了 正确的缩进 选项 但当我将几行修改为 4 空格缩进时 它只是将所有内容
  • 使用 AsyncTask 传递值

    我一直在努力解决这个问题 但我已经到了不知道该怎么办的地步 我想做的是使用一个类下载文件并将其解析为字符串 然后将该字符串发送到另一个类来解析 JSON 内容 所有部件都可以单独工作 并且我已经单独测试了所有部件 我只是不知道如何将值发送到
  • 关键字“table”附近的语法不正确,无法提取结果集

    我使用 SQL Server 创建了一个项目 其中包含以下文件 UserDAO java public class UserDAO private static SessionFactory sessionFactory static se
  • 专门针对 JSP 的测试驱动开发

    在理解 TDD 到底是什么之前 我就已经开始编写测试驱动的代码了 在没有实现的情况下调用函数和类可以帮助我以更快 更有效的方式理解和构建我的应用程序 所以我非常习惯编写代码 gt 编译它 gt 看到它失败 gt 通过构建其实现来修复它的过程
  • Cucumber 0.4.3 (cuke4duke) 与 java + maven gem 问题

    我最近开始为 Cucumber 安装一个示例项目 并尝试使用 maven java 运行它 我遵循了这个指南 http www goodercode com wp using cucumber tests with maven and ja
  • 干净构建 Java 命令行

    我正在使用命令行编译使用 eclipse 编写的项目 如下所示 javac file java 然后运行 java file args here 我将如何运行干净的构建或编译 每当我重新编译时 除非删除所有内容 否则更改不会受到影响 cla
  • 创建一个 JSON 对象以在 Spring Boot 测试中发布

    我想编写基本测试来使用 JSON 负载在 users URL 上执行 POST 请求来创建用户 我找不到如何将新对象转换为 JSON 到目前为止有这么多 这显然是错误的 但解释了目的 Test public void createUser
  • 长轮询会冻结浏览器并阻止其他 ajax 请求

    我正在尝试在我的中实现长轮询Spring MVC Web 应用程序 http static springsource org spring docs 2 0 x reference mvc html但在 4 5 个连续 AJAX 请求后它会
  • 使用 CXF-RS 组件时,为什么我们使用 而不是普通的

    作为后续这个问题 https stackoverflow com questions 20598199 对于如何正确使用CXF RS组件我还是有点困惑 我很困惑为什么我们需要

随机推荐