How to do dynamic SQL in myBatis 3.1.1 based on an enum constant parameter?
How to do dynamic SQL based on enum constants
public enum Test {
A, B;
}
Mapper.java:
int test(@Param("t") Test t);
Mapper.xml:
<select id="test" resultType="int">
select
<choose>
<when test='t.name().equals("A")'>65</when>
<when test='t.name().equals("B")'>66</when>
<otherwise>0</otherwise>
</choose>
</select>
Notes
MyBatis allows ==
instead of equals
for strings in if
(or when
) statements. So the following would also work (quotes don't matter):
public enum Test {
A, B;
}
Mapper.java:
int test(@Param("t") Test t);
Mapper.xml:
<select id="test" resultType="int">
select
<choose>
<when test="t.name() == 'A'">65</when>
<when test="t.name() == 'B'">66</when>
<otherwise>0</otherwise>
</choose>
</select>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With