因为springboot版本过高导致,Mongo查询语句中,使用in查询,字段不能出现下划线,比如test_id。于是对Test_id进行其别名解决,通过对springboot的版本降低来解决。降低到2.4.0
更改结果代码:
// 实体类代码
@Data
@Document("test") // 列表数据来源
public class TestEntity extends BaseEntity {
@Field("test_id") // 字段改名 test_id,实际查询的是 test_id
private String testid;
}
原来的代码:
// 实体类代码
@Data
@Document("test") // 列表数据来源
public class TestEntity extends BaseEntity {
private String test_id;
}
Mongo 数据库字段:共有下面三个字段
_id
test_id
content
那么实际使用的是原来的代码,就会报 No property test found for type TestEntity
List<String> str = new ArrayList<>();
str.add("123456");
Query query = new Query(Criteria.where("test_id").in(str));
需要添加 @Field
转换一下,笔者也不太理解需要这一步转换,猜测是和 Mongo 内部的 _id
字段冲突,应该是 MongoTemplate 接口的问题。添加了 @Field
,那么继续使用下面的代码,就不会继续报类似 No property xxx found for type 某个实体类名
的问题了。
List<String> str = new ArrayList<>();
str.add("123456");
Query query = new Query(Criteria.where("test_id").in(str));
特别注意:@Field
只是别名查询,并不是数据返回指定的键名,真正的键名还是实体类定义的testid
,使用的是 testid
,而不是@Field("test_id")
里面的 test_id
如下是查询结果,返回数据:
TestEntity(testid="123456", content="测试内容")