• 企业级 SpringBoot 教程 (八)springboot整合mongodb


    准备工作

    环境依赖

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

    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-mongodb</artifactId>
            </dependency>
    

      

    数据源配置

    如果mongodb端口是默认端口,并且没有设置密码,可不配置,sprinboot会开启默认的。

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

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

    spring.data.mongodb.uri=mongodb://name:pass@localhost:27017/dbname
    

      

    定义一个简单的实体

    mongodb

    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已经帮你实现了。

    测试

    @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);
            }
        }
    

      资料和源码来源地址

  • 相关阅读:
    沙盒解决方案与场解决方案之间的差异
    Windows 7:77 个 Windows 7 提示
    SharePoint disable loopback check
    SharePoint 2010 工作流解决方案:序言
    SharePoint 2010 查看“运行时错误”
    sharepoint 链接库链接在新窗口打开
    如何启用SharePoint 2010的代码块
    沙盒解决方案注意事项
    ie8.0 不能用document.all兼容IE7模式
    php导出数据到excel,防止身份证等数字字符格式变成科学计数的方法
  • 原文地址:https://www.cnblogs.com/xiamudaren/p/8397992.html
Copyright © 2020-2023  润新知