3.3-Query-Criteria 查询
优质
小牛编辑
129浏览
2023-12-01
3.2.1 查询
示例:
QueryCriteria queryCriteria=new QueryCriteria();
queryCriteria.createCriteria().andGreaterThan("id", 100).andLessThan("id",151);
queryCriteria.or().andLessThan("id", 41);
List<User> users = elasticsearchTemplate.find(queryCriteria,User.class);
3.2.2 获取单列数据get
示例:
QueryCriteria queryCriteria=new QueryCriteria();
queryCriteria.createCriteria().andGreaterThan("id", 100).andLessThan("id",151);
queryCriteria.or().andLessThan("id", 41);
User user= elasticsearchTemplate.get(queryCriteria,User.class);
3.2.3 获取总数 count
示例:
QueryCriteria queryCriteria=new QueryCriteria();
int total = elasticsearchTemplate.count(queryCriteria,User.class);
3.2.4 动态 查询
示例:
QueryCriteria queryCriteria=new QueryCriteria();
QueryCriteria.Criteria criteria = queryCriteria.createCriteria();
if(user.getUserName()!= null){
criteria.andLike("userName", user.getUserName() + "%");
}
if(user.getId() != null){
criteria.andGreaterThan("id", user.getId());
}
List<User> users =elasticsearchTemplate.find(queryCriteria,User.class);
3.2.5 排序
示例:
QueryCriteria queryCriteria=new QueryCriteria();
//单字段排序
Sorts sort=new Sorts(Sorts.Direction.DESC, "id");
//多字段排序
Sorts.Order order=new Sorts.Order(Sorts.Direction.DESC, "id");;
Sorts.Order order1=new Sorts.Order(Sorts.Direction.DESC, "createTime");;
Sorts sort=new Sorts(order,order1);
queryCriteria.order(sort);
List<User> users =elasticsearchTemplate.find(queryCriteria,User.class);
3.2.6 动态映射对象
示例:
QueryCriteria queryCriteria=new QueryCriteria();
QueryCriteria.Criteria criteria = queryCriteria.createCriteria();
if(user.getUserName()!= null){
criteria.andLike("userName", user.getUserName() + "%");
}
queryCriteria.entityClass(UserDTO.class);
List<UserDTO> users =elasticsearchTemplate.find(queryCriteria,User.class);
3.2.8 mapping动态组装对象
示例:
QueryCriteria queryCriteria=new QueryCriteria();
QueryCriteria.Criteria criteria = queryCriteria.createCriteria();
if(user.getUserName()!= null){
criteria.andLike("userName", user.getUserName() + "%");
}
queryCriteria.mapping("id","userId"); //初始化映射数据库对象User(DO)字段id 动态映射组装UserDTO的字段为userId
queryCriteria.entityClass(UserDTO.class); //组装UserDTO(DTO)
List<UserDTO> users=elasticsearchTemplate.find(queryCriteria,User.class);
3.2.12 分页
示例:
QueryCriteria queryCriteria=new QueryCriteria();
queryCriteria.page(1,2);
QueryCriteria.Criteria criteria = queryCriteria.createCriteria();
if(user.getUserName()!= null){
criteria.andLike("userName", user.getUserName() + "%");
}
queryCriteria.entityClass(UserDTO);
Page<UserDTO> pageInfo=easyJdbcTemplate.findByPage(queryCriteria,,User.class);