SpringBoot整合MongoDB实现增删改查
程序员文章站
2022-05-07 12:44:33
...
-
新建一个SpringBoot工程,在pom文件中导入相关依赖
<dependencies> <!--springboot整合mongodb依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <!--springboot依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
-
修改配置文件为yml格式(application.yml),并配置下面的配置项
spring: data: mongodb: uri: mongodb://localhost:27017/springboot-db #springboot-db为数据库的名称
-
新建一个实体类,用来保存数据
public class Customer { public String id; public String firstName; public String lastName; public Customer() { } public Customer(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } @Override public String toString() { return "Customer{" + "id='" + id + '\'' + ", firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + '}'; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } }
-
新建一个实体类对应的dao接口(如果你想自定义一些查询,比如根据firstName来查询,获取根据lastName来查询,只需要定义一个方法即可。注意firstName严格按照存入的mongodb的字段对应。在典型的java的应用程序,写这样一个接口的方法,需要自己实现,但是在springboot中,只需要按照格式写一个接口名和对应的参数就可以了,因为springboot已经实现好了)
import com.kangswx.springbootmongodb.domain.Customer; import org.springframework.data.mongodb.repository.MongoRepository; import java.util.List; public interface CustomerRepository extends MongoRepository<Customer, String>{ Customer findByFirstName(String firstName); List<Customer> findByLastName(String lastName); }
-
写一个测试类,测试对应的增删改查的功能
import com.kangswx.springbootmongodb.domain.Customer; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.List; import java.util.Optional; @RunWith(SpringRunner.class) @SpringBootTest public class CustomerRepositoryTest { @Autowired private CustomerRepository repository; /** * 新添加数据 */ @Test public void addTest(){ Customer customer = repository.save(new Customer("Bob", "Smith")); System.out.println("保存结果"+customer); } /** * 查询所有的数据 */ @Test public void findAllTest(){ List<Customer> list = repository.findAll(); for(int i = 0; i < list.size(); i++){ System.out.println("i: " + list.get(i)); } } /** * 根据FirstName查询数据 */ @Test public void findByFirstNameTest(){ Customer customer = repository.findByFirstName("Alice"); System.out.println(customer); } /** * 根据LastName查询数据 */ @Test public void findByLastNameTest(){ List<Customer> list = repository.findByLastName("Smith"); for(int i = 0; i < list.size(); i++){ System.out.println("i: " + list.get(i)); } } /** * 根据id查询数据 */ @Test public void findById(){ Optional<Customer> customer = repository.findById("5d32760ae044c50370e61b58"); System.out.println(customer.get()); } /** * 根据id删除数据 */ @Test public void deleteTest(){ repository.deleteById("5d32760ae044c50370e61b58"); } }
-
具体的代码见 SpringBoot整合MongoDB实现增删改查
推荐阅读
-
Springboot整合微信小程序实现登录与增删改查
-
springboot整合Elasticsearch实现增删改查
-
SpringBoot整合MongoDB实现文件上传下载删除
-
webpack4+express+mongodb+vue实现增删改查的示例
-
SpringBoot整合MyBatis实现增删改查案例完整版(附源代码)
-
SpringBoot模拟员工数据库并实现增删改查操作
-
记录一下springboot+mybatis实现增删改查的基本流程和心得
-
express+mongoose实现对mongodb增删改查操作详解
-
mongodb+php实现简单的增删改查
-
SpringBoot工程下MongoDB增删改查,以及复杂查询和更新