目录
1 Java连接
1.1 Maven依赖
1.2 客户端代码
2 Spring Boot连接
2.1 Maven依赖
2.2 配置文件
2.3 service层代码
笔者所用的MongoDB的版本是4.2.6。
1 Java连接
1.1 Maven依赖
org.mongodb
mongodb-driver
3.12.4
1.2 客户端代码
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import org.bson.Document;
import java.util.ArrayList;
import java.util.List;
/**
* MongoDB客户端
*
* @author Robert Hou
* @date 2020年05月08日 22:35
**/
public class MongoDBClient {
private static final String MONGODB_URL = "mongodb://127.0.0.1:27017";
private MongoDBClient() {
}
public static void main(String[] args) {
MongoClientURI mongoClientURI = new MongoClientURI(MONGODB_URL);
//取得MongoDB客户端
MongoClient mongoClient = new MongoClient(mongoClientURI);
//取得test库下的Product集合
MongoCollectioncollection = mongoClient.getDatabase("test").getCollection("Product");
//创建文档
Listdocuments = new ArrayList<>();
Document document1 = new Document("Name", "Robert Hou")
.append("Gender", "Male")
.append("Tel", "18500000000")
.append("Location", new Document("City", "Beijing").append("District", "Fengtai"));
documents.add(document1);
Document document2 = new Document("Name", "Tony")
.append("Gender", "Male")
.append("Tel", "13400000000")
.append("Location", new Document("Province", "Guangxi").append("City", "Nanning"));
documents.add(document2);
collection.insertMany(documents);
//修改文档内容
Document document3 = new Document("$set", new Document("Tel", "13400000001"));
collection.updateMany(Filters.eq("Name", "Tony"), document3);
//查询全部文档
for (Document cur : collection.find()) {
System.out.println(cur.toJson());
}
mongoClient.close();
}
}
2 Spring Boot连接
2.1 Maven依赖
org.springframework.boot
spring-boot-starter-data-mongodb
2.2 配置文件
spring.data.mongodb.uri=mongodb://127.0.0.1:27017/test
2.3 service层代码
import com.hys.mongodb.springboot.model.User;
import com.hys.mongodb.springboot.service.UserService;
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.Service;
/**
* 用户接口实现
*
* @author Robert Hou
* @date 2020年05月10日 02:44
**/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private MongoTemplate mongoTemplate;
@Override
public void saveUser(User user) {
mongoTemplate.save(user);
}
@Override
public void deleteUserById(Long id) {
Query query = new Query(Criteria.where("id").is(id));
mongoTemplate.remove(query, User.class);
}
@Override
public void updateUser(User user) {
Query query = new Query(Criteria.where("id").is(user.getId()));
Update update = new Update().set("username", user.getUsername()).set("password", user.getPassword());
mongoTemplate.updateMulti(query, update, User.class);
}
@Override
public User findUserByUsername(String username) {
Query query = new Query(Criteria.where("username").is(username));
return mongoTemplate.findOne(query, User.class);
}
}