1.配置pom文件,添加
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
2.配置application.properties文件
- 由于我本地安装的MongoDB没有用户名密码,所以配置如下:
#MongoDB
spring.data.mongodb.uri=mongodb://127.0.0.1:27017/test
- 带上用户名密码的配置如下:
spring.data.mongodb.uri=mongodb://name:pass@localhost:27017/test,其中name是用户名,pass是密码
3.创建Model对象MongoTest
public class MongoTest {
private Integer id;
private String name;
private String sex;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
4.接口层Controller实现
- MongoTestController
@RestController
@RequestMapping("/mongo")
public class MongoTestController {
@Autowired
private MongoTestService mongoTestService;
/**
* 创建对象接口
* @param mongoTest
* @return
* @throws Exception
*/
@PostMapping(value="/insert")
public JsonResponse<Boolean> saveTest(@RequestBody MongoTest mongoTest) throws Exception {
boolean saveTest = mongoTestService.saveTest(mongoTest);
return new JsonResponse<Boolean>(saveTest);
}
/**
* 根据姓名查找对象接口
* @param name
* @return
*/
@GetMapping(value="/find")
public JsonResponse<MongoTest> findTestByName(@RequestParam(value = "name") String name){
MongoTest mongoTest = mongoTestService.findTestByName(name);
return new JsonResponse<MongoTest>(true, mongoTest);
}
/**
* 更新对象接口
* @param mongoTest
* @return
*/
@PutMapping(value="/update")
public JsonResponse<Boolean> updateTest(@RequestBody MongoTest mongoTest){
boolean updateTest = mongoTestService.updateTest(mongoTest);
return new JsonResponse<Boolean>(updateTest);
}
/**
* 删除对象接口
* @param id
* @return
*/
@DeleteMapping(value="/delete/{id}")
public JsonResponse<Boolean> deleteTestById(@PathVariable(value = "id") int id){
boolean deleteTestById = mongoTestService.deleteTestById(id);
return new JsonResponse<Boolean>(deleteTestById);
}
}
- JsonResponse
public class JsonResponse<T> implements Serializable {
private static final long serialVersionUID = 1L;
private boolean success;
private T data;
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public Object getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public JsonResponse(boolean success, T data) {
super();
this.success = success;
this.data = data;
}
public JsonResponse(boolean success) {
super();
this.success = success;
}
@SuppressWarnings("unchecked")
public JsonResponse(ExceptionEnum error) {
super();
this.success = false;
this.data = (T) error.getExceptionResponse();
}
}
5.业务层Service实现
@Service
public class MongoTestServiceImpl implements MongoTestService {
@Autowired
private MongoTestDao mongoTestDao;
// @Autowired
// private MongoTestRepository mongoTestRepository;
/**
* 根据姓名查找
*/
@Override
public MongoTest findTestByName(String name) {
return mongoTestDao.findTestByName(name);
}
/**
* 创建MongoTest对象
*/
@Override
public boolean saveTest(MongoTest test) {
boolean flag = false;
MongoTest mongoTest = mongoTestDao.saveTest(test);
if (null != mongoTest) {
flag = true;
}
return flag;
}
/**
* 更新MongoTest对象
*/
@Override
public boolean updateTest(MongoTest test) {
boolean flag = false;
UpdateResult updateTest = mongoTestDao.updateTest(test);
if (null != updateTest && updateTest.getModifiedCount() > 0) {
flag = true;
}
return flag;
}
/**
* 根据ID删除对象
*/
@Override
public boolean deleteTestById(Integer id) {
boolean flag = false;
DeleteResult deleteResult = mongoTestDao.deleteTestById(id);
if (null != deleteResult && deleteResult.getDeletedCount() > 0) {
flag = true;
}
return flag;
}
}
6.数据访问层Dao的实现--两种方式:MongoTemplate和MongoRepository
- MongoTemplate
@Component
public class MongoTestDao {
@Autowired
private MongoTemplate mongoTemplate;
/**
* 创建对象
*/
public MongoTest saveTest(MongoTest test) {
return mongoTemplate.save(test);
}
/**
* 根据用户名查询对象
* @return
*/
public MongoTest findTestByName(String name) {
Query query=new Query(Criteria.where("name").is(name));
return mongoTemplate.findOne(query , MongoTest.class);
}
/**
* 更新对象
*/
public UpdateResult updateTest(MongoTest test) {
Query query=new Query(Criteria.where("id").is(test.getId()));
Update update= new Update().set("name", test.getName()).set("sex", test.getSex());
//更新查询返回结果集的第一条
return mongoTemplate.updateFirst(query,update,MongoTest.class);
//更新查询返回结果集的所有
// mongoTemplate.updateMulti(query,update,TestEntity.class);
}
/**
* 删除对象
* @param id
*/
public DeleteResult deleteTestById(Integer id) {
Query query=new Query(Criteria.where("id").is(id));
return mongoTemplate.remove(query,MongoTest.class);
}
}
- MongoRepository
public interface MongoTestRepository extends MongoRepository<MongoTest, Integer> {
/*
* 根据姓名查找
*/
MongoTest findByName(String name);
/**
* 根据姓名,自定义查询--分页
* 需要注意 查询的语法结构 ,同时这里和`jpa`不一样的地方是,第一个索引值从0 开始
* @param name
* @param pageable
* @return
*/
@Query("{'name':?0}")
Page<MongoTest> queryBySql(String name,Pageable pageable);
}
7.测试
- 创建对象
查看数据库:
- 查看对象
- 更新对象--根据ID,如图:更新id为1的对象
查看数据库:
- 删除对象
查看数据库: