https://mybatis.org/mybatis-3/zh/statement-builders.html#
// 动态条件(注意参数需要使用 final 修饰,以便匿名内部类对它们进行访问)
public String selectPersonLike(final String id, final String firstName, final String lastName) {
return new SQL() {{
SELECT("P.ID, P.USERNAME, P.PASSWORD, P.FIRST_NAME, P.LAST_NAME");
FROM("PERSON P");
if (id != null) {
WHERE("P.ID like #{id}");
}
if (firstName != null) {
WHERE("P.FIRST_NAME like #{firstName}");
}
if (lastName != null) {
WHERE("P.LAST_NAME like #{lastName}");
}
ORDER_BY("P.LAST_NAME");
}}.toString();
}
我希望通过这种方式,构造出 sql,传递给另一个服务,他会把我的 sql 当做子查询条件进行查询
但是这样的话,当遇到参数是日期类型时,有什么好的办法吗?只能手动把日期格式化为 String 类型进行拼接吗?
他 demo 中给出的#{firstName}这样的只是个占位符,并不能直接获取入参进行注入