1.添加相应依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
2.配置文件中配置
#mongodb配置 spring.data.mongodb.uri=mongodb://127.0.0.1:27017/testdb
如果没有配置数据库,但本地有mongodb服务,数据默认存到test中
3.测试类测试
添加实体类
package com.example.java.data.domain;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
/**
* @author ouqf
* @date 2023/10/21 16:01
*/
@Data
@Document("MongoUser")
public class MongoUser {
@Id
private String id;
private String name;
private Integer age;
private String email;
private String createTime;
}
使用现成的类MongoTemplate进行对应增删改查测试
操作中可以指定集合名称,集合名称相当于MySQL的表,再在集合中进行文档数据操作。
package com.example.java.data;
import com.alibaba.fastjson2.JSON;
import com.example.java.data.domain.MongoUser;
import com.mongodb.client.result.DeleteResult;
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.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 java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;
/**
* @author ouqf
* @date 2023/10/21 16:05
*/
@SpringBootTest
public class MongoTests {
private final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Autowired
private MongoTemplate mongoTemplate;
@Test
void createDbTest() {
}
@Test
void insertDocTest() {
MongoUser mongoUser = new MongoUser();
mongoUser.setAge(28);
mongoUser.setId("10005");
mongoUser.setName("高兴");
mongoUser.setEmail("gaox@163.com");
mongoUser.setCreateTime(sf.format(new Date()));
MongoUser res = mongoTemplate.insert(mongoUser, "nio_user");
System.out.println("返回结果:" + JSON.toJSONString(res));
}
@Test
void findDocListTest() {
// 排序
Query query = new Query();
query.with(Sort.by(Sort.Direction.DESC, "createTime"));
// List<MongoUser> ls = mongoTemplate.findAll(MongoUser.class, "nio_user");
List<MongoUser> ls = mongoTemplate.find(query, MongoUser.class, "nio_user");
System.out.println("序号\t\t姓名\t\t创建时间\t\t\t邮箱");
for (MongoUser mongoUser : ls) {
System.out.println(mongoUser.getId() + " " + mongoUser.getName() + "\t"
+ mongoUser.getCreateTime() + "\t" + mongoUser.getEmail());
}
System.out.println("--------------------------------------------------");
}
@Test
void findDocListByConditionTest() {
// Query query = new Query(Criteria.where("email").is("gaox@163.com"));
// Query query = new Query(Criteria.where("id").gte("10010"));
// 右匹配模糊查询
Pattern pattern = Pattern.compile("^.*qq.com$", Pattern.CASE_INSENSITIVE);
Query query = new Query(Criteria.where("email").regex(pattern)
.and("age").gte(30));
// 排序
query.with(Sort.by(Sort.Direction.ASC, "id"));
List<MongoUser> ls = mongoTemplate.find(query, MongoUser.class, "nio_user");
for (MongoUser user : ls) {
System.out.println(JSON.toJSONString(user));
}
}
@Test
void findDocByIdTest() {
MongoUser user = mongoTemplate.findById("10010", MongoUser.class,"nio_user");
System.out.println(user);
}
@Test
void deleteDocTest() {
Query query = new Query(Criteria.where("name").is("高兴"));
DeleteResult res = mongoTemplate.remove(query, MongoUser.class, "nio_user");
System.out.println(res);
}
}
4.数据库查看
采用命令行或相关GUI数据库软件查看。
命令行参考:菜鸟教程