springboot+Mongodb开发环境搭建

1 引入依赖包

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

2 配置配置文件
spring.data.mongodb.database=lg_resume
spring.data.mongodb.host=47.100.249.136
spring.data.mongodb.port=27017
spring.data.mongodb.username=*******
spring.data.mongodb.password=******
#认证的库
spring.data.mongodb.authentication-database=admin
3 创建bean对象
在这里插入图片描述
4 创建dao

package com.lagou.dao;

import com.lagou.bean.Resume;
import org.springframework.beans.factory.annotation.Autowired;
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.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ResumeDao {

@Autowired
private MongoTemplate mongoTemplate;

/***
 * 插入数据
 * @param resume
 */
public void add(Resume resume){
    mongoTemplate.insert(resume);
}

/***
 * 通过名字查询
 * @param name
 * @return
 */
public List<Resume> findByName(String name){
    //构建查询条件
    Query query = new Query(Criteria.where("name").is(name));
    List<Resume> resumes = mongoTemplate.find(query, Resume.class);
    return  resumes;
}

/**
 * 通过名字和薪水查询
 * @param name 名字
 * @param expectSalary 薪水
 * @return
 */
public Resume findByNameAndExpectSalary(String name,double expectSalary){
    Query query = new Query(Criteria.where("name").is(name)
            .andOperator(Criteria.where("expectSalary").is(expectSalary)));
    Resume resume = mongoTemplate.findOne(query, Resume.class);
    return resume;
}

/**
 * 范围查询,查询高于此薪水的数据
 * @param expectSalary
 * @return
 */
public List<Resume> findExpectSalary(double expectSalary){
    Query query = new Query(Criteria.where("expectSalary").gt(expectSalary));
    return mongoTemplate.find(query, Resume.class);
}

/**
 * 通过id更新名字和薪资
 * @param resume
 */
public void updateById(Resume resume){
    //构建查询条件
    Query query = new Query(Criteria.where("id").is(resume.getId()));
    //构建需要修改的语句
    Update update = new Update().set("name",resume.getName()).set("expectSalary",resume.getExpectSalary());
    //更新查询返回结果集的第一条
    mongoTemplate.updateFirst(query,update,Resume.class);
    //更新查询返回结果集的所有
   // mongoTemplate.updateMulti(query,update,Resume.class);

}

/**
 * 通过id删除数据
 * @param id
 */
public void deleteById(String id){
    Query query = new Query(Criteria.where("id").is(id));
    mongoTemplate.remove(query,Resume.class);
}

}
5 创建服务层接口
package com.lagou.service;

import com.lagou.bean.Resume;

import java.util.List;

public interface ResumeService {

/**
 *
 * @param resume
 */
public void add(Resume resume);

/***
 * 通过名字查询
 * @param name
 * @return
 */
public List<Resume> findByName(String name);

/**
 * 通过名字和薪水查询
 * @param name 名字
 * @param expectSalary 薪水
 * @return
 */
public Resume findByNameAndExpectSalary(String name,double expectSalary);

/**
 * 查询薪水大于expectSalary的数据
 * @param expectSalary
 * @return
 */
List<Resume> findExpectSalary(double expectSalary);

/**
 * 通过id更新名字和薪资
 * @param resume
 */
public void updateById(Resume resume);

/**
 *
 * @param id
 */
public void deleteById(String id);

}
6 实现接口
package com.lagou.service.Impl;

import com.lagou.bean.Resume;
import com.lagou.dao.ResumeDao;
import com.lagou.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ResumeServiceImpl implements ResumeService {

@Autowired
private ResumeDao resumeDao;
@Override
public void add(Resume resume) {
    resumeDao.add(resume);
}

@Override
public List<Resume> findByName(String name) {
    return resumeDao.findByName(name);
}

@Override
public Resume findByNameAndExpectSalary(String name, double expectSalary) {
    return resumeDao.findByNameAndExpectSalary(name,expectSalary);
}

@Override
public List<Resume> findExpectSalary(double expectSalary) {
    return resumeDao.findExpectSalary(expectSalary);
}

@Override
public void updateById(Resume resume) {
    resumeDao.updateById(resume);
}

@Override
public void deleteById(String id) {
    resumeDao.deleteById(id);
}

}
7 写测试代码
在这里插入图片描述


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值