mongodb+maven+springboot的搭建

本文介绍了如何利用Maven构建工具和SpringBoot框架来搭建包含MongoDB数据库的项目。首先从新建项目开始,然后按照特定的文件目录结构创建文件,接着配置Customer相关类和Repository接口,同时配置了application.yml文件以连接MongoDB。此外,pom.xml中包含了SpringBoot的配置信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.首先new project



2文件目录,按照文件目录创建


3配置文件

Customer

package com.augmentum.quncrm.model;
import org.springframework.data.annotation.Id;
public class Customer {
    @Id
    public String id;

    public String firstName;
    public String lastName;

    public Customer() {}

    public Customer(String id,String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.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;
    }

    @Override
    public String toString() {
        return String.format(
                "com.augmentum.quncrm.model.Customer[id=%s, firstName='%s', lastName='%s']",
                id, firstName, lastName);
    }
}

CustomerRepository

package com.augmentum.quncrm.model;
import org.springframework.data.annotation.Id;
public class Customer {
    @Id
    public String id;

    public String firstName;
    public String lastName;

    public Customer() {}

    public Customer(String id,String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.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;
    }

    @Override
    public String toString() {
        return String.format(
                "com.augmentum.quncrm.model.Customer[id=%s, firstName='%s', lastName='%s']",
                id, firstName, lastName);
    }
}

testmongo

package com.augmentum.quncrm;

import com.augmentum.quncrm.model.Customer;
import com.augmentum.quncrm.repository.CustomerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class testmongo implements CommandLineRunner {

    @Autowired
    private CustomerRepository repository;

    public static void main(String[] args) {
        SpringApplication.run(testmongo.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        //clear
        repository.deleteAll();

        // insert a list of customers
        insert();

        // fetch all customers
        print();

        // fetch an individual customer
        System.out.println("com.augmentum.quncrm.model.Customer found with findByFirstName('Alice'):");
        System.out.println("--------------------------------");
        System.out.println(repository.findByFirstName("Alice"));
        System.out.println();

        System.out.println("Customers found with findByLastName('Smith'):");
        System.out.println("--------------------------------");
        for (Customer customer : repository.findByLastName("Smith")) {
            System.out.println(customer);
        }

        // delete  customers
        System.out.println("Customers deleteById('1') :");
        System.out.println("-------------------------------");
        repository.deleteById("1");
        //repository.deleteById("3");
        print();
        System.out.println("Customers delete(customer):");
        System.out.println("-------------------------------");
        Customer customer = new Customer();
        customer.setId("2");
        repository.delete(customer);
        print();

        //update customers
        System.out.println("Customers save(customer1) Jack White change to Jack Black:");
        System.out.println("-------------------------------");
        Customer customer1=new Customer();
        customer1.setId("4");
        customer1.setLastName("Black");
        repository.save(customer1);
        print();
    }
    void insert()
    {
        repository.insert(new Customer("1","Alice", "Smith"));
        repository.insert(new Customer("2","Bob", "Smith"));
        repository.insert(new Customer("3","May", "James"));
        repository.insert(new Customer("4","Jack", "White"));
        repository.insert(new Customer("5","Ethan", "Chen"));
        repository.insert(new Customer("6","Allen", "Wang"));
        repository.insert(new Customer("7","Baby", "Dollar"));
    }
    void print() {
        System.out.println("Customers found with findAll():");
        System.out.println("-------------------------------");
        for (Customer customer : repository.findAll()) {
            System.out.println(customer);
        }
        System.out.println();
    }
}

resources目录下的application.yml

spring:
  data:
    mongodb:
      uri: mongodb://localhost:27017/springboot-db

pom.xml 这里有springboot 的配置信息https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/reference/htmlsingle/#getting-started-installing-spring-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 http://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.0.1.RELEASE</version>
    </parent>
    <groupId>mongo1</groupId>
    <artifactId>mongo1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
    </dependencies>
</project>
一定要import,会有自动提示
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值