Difference between # and $ in MyBatis

Difference between # and $ in MyBatis

This is about the difference between # and $ in MyBatis 3.0.4.

${aaa}・・・Output the value of aaa as it is.

#{aaa}・・・Escapes or, in the case of strings, encloses them in single quotes.

There is a difference between the above.

<select id="selectA" parameterType="map" resultType="map">
  FROM ${schema}.TB_A AS A
  WHERE A.COLUMN = #{id}

As above, it is correct to use $ for schemas, etc., since they should not be enclosed in single quotes.

Since parameterType is map, Map is passed on the Java side.

Since resultType is also a map, the return value is also a map on the Java side.

The following is an image of the Java side.

Map cond = new HashMap<String, String>();
cond.put("id", "~~");
Map ret = (Map<String, String>) session.selectOne("selectA", cond);