
使用 MongoTemplate 查询都需要把实体类的属性名字以字符串的信息列出,如何优雅地避免这些魔法值,做到使用类的属性来查询呢? 例如:
实体类:
@Document(collection = "demo") @Data public class Demo { private Date createTime; } 查询语句:
Query query = new Query(where("createTime").gte(fromDate).lte(toDate); 有没有工具类可以直接从 Demo.createTime.getName()的方式获取属性名呢?
1 joesonw 2020 年 6 月 11 日 sprint-data repository??? https://docs.spring.io/spring-data/mongodb/docs/1.2.0.RELEASE/reference/html/mongo.repositories.html |
2 YUyu101 2020 年 6 月 11 日 换 c# |
3 JasonLaw 2020 年 6 月 11 日 via iPhone |
4 aguesuka 2020 年 6 月 12 日 via Android 定义一个 function 接口,继承 serializable 。比如 Serializable fun =(Serializable & Function<>) Dome::createTime 。反射 fun 的 writeReplace 方法,SerializableLambda metadata = (SerializableLambda) fun.writeReplace()//使用反射因为这是个 private 方法 SerializableLambda 中可以获得 Dome 和 creatTime |
5 aguesuka 2020 年 6 月 12 日 via Android |