将枚举、工具类、常量、对象等绑定到sql
上下文中,然后在xml中使用它。其实就是把 <bind />
标签用注解的方式配置
如:
<select id="test" resultType="">
SELECT *
FROM user
WHERE 1 = 1
<if test="StringUtils.isNotBlank(userId)">
AND user_id = #{userId}
</if>
</select>
<dependency>
</dependency>
枚举类必须提供 getCode()
方法,并返回枚举值
mapper 接口:
@Bindings({
@Binding(varType = BaseBinding.VarType.ENUM, type = LogisticsFeeConfirmationDetailInvoiceStatusApiEnum.class, alias = ""),
@Binding(varType = BaseBinding.VarType.ENUM, type = LogisticsFeeConfirmationDetailPayStatusApiEnum.class, alias = "pay")
})
这样就会将两个枚举的 所有变量
绑定到sql上下文中。如果有别名,那么只可以用别名来引用,没有别名只能用枚举(ps:最好加个别名,有同名的变量会被覆盖),然后在xml中使用:
xml:
AND a.pay_status = #{pay.PAID}
AND b.invoice_status = #{WAIT_INVOICE}
mapper 接口:
@Bindings({
@Binding(type = StringUtils.class, alias = "v", varType = BaseBinding.VarType.UTIL),
@Binding(type = DateUtils.class, alias = "d", varType = BaseBinding.VarType.UTIL)
})
会将 StringUtils
和 DateUtils
两个对象绑定,并且还可以给对象取别名 alias
。要调用 StringUtils.class
类中的方法,可以 StringUtils.isBlank
或 v.isBlank
。
方法不一定要申明为静态方法,非静态的也可以
xml: #{UTIL_CLASS.method()} or #{alias.VAR_NAME}
<if test="v.isNotBlank(other_var)">
AND table_column like CONCAT('%', #{other_var}, '%')
</if>
<if test="StringUtils.isBlank(other_var)">
AND table_column like CONCAT('%', #{other_var}, '%')
</if>
mapper 接口:
@Bindings({
@Binding(type = ObjectCache.class, alias = "b", varType = BaseBinding.VarType.STATIC, varName = {"TEST_NAME"})
})
绑定 ObjectCache
类中的 TEST_NAME
静态变量
xml: #{VAR_NAME} or #{alias.VAR_NAME}
AND table_column like CONCAT('%', #{b.TEST_NAME}, '%')
AND table_column like CONCAT('%', #{TEST_NAME}, '%')
@Bindings({
@Binding(alias = "e", varType = BaseBinding.VarType.EXPRESSION, expression = "@com.ddmcc.mybatis.bindings.helper.ObjectCache@TEST_NAME"),
@Binding(alias = "c", varType = BaseBinding.VarType.EXPRESSION, expression = "new com.ddmcc.mybatis.bindings.model.User('数据', 2)"),
@Binding(alias = "d", varType = BaseBinding.VarType.EXPRESSION, expression = "@com.ddmcc.mybatis.bindings.model.enums.DingStatusEnum@APPROVALING.getCode()")
})
相当于标签:
<bind name = 'alias' value = 'expression' />
使用:
if中使用变量: <if test="c.userName == 'xxx'">
或者参数拼接: AND table_column = #{c.age}
或者调用方法: <if test="c.method1()"> <if test="c.method2(c.userName)">
或者枚举常量:git config --global user.name "bryan sun" AND table_column = #{d}
或者静态变量: AND table_column = #{e}