Write MyBatis SQL with annotations or XML? How do dynamic SQL tags <if>, <foreach>, <choose>, <sql> work, and how does #{} differ from ${}?
Short answer
Annotations (@Select) fit short, fixed statements; dynamic SQL (optional conditions) belongs in XML with <where>, <if>, <foreach>. Most important: #{} passes parameters safely, ${} pastes the string straight in and invites SQL injection.
3 points to say:
- Every value uses
#{}. ${}only for column names checked against a whitelist.<choose>picks exactly one branch; several<if>s pick every true one.
Keep reading the deep dive
Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.