gpt4 book ai didi

java - mybatis中使用$与#

转载 作者:行者123 更新时间:2023-11-30 05:33:39 24 4
gpt4 key购买 nike

我有一个关于“$”和“#”的奇怪问题,为什么单个“$”不起作用?

mybatis-spring-boot-starter版本是2.0.1。

@Select("select * from user where user_id=#{userId}")
User findUserId(int userId);

没关系

@Select("select * from user where user_id=${userId}")
User findUserId(String userId);

有一个错误: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'userId' in 'class java.lang.String'


我使用mybatis配置useActualParamName=true ,所以我不需要申请@Param ,如果使用@Param

@Select("select * from user where user_id=${userId}")
User findUserId(@Param("userId") String userId);

或者同时使用“#”和“$”

@Select("select * from user where user_id=${userId} and status=#{status}")
User findUserId(String userId, int status);

没关系。

为什么会出现这种情况?如果不使用@Param ,单个 '$' 不起作用并抛出异常?

最佳答案

In mybatis, #{variable} gets replaced with 'variable value' and ${variable} is replaced with variable value(without quotes).

在您的情况下,如果 userId 的值为 String,则会出现异常。

关于java - mybatis中使用$与#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57036084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com