How to generate dynamic SQL using if test in MyBatis

How to generate dynamic SQL using if test in MyBatis

You can use if test statements in MyBatis to generate dynamic SQL. When you use the if test statement, it is often null or not.

SELECT *
FROM TBL
WHERE 
  COMPANY = #{company}
  <if test="cd != null">
    AND CD = #{cd}
  </if>

If there are multiple conditions, use the logical conjunction or disjunction of and and or. When comparing with a numerical value, write as follows.

<if test="cd == 1">

When comparing to a string, enclose the string in single quotes.

<if test="cd == '01'">

When comparing with a boolean, use = as shown below.

<if test="isNew">

コメント

Discover more from 株式会社CONFRAGE ITソリューション事業部

Subscribe now to keep reading and get access to the full archive.

Continue reading

Copied title and URL