2

I have a weird question about '$' and '#', why doesn't the single '$' work?

The mybatis-spring-boot-starter version is 2.0.1.

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

That's ok

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

There is an error: 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'


I use mybatis configuration useActualParamName=true, so I don't need to apply @Param, if use @Param

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

or at the same time use '#' and '$'

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

That's ok.

Why does this happen?If not use @Param, the single '$' not work and throw exception?

Hardy
  • 21
  • 2

1 Answers1

1

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

In your case, if the value of userId is String, you get an exception.

Eeshwar Ankathi
  • 260
  • 3
  • 11