依赖
org.springframework.boot
spring-boot-starter-test
2.6.10
org.springframework.boot
spring-boot-starter-data-mongodb
2.6.10
配置文件
spring:
data:
mongodb: #用上面这种查询报错,只能用下面那种
# host: 192.168.208.139
# port: 27017
# database: test
# username: root
# password: 123456
uri: mongodb://root:[email protected]:27017/test?authSource=admin&authenticationMechanism=SCRAM-SHA-1
bean
package com.cyz.bean;
/**
* @author cyz
* @date 2023/7/26 17:07
* @mood happy
*/
public class Stu {
String _id;
String name;
String age;
String gender;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
package com.cyz.bean;
/**
* @author cyz
* @date 2023/7/26 17:10
* @mood happy
*/
public class Person {
String name;
String gender;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
查询操作
package com.cyz;
import com.cyz.bean.Stu;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author cyz
* @date 2023/7/26 16:16
* @mood happy
*/
@SpringBootTest
public class TestApplication {
@Autowired
private MongoTemplate mongoTemplate;
@Test
void query() {
// 查询name=zs
Query query = Query.query(Criteria.where("name").is("cyz"));
//要想使用下面的方法就必须stu的属性和数据库中的某个文档一模一样才行,否则就查询不到数据
mongoTemplate.find(query, Stu.class);
//下面的方法是如果使用的是map接收就会查询所以的字段,如果指定某个bean接收,则只会给匹配上的字段赋值
mongoTemplate.find(query, Map.class, "stu");
// 查询所有
//要想使用下面的方法就必须stu的属性和数据库中的某个文档一模一样才行,否则就查询不到数据
mongoTemplate.findAll(Stu.class);
//下面的方法是如果使用的是map接收就会查询所以的字段,如果指定某个bean接收,则只会给匹配上的字段赋值
mongoTemplate.findAll(Map.class, "stu");
// 分页查询 page页码,pageSize每页展示几个
Integer page = 2;
Integer pageSize = 2;
Pageable pageable = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("age")));
Query query1 = new Query().with(pageable);
mongoTemplate.find(query1, Stu.class, "stu");
// 查询多个
Query query2 = Query.query(Criteria.where("_id").in("64c0d51a682e0000ed002883", "64c0d543682e0000ed002884")).with(Sort.by(Sort.Order.desc("age")));
mongoTemplate.find(query2, Stu.class);
// 查询数量
Query query3 = Query.query(Criteria.where("_id").in("64c0d51a682e0000ed002883", "64c0d543682e0000ed002884")).with(Sort.by(Sort.Order.desc("age")));
mongoTemplate.count(query3, Stu.class);
}
}
添加操作
package com.cyz;
import com.cyz.bean.Stu;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author cyz
* @date 2023/7/26 16:16
* @mood happy
*/
@SpringBootTest
public class TestApplication {
@Autowired
private MongoTemplate mongoTemplate;
@Test
void add() {
List list = new ArrayList<>();
Stu stu = new Stu();//
stu.setName("admin");
stu.setAge("12");
list.add(stu);
//如下两种方法bean的属性必须和文档一致
mongoTemplate.save(stu);
mongoTemplate.insert(stu);
//如下两种方法bean的属性不需要文档一致,如果字段多了就会在文档中创建新的字段
Map map = new HashMap();
map.put("name", "map1");
map.put("age", "323");
map.put("host", "host");
mongoTemplate.save(map, "stu");
mongoTemplate.insert(map, "stu");
//如下两种方法bean的属性必须和文档一致
mongoTemplate.save(list);
mongoTemplate.insert(list);
//如下两种方法bean的属性不需要文档一致,如果字段多了就会在文档中创建新的字段
mongoTemplate.save(list, "stu");
mongoTemplate.insert(list, "stu");
//根据stu属性去数据库中匹配对应的文档,如果没有就就会创建对应的文档,并且要把集合中的字段如果在文档中没有就会创建对应字段
mongoTemplate.insert(list, Stu.class);
}
}
更新操作
package com.cyz;
import com.cyz.bean.Stu;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author cyz
* @date 2023/7/26 16:16
* @mood happy
*/
@SpringBootTest
public class TestApplication {
@Autowired
private MongoTemplate mongoTemplate;
@Test
void update() {
Query query = Query.query(Criteria.where("_id").is("64c0d51a682e0000ed002883"));
Update update = Update.update("name", "zs");
// 更新一条数据
mongoTemplate.updateFirst(query, update, Stu.class);
mongoTemplate.updateFirst(query, update, "stu");
mongoTemplate.updateFirst(query, update, Stu.class, "stu");
// 更新多条数据
mongoTemplate.updateMulti(query, update, Stu.class);
mongoTemplate.updateMulti(query, update, "stu");
mongoTemplate.updateMulti(query, update, Stu.class, "stu");
// 更新数据,如果数据不存在就新增
mongoTemplate.upsert(query, update, Stu.class);
mongoTemplate.upsert(query, update, "stu");
mongoTemplate.upsert(query, update, Stu.class, "stu");
}
}
删除操作
package com.cyz;
import com.cyz.bean.Stu;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author cyz
* @date 2023/7/26 16:16
* @mood happy
*/
@SpringBootTest
public class TestApplication {
@Autowired
private MongoTemplate mongoTemplate;
@Test
void delete() {
Stu stu = new Stu();
stu.set_id("64c0d51a682e0000ed002883");
Query query = Query.query(Criteria.where("_id").in("5d1312aeb1829c279c6c256b", "5d13133ab1829c29d02ce29c"));
// 根据条件删除
mongoTemplate.remove(query);
mongoTemplate.remove(stu);
mongoTemplate.remove(Stu.class);
// 根据条件删除(可删除多条)
mongoTemplate.remove(query, Stu.class, "stu");
}
}