SpringBoot集成Mongntemplate 模糊查询、日期比较和分页

pom.xml

        
        
            org.springframework.boot
            spring-boot-starter-data-mongodb
        

配置文件:

spring: 
    data: 
        mongodb: 
            host: localhost
            port: 27017
            database: test-logs

 

model:



import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;

import java.io.Serializable;


@Data
@Document(collection = "test_info")
public class LogsInfo implements Serializable {

    private String id;

    private String projectName;

   
}

controller:

    @GetMapping("/getUserLog")
    public List  getUserLog( Integer page,
                                 Integer size,
                                 String sortRow,
                                 Integer sortDire,
                                 String status,
                                String startTime,
                                 String endTime) {
        Sort sort;
        if (sortDire == 1) {
            sort = new Sort(Sort.Direction.ASC, sortRow);
        } else {
            sort = new Sort(Sort.Direction.DESC, sortRow);
        }
        Pageable pageable = PageRequest.of(page - 1, size, sort);
        Page logsInfoList = logsInfoService.getUserLogs(pageable, status, startTime, endTime);
        return logsInfoList;
    }

service:


import com.micro.model.LogsInfo;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Page;


import java.util.List;

public interface LogsInfoService {

    Page getUserLogs(Pageable pageable, String status, String startTime, String endTime);

}

Impl:


import cn.com.sinosoft.model.LogsInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.regex.Pattern;

import java.util.List;

@Component
public class LogsInfoServiceImpl implements LogsInfoService {
      @Autowired
    MongoTemplate mongoTemplate;

 public Page getUserLogs(Pageable pageable, String status, String startTime, String endTime) {
        //模糊查询
        Pattern statusPattern = Pattern.compile("^" + status + ".*", Pattern.CASE_INSENSITIVE);
        Query query = new Query(
                Criteria.where("status").regex(statusPattern)
                        .andOperator(
                                //小于
                                Criteria.where("endTime").lt(endTime),
                                //大于
                                Criteria.where("startTime").gte(startTime)
                        )
        );
        //分页
        query.with(pageable);
        System.out.println(query.toString());

        List lists = mongoTemplate.find(query, LogsInfo.class);
        long total = mongoTemplate.count(query, LogsInfo.class);
        return new PageImpl(lists, pageable, total);
    }


}

MongoDB可视化工具:robo3t

注意点:

1、授权失败,添加用户

 

use admin

 db.createUser(
   {
     user: "admin",
     pwd: "admin123",
     roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
   } )

你可能感兴趣的:(数据库,mongodb)