1. 功能

利用spring链接MongoDB,并进行增删改查

2. 类结构图

spring-boot-mongodb_ide

3. 实现

代码结构

spring-boot-mongodb_mongodb_02

实体#UserEntity

spring-boot-mongodb_ide_03

spring-boot-mongodb_mongodb_04

package com.jihite.entity;

public class UserEntity {
private int id;
private String name;
private String passwd;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPasswd() {
return passwd;
}

public void setPasswd(String passwd) {
this.passwd = passwd;
}
@Override
public String toString() {
return String.format("id:%s, name:%s, passwd:%s", id, name, passwd);
}
}

View Code

接口#UserDao

package com.jihite.dao;

import com.jihite.entity.UserEntity;

public interface UserDao {
void saveUser(UserEntity userDao);
UserEntity getUserByName(String name);
long updateUserEntity(UserEntity userEntity);
void deleteUserById(int id);
}

实现#UserDaoImpl

package com.jihite.dao.impl;

import com.jihite.dao.UserDao;
import com.jihite.entity.UserEntity;
import com.mongodb.client.result.UpdateResult;
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.Component;

@Component
public class UserDaoImpl implements UserDao{
@Autowired
private MongoTemplate mongoTemplate;

@Override
public void saveUser(UserEntity userDao) {
mongoTemplate.save(userDao);
}

@Override
public UserEntity getUserByName(String name) {
Query query = new Query(Criteria.where("name").is(name));
return mongoTemplate.findOne(query, UserEntity.class);
}

@Override
public long updateUserEntity(UserEntity userEntity) {
Query query = new Query(Criteria.where("id").is(userEntity.getId()));
Update update = new Update().set("name", userEntity.getName()).set("passwd", userEntity.getPasswd());
UpdateResult writeResult = mongoTemplate.updateFirst(query, update, UserEntity.class);
return writeResult.getMatchedCount();
}

@Override
public void deleteUserById(int id) {
Query query = new Query(Criteria.where("id").is(id));
mongoTemplate.remove(query, UserEntity.class);
}

}

#测试

package com.jihite.dao;

import com.jihite.entity.UserEntity;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserDaoTest {

@Autowired
private UserDao userDao;

@Test
public void testSaveUser() {
UserEntity user = new UserEntity();
user.setId(1);
user.setName("jihite");
user.setPasswd("abc");
userDao.saveUser(user);

}

@Test
public void testGetUserByName() {
UserEntity user = userDao.getUserByName("jihite");
System.out.println(user);
}

@Test
public void testUpdateUserEntity() {
UserEntity user = new UserEntity();
user.setId(1);
user.setName("jihite2");
user.setPasswd("efg");
userDao.updateUserEntity(user);

user = userDao.getUserByName("jihite2");
System.out.println(user);

}

@Test
public void testDeleteUserById() {
userDao.deleteUserById(1);
UserEntity user = new UserEntity();

System.out.println(userDao.getUserByName("jihite"));
System.out.println(userDao.getUserByName("jihite2"));

}
}

4. 注释

4.1 spring boot父级依赖

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

这块配置就是Spring Boot父级依赖,有了这个,当前的项目就是Spring Boot项目了。spring-boot-starter-parent是一个特殊的starter,它用来提供相关的Maven默认依赖,使用它之后,常用的包依赖可以省去version标签。

 

4.2 起步依赖spring-boot-starter-*** 

起步依赖就像品牌机(对比组装机),自动给你封装好了你想要实现的功能的依赖,减少解决依赖关系耗费精力。

spring boot提供了很多起步依赖降低项目依赖的复杂度。

spring-boot-mongodb_spring_05

5. 代码

​链接​​