mongodb使用笔记,增加、删除、分页查询

由于在项目中使用mongodb时,数据库已经被安装成功,所以在这块不讲怎么安装mongodb数据库

一、在配置文件中填入mongodb的配置信息

spring:
  data:
    mongodb:
      #数据库地址
      host: localhost
      #数据库端口
      port: 27017
      #数据库登陆名
      username: model
      #数据库登陆密码
      password: model
      #数据库名称
      database: model

二、创建实体类


public class Model {
	
	@Id
	private String id;
	private String name;
	private int age;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}


/**
 *查询和删除Model信息时使用这个实体类
 */
public class ModelRequest implements Serializable {
	private static final long serialVersionUID = -6635068513666846002L;
	private Date startTime;
	private Date endTime;
	//查询条件queryCriteria
	private String queryCriteria;
	private int pageNumber;
    private int pageSize;
	public Date getStartTime() {
		return startTime;
	}
	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}
	public Date getEndTime() {
		return endTime;
	}
	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}
	public String getQueryCriteria() {
		return queryCriteria;
	}
	public void setQueryCriteria(String queryCriteria) {
		this.queryCriteria = queryCriteria;
	}
	public int getPageNumber() {
		return pageNumber;
	}
	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

    
}

三、创建操作类(通过这个类就能和数据库打交道)

import org.springframework.data.mongodb.repository.MongoRepository;

import com.shsc.log.entity.ApiLog;

public interface ModelDao extends MongoRepository{

}

四、创建Service 接口


public interface ModelService {
	 
	 public boolean save(Model model);
	 
	 public boolean delete (Model model);
	 //分页查询
	 public Page findPageList(ModelPage model);
}

实现Service

@Service
public class OperationLogServiceImpl implements OperationLogService {
	@Autowired
	private ModelDao modelDao;
	@Autowired
	//这个也可以操纵mongodb数据库,功能很强大,熟悉的话,可以取代modelDao,完全依靠mongoTemplate和数据库打交道
	private MongoTemplate mongoTemplate;
	//增加
	@Override
	public boolean save(Model model ) {
		model .setId(UUID.randomUUID().toString().replace("-", ""));
		return modelDao.save(model ) != null;
	}
	//删除
	@Override
	public boolean delete(ModelRequest modelRequest ) {
		Query query = new Query();//放查询条件的地方
		Criteria criteria = new Criteria();//查询条件
		if (operationLogRequest.getStartTime() != null && operationLogRequest.getEndTime() != null) {
			criteria.andOperator(
 			    	Criteria.where("logTime").gte(operationLogRequest.getStartTime()),
					Criteria.where("logTime").lt(operationLogRequest.getEndTime())
			);
		}
		query.addCriteria(criteria);//将criteria放入query里面执行
		return mongoTemplate.remove(query, Model.class) != null;
	}
    //分页查询
	@Override
	public Page findPageList(OperationLogRequest operationLogRequest) {
		int page = operationLogRequest.getPageNumber();
		int size = operationLogRequest.getPageSize();
		Sort sort = new Sort(Sort.Direction.DESC, "logTime");
		Pageable pageable = new PageRequest(page - 1, size, sort);
		Query query = new Query();
		Criteria criteria = new Criteria();
		if (operationLogRequest.getStartTime() != null && operationLogRequest.getEndTime() != null) {
			criteria.andOperator(
					Criteria.where("logTime").gte(operationLogRequest.getStartTime()),
					Criteria.where("logTime").lt(operationLogRequest.getEndTime())
			);
		}
		Criteria queryCriteria = new Criteria();
		// 增加条件查询
		if (operationLogRequest.getQueryCriteria() != null) {
				queryCriteria.orOperator(Criteria.where("requestUserName").regex(operationLogRequest.getQueryCriteria()),
					Criteria.where("systemName").regex(operationLogRequest.getQueryCriteria()),
					Criteria.where("systemCode").regex(operationLogRequest.getQueryCriteria()));
		}
		//这个query里面放了多个条件,但是总的来看是一个new Criteria(),Criteria里面又包含了(criteria, queryCriteria)
		query.addCriteria(new Criteria().andOperator(criteria, queryCriteria));
		long total = operationinLogDao.count();
		List items = mongoTemplate.find(query.with(pageable), OperationLog.class);
		return new PageImpl(items, pageable, total);
	}
}

你可能感兴趣的:(学习笔记)