@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");
}
// passed
可以看到,TestQuery的account字段通过@QueryField注解定义的查询条件被原样拼接到where语句,并且因为查询条件里有3个占位符,account的值“test”也被添加了三次到argList。