MongoDB(五)——MongoRepository操作MongoDB
<p><font style="font-size: 40px"></font></p><center><font style="font-size: 40px">MongoRepository操作MongoDB</font></center><p></p><div style="background-color: rgba(100, 149, 237, 1); font-size: 19px; border-radius: 5px"><h1>环境搭建</h1></div>
<h3 id="添加repository类">添加Repository类</h3>
<pre><code class="language-java">@Repository
public interface UserRepository extends MongoRepository<User, String> {
}
</code></pre>
<div style="background-color: rgba(100, 149, 237, 1); font-size: 19px; border-radius: 5px"><h1>添加操作</h1></div>
<pre><code class="language-java">package com.study;
import com.study.mongodb.entiity.User;
import com.study.mongodb.repository.UserRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringbootMongoDbApplicationTest1 {
@Autowired
private UserRepository userRepository;
//添加操作
@Test
void create(){
User user = new User();
user.setAge(20);
user.setName("zhangsan");
user.setEmail("963330213@qq.com");
User user1 = userRepository.save(user);
System.out.println(user1);
}
}
</code></pre>
<p><img src="https://img2020.cnblogs.com/blog/2203076/202108/2203076-20210816172628010-695269371.png" alt="image" loading="lazy"></p>
<div style="background-color: rgba(100, 149, 237, 1); font-size: 19px; border-radius: 5px"><h1>查询操作</h1></div>
<p><strong>1.查询所有</strong></p>
<pre><code class="language-java"> //查询所有
@Test
void findAll(){
List<User> userList = userRepository.findAll();
System.out.println(userList);
}
</code></pre>
<p><img src="https://img2020.cnblogs.com/blog/2203076/202108/2203076-20210816173033280-143648506.png" alt="image" loading="lazy"><br>
<strong>2.根据id查询</strong></p>
<pre><code class="language-java"> //根据id查询
@Test
void findById(){
User user = userRepository.findById("611a2f1f5e7124132f365fa7").get();
System.out.println(user);
}
</code></pre>
<p><img src="https://img2020.cnblogs.com/blog/2203076/202108/2203076-20210816173018993-1439253346.png" alt="image" loading="lazy"></p>
<p><strong>3.条件查询</strong></p>
<pre><code class="language-java"> //条件查询
@Test
void findUserList(){
//name = zhangsan and age = 20
User user = new User();
user.setName("zhangsan");
user.setAge(20);
Example<User> userExample = Example.of(user);
List<User> users = userRepository.findAll(userExample);
System.out.println(users);
}
</code></pre>
<p><img src="https://img2020.cnblogs.com/blog/2203076/202108/2203076-20210816173345797-408249686.png" alt="image" loading="lazy"></p>
<p><strong>4.模糊查询</strong></p>
<pre><code class="language-java"> //模糊查询
@Test
void findLikeUserList(){
//设置模糊查询匹配规则
ExampleMatcher matcher = ExampleMatcher.matching()
.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
.withIgnoreCase(true);//忽略大小写
User user = new User();
user.setName("z"); //包含z的数据
Example<User> userExample = Example.of(user,matcher);
List<User> users = userRepository.findAll(userExample);
System.out.println(users);
}
</code></pre>
<p><img src="https://img2020.cnblogs.com/blog/2203076/202108/2203076-20210816173805581-266869092.png" alt="image" loading="lazy"></p>
<p><strong>5.分页查询</strong></p>
<pre><code class="language-java"> //分页查询
@Test
void findPageUserList(){
//分页
Pageable pageable = PageRequest.of(0, 3);//0代表第一页,3显示3个字段
User user = new User();
user.setName("zhangsan");
Example<User> userExample = Example.of(user);
Page<User> page = userRepository.findAll(userExample, pageable);
System.out.println(page);
}
</code></pre>
<div style="background-color: rgba(100, 149, 237, 1); font-size: 19px; border-radius: 5px"><h1>修改操作</h1></div>
<pre><code class="language-java"> //修改操作
@Test
void updateUser(){
User user = userRepository.findById("611a2f1f5e7124132f365fa7").get();
user.setAge(18);
user.setName("lisi");
User save = userRepository.save(user); //有id则修改,无id则变成添加操作
System.out.println(save);
}
</code></pre>
<p><img src="https://img2020.cnblogs.com/blog/2203076/202108/2203076-20210816174703563-2015658409.png" alt="image" loading="lazy"></p>
<div style="background-color: rgba(100, 149, 237, 1); font-size: 19px; border-radius: 5px"><h1>删除操作</h1></div>
<pre><code class="language-java"> //删除操作
@Test
void deleteUser(){
userRepository.deleteById("611a2f1f5e7124132f365fa7");
}
</code></pre><br><br>
来源:https://www.cnblogs.com/luoxiao1104/p/15149155.html
頁:
[1]