MyBatis mapper文件中使用常量
Java 開發中會經常寫一些靜態常量和靜態方法,但是我們在寫sql語句的時候會經常用到判斷是否等於
//靜態類
public class CommonCode {
public static String EENTERPRISE_PRODUCE = "waste_produce_enterprise"
public static String EENTERPRISE_DISPOSAL = "waste_disposal_enterp"
//內部類
public static class Enterprise {
public static String EENTERPRISE_PRODUCE = "waste_produce_enterprise";
public static String EENTERPRISE_DISPOSAL = "waste_disposal_enterprise";
}
}
我們在mapper文件中做判斷
//內部類 注意**$** 符號
<if test="(type ==@com.xx.xxxx.xxxxxxxxxxxx.CommonCode$Enterprise@EENTERPRISE_DISPOSAL) ">
<if test="wasteCategory !=null and wasteCategory !=''">
and wd.waste_category in(
<foreach collection="wasteCategory.split(',')" item="item" index="index" separator=",">
#{item}
</foreach>
)
</if>
</if>
//常量
<if test="(type ==@com.xx.xxxx.xxxxxxxxxxxx.CommonCode@EENTERPRISE_PRODUCE) ">
<if test="wasteCode !=null and wasteCode !=''">
and wp.waste_code in(
<foreach collection="wasteCode.split(',')" item="item" index="index" separator=",">
#{item}
</foreach>
)
</if>
</if>