1、确保mongodb是否安装
Linux安装docker 保姆级教程_ 来杯咖啡的博客-CSDN博客(可以看这篇文章)
2、代码展示
2.1 使用 MongoTemplate
创建boot项目,导入架包。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gitkeki</groupId>
<artifactId>mongo01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mongo01</name>
<description>mongo01</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--mongodb所需包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
只要jar包
<!--mongodb所需包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
编写配置文件
编写实体类
@Data
@Document("User")
public class User {
@Id //生成id策略
private String id;
private String name;
private Integer age;
private String email;
private String createDate;
}
测试类
@SpringBootTest
public class Mongo01ApplicationTests {
//注入mongoMongoTemplate
@Autowired
private MongoTemplate mongoTemplate;
//添加操作
@Test
public void test1(){
User user = new User();
user.setAge(22);
user.setName("zhangsan");
user.setEmail("123@qq.com");
User user1 = mongoTemplate.insert(user);
System.out.println(user1);
}
//查询所有记录
@Test
public void test2(){
List<User> userList = mongoTemplate.findAll(User.class);
System.out.println("==================>>>>>>"+userList);
}
//id查询
@Test
public void test3(){
User user = mongoTemplate.findById("63721b7b654a227a3eb55ac8", User.class);
System.out.println("==================>>>>>>"+user);
}
//条件查询
@Test
public void test4(){
//name = test and age = 20
Query query = new Query(Criteria.where("name").is("test")
.and("age").is(20));
List<User> user = mongoTemplate.find(query, User.class);
System.out.println("==================>>>>>>"+user);
}
//模糊查询
@Test
public void test5(){
//name like test and age = 20
String name = "est";
String regex = String.format("%s%s%s", "^.*", name, ".*$");
Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
Query query = new Query(
Criteria.where("name").regex(pattern));
List<User> user = mongoTemplate.find(query, User.class);
System.out.println("==================>>>>>>"+user);
}
//分页查询
@Test
public void test6(){
int pageNo = 1;
int pageSize = 5;
//条件构造
String name = "est";
String regex = String.format("%s%s%s", "^.*", name, ".*$");
Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
Query query = new Query(
Criteria.where("name").regex(pattern));
//分页构键
//查询记录数
long count = mongoTemplate.count(query, User.class);
//分页
List<User> users =
mongoTemplate.find(query.skip((pageNo - 1) * pageSize).limit(pageSize), User.class);
System.out.println("==================>>>>>>>>>>总条数"+count);
System.out.println("==================>>>>>>>>>>数据"+users);
}
//修改
@Test
public void test7(){
//根据id查询
User user = mongoTemplate.findById("63721b7b654a227a3eb55ac8", User.class);
//修改值
user.setAge(2);
user.setName("testUpdate");
user.setEmail("129999999993@qq.com");
//调用方法
Query query = new Query(Criteria.where("id").is(user.getId()));
Update update = new Update();
update.set("name", user.getName());
update.set("age", user.getAge());
update.set("email", user.getEmail());
UpdateResult upsert = mongoTemplate.upsert(query, update, User.class);
long count = upsert.getModifiedCount();
if (count>0){
System.out.println("=========>>>>>>>>>成功" + count);
}else{
System.out.println("=========>>>>>>>>>失败" + count);
}
}
//删除
@Test
public void test8(){
Query query = new Query(Criteria.where("id").is("63721b7b654a227a3eb55ac8"));
DeleteResult remove = mongoTemplate.remove(query, User.class);
long count = remove.getDeletedCount();
if (count>0){
System.out.println("=========>>>>>>>>>成功" + count);
}else{
System.out.println("=========>>>>>>>>>失败" + count);
}
}
}
2.2使用 Repository
创建boot项目,导入架包。(与上面的jar是一样的)
编写配置文件(与上面的配置文件是一样的)
编写实体类((与上面的实体类是一样的)
编写Repository类
@Repository
public interface UserRepository extends MongoRepository<User, String> {
}
测试类
@SpringBootTest
class Mongo02ApplicationTests {
@Autowired
private UserRepository userRepository;
//添加操作
@Test
public void test1() {
User user = new User();
user.setAge(20);
user.setName("张三");
user.setEmail("3332200@qq.com");
User user1 = userRepository.save(user);
System.out.println(user1);
}
//查询所有记录
@Test
public void test2() {
List<User> userList = userRepository.findAll();
System.out.println(userList);
}
//id查询
@Test
public void test3() {
User user = userRepository.findById("63722813f46ed0045f89c175").get();
System.out.println(user);
}
//条件查询
@Test
public void test4() {
User user = new User();
user.setName("张三");
user.setAge(20);
Example<User> userExample = Example.of(user);
List<User> userList = userRepository.findAll(userExample);
System.out.println(userList);
}
//模糊查询
@Test
public void test5(){
//设置模糊查询规则
ExampleMatcher matcher = ExampleMatcher.matching()
.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
.withIgnoreCase(true);
User user = new User();
user.setName("tes");
Example<User> userExample = Example.of(user,matcher);
List<User> userList = userRepository.findAll(userExample);
System.out.println(userList);
}
//分页查询
@Test
public void test6(){
//设置分页参数 0 == 第一页
Pageable pageable = PageRequest.of(0, 3);
User user = new User();
user.setName("test1");
Example<User> userExample = Example.of(user);
Page<User> page = userRepository.findAll(userExample, pageable);
System.out.println(page.getContent());
}
//修改
@Test
public void test7(){
User user = userRepository.findById("63722813f46ed0045f89c175").get();
user.setName("张三_1");
user.setAge(25);
user.setEmail("883220990@qq.com");
User save = userRepository.save(user);
System.out.println(save);
}
//删除
@Test
public void test8() {
userRepository.deleteById("5ffbfe8197f24a07007bd6ce");
}
}