public <T> List<T> queryCommunityByKeyword(String keyword, Integer page, Integer pageSize) {Query query = new Query();query.addCriteria(Criteria.where("communityName").regex(keyword).orOperator(Criteria.where("announcement").regex(keyword).orOperator(Criteria.where("remark").regex(keyword)))).skip( (page - 1) * pageSize).limit(pageSize);//分页System.out.println(query);List<SmsCommunity> list = mongoTemplate.find(query, SmsCommunity.class);if(list == null || list.size() < 1){return null;}return (List<T>) list;}以上是接口的实现代码;Query: { "communityName" : { "$regex" : "车"} , "$or" : [ { "announcement" : { "$regex" : "车"} , "$or" : [ { "remark" : { "$regex" : "车"}}]}]}, Fields: null, Sort: null以上是后台打印的查询语句;我要的效果是(Oracle):SELECT * FROM tbl_name WHERE communityName LIKE ? OR announcement LIKE ? OR remark LIKE ?可是上面的mongoTemplate代码查出来的不是这样的,后面两个or的条件直接被忽略了.下面是我用到的spring整合mongodb的包:import org.springframework.data.mongodb.core.MongoTemplate;
2 回答
qq_笑_17
TA贡献1818条经验 获得超7个赞
代码如下:
Query query = new Query();
Criteria cr = new Criteria();
query.addCriteria(cr.orOperator(
Criteria.where("communityName").regex(keyword)
,Criteria.where("communityName").regex(keyword)
,Criteria.where("remark").regex(keyword)
));
打印的SQL语句如下:
Query: { "$or" : [ { "communityName" : { "$regex" : "俗"}} , { "communityName" : { "$regex" : "俗"}} , { "remark" : { "$regex" : "俗"}}]}, Fields: null, Sort: null
- 2 回答
- 0 关注
- 1240 浏览
添加回答
举报
0/150
提交
取消