java版电子商务spring cloud分布式微服务b2b2c社交电商 (八)springboot整合mongodb

电子商务社交平台源码请加企鹅求求:三五三六二四七二五九

准备工做

  • 安装 MongoDB
  • jdk 1.8
  • maven 3.0
  • idea

环境依赖

在pom文件引入spring-boot-starter-data-mongodb依赖:java

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>复制代码

数据源配置

若是mongodb端口是默认端口,而且没有设置密码,可不配置,sprinboot会开启默认的。spring

spring.data.mongodb.uri=mongodb://localhost:27017/springboot-db复制代码

mongodb设置了密码,这样配置:mongodb

spring.data.mongodb.uri=mongodb://name:pass@localhost:27017/dbname复制代码

定义一个简单的实体

mongodbspringboot

复制代码
package com.forezp.entity;
 
import org.springframework.data.annotation.Id;
 
 
public class Customer {
 
    @Id
    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 String.format(
                "Customer[id=%s, firstName='%s', lastName='%s']",
                id, firstName, lastName);
    }
 
}复制代码
复制代码

数据操做dao层

public interface CustomerRepository extends MongoRepository<Customer, String> {
 
    public Customer findByFirstName(String firstName);
    public List<Customer> findByLastName(String lastName);
 
}复制代码

写一个接口,继承MongoRepository,这个接口有了几本的CURD的功能。若是你想自定义一些查询,好比根据firstName来查询,获取根据lastName来查询,只须要定义一个方法便可。注意firstName严格按照存入的mongodb的字段对应。在典型的java的应用程序,写这样一个接口的方法,须要本身实现,可是在springboot中,你只须要按照格式写一个接口名和对应的参数就能够了,由于springboot已经帮你实现了。bash

测试

复制代码
@SpringBootApplication
public class SpringbootMongodbApplication  implements CommandLineRunner {
 
 
    @Autowired
    private CustomerRepository repository;
 
    public static void main(String[] args) {
        SpringApplication.run(SpringbootMongodbApplication.class, args);
    }
 
 
    @Override
    public void run(String... args) throws Exception {
        repository.deleteAll();
 
        // save a couple of customers
        repository.save(new Customer("Alice", "Smith"));
        repository.save(new Customer("Bob", "Smith"));
 
        // fetch all customers
        System.out.println("Customers found with findAll():");
        System.out.println("-------------------------------");
        for (Customer customer : repository.findAll()) {
            System.out.println(customer);
        }
        System.out.println();
 
        // fetch an individual customer
        System.out.println("Customer found with findByFirstName('Alice'):");
        System.out.println("--------------------------------");
        System.out.println(repository.findByFirstName("Alice"));
 
        System.out.println("Customers found with findByLastName('Smith'):");
        System.out.println("--------------------------------");
        for (Customer customer : repository.findByLastName("Smith")) {
            System.out.println(customer);
        }
    }复制代码
复制代码

电子商务社交平台源码请加企鹅求求:三五三六二四七二五九maven

相关文章
相关标签/搜索