我有一个关于“$”和“#”的奇怪问题,为什么单个“$”不起作用?
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/