自定义查询字段
使用场景
当遇到现有的字段映射方法无法支持的SQL语句时,可以使用注解@QueryField
自定义SQL语句,作为临时方案。
注解定义
@Target(FIELD)
@Retention(RUNTIME)
public @interface QueryField {
String and();
}
代码示例
业务代码:
public class TestQuery extends PageQuery {
@QueryField(and = "(username = ? OR email = ? OR mobile = ?)")
private String account;
}
单元测试:
@Test
void testQueryField() {
TestQuery testQuery = TestQuery.builder().account("test").build();
ArrayList<Object> argList = new ArrayList<>();
String sql = BuildHelper.buildWhere(testQuery, argList);
assertThat(sql).isEqualTo(" WHERE (username = ? OR email = ? OR mobile = ?)");
assertThat(argList).containsExactly("test", "test", "test");
}
TestQuery
的account字段通过@QueryField
注解定义的查询条件被原样拼接到WHERE语句,并且因为查询条件里有3个占位符,account的值"test"也被三次添加到argList。
最后更新于
这有帮助吗?